IOS development-iOS app localization-Text Information Localization

Source: Internet
Author: User

Text Information localization occupies a large proportion in the localization work. Including Application name localization, system buttons and information localization, and static text information localization.

System buttons and information Localization

Remember the "finish" button behind the weather forecast application. It is "finished" in the Chinese environment and "Done" in the English environment ".

There are also some system prompts for us to connect to the bluetooth device. It is a Chinese prompt in the Chinese environment, and an English prompt in the English environment.

The text on the system button and the text of the system prompt information cannot be modified, but if we do not set the localization, even the basic information is always displayed in English. We can try to create a screen in the story board and place two system buttons Done and Edit in the navigation bar. Then run the command in English and Chinese respectively to check whether there are any changes.

In fact, they have never changed. Why? The reason is that we didn't perform localization settings for the PROJECT. Open the PROJECT in the PROJECT and select L10N. Click "+" under Localizations, and select "Chinese (zh-Hans)" in the pop-up menu )", in this way, the simplified Chinese localized file is added.

In this way, our project supports both Chinese and English localization, and the system buttons and prompt information have also been localized. You do not need to write code in this process.

Application name Localization

Application name localization is a very important issue. The picture on the left is the iPod touch desktop in the Chinese language environment, and the picture on the right is the iPod touch desktop in the English language environment. We can see that the calendar, MAP, stock market, and other application names are localized.

We want to write an application whose English name is "Localization" and whose Chinese name is "Localization ". Open the project L10N application and find L10N-Info in the project. plist file, which is a project property file. The application name is defined in this file, but we cannot localize it here. To localize it, you must use another file InfoPlist. strings, InfoPlist. strings can be localized. After the system buttons and information are localized in the previous section, InfoPlist. strings contains two files: InfoPlist. strings (English) and InfoPlist. strings (Chinese ). Open the Finder to see en. lproj and the zh-Hans.lproj. Their directory structure is as follows:

├ ── En. lproj

│ ├ ── InfoPlist. strings

│ ── MainStoryboard. storyboard

── Zh-Hans.lproj

── InfoPlist. strings

── MainStoryboard. storyboard

The key of CFBundleDisplayName and CFBundleName can be used to configure the application name, and the key of CFBundleDisplayName can be used to configure the name displayed by the application. CFBundleName: Specifies the short name of the application, which cannot exceed 16 characters. The information in the menu bar and application window is displayed.

The content of the InfoPlist. strings (Chinese) file is as follows:

CFBundleDisplayName = "localization ";

CFBundleName = "localization ";

The content of the InfoPlist. strings (English) file is as follows:

CFBundleDisplayName = "Localization ";

CFBundleName = "L10N ";

Running result. The text under the icon displays the name of the CFBundleDisplayName key configuration.

Static text Localization

Static texts in application should be localized, but they may be output through program code or through IB's design and output in nib or storyboard. The label Application created using the "Tabbed Application" Project template. The title First and Second on the two labels and the text in the screen are both static files.

The same is true for this project. If two versions based on the story board and nib technology are created during the creation process, the titles on the two tabs of the storyboard version are written in the storyboard file through IB (we will introduce the localization of static texts in storyboards and nib in the next section ). However, in the nib version, the titles on the two labels are output through program code. The constructor in FirstViewController. m is as follows:

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil

{

Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];

If (self ){

Self. title = NSLocalizedString (@ "First", @ "First ");

Self. tabBarItem. image = [UIImage imageNamed: @ "first"];

}

Return self;

}

Constructor in SecondViewController. m:

-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil

{

Self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil];

If (self ){

Self. title = NSLocalizedString (@ "Second", @ "Second ");

Self. tabBarItem. image = [UIImage imageNamed: @ "second"];

}

Return self;

}

The NSLocalizedString macro is used to set the title attribute in the two constructor methods. The NSLocalizedString macro essentially calls the localizedStringForKey: value: table: Method of NSBundle, NSLocalizedString is from the default string resource file (Localizable. strings.

The string resource file is named Localizable. strings by default, and the file is encoded in UTF-16. If there are not many static texts, you can create your own Localizable. strings file. Select the Supporting Files group, and choose File> New> File… from the menu ..., Select iOS> Resource> String File and enter the File name "Localizable. strings ".

Select Localizable. strings to open the file display checker and click "Make localized" in Localization. This will help us create a localized Localizable. strings file.

Click "Make localized" to bring up a dialog box for selecting a localized language. We can select English and then click the Localize button.

Then add the Simplified Chinese localization file according to the fact.

Add content to the Localizable. strings file in the English version:

/* First */

"First" = "First ";

/* Second */

"Second" = "Second ";

Add content to the Localizable. strings file in the Chinese version:

/* First */

"First" = "First ";

/* Second */

"Second" = "Second ";

Use

However, sometimes there are many strings, which are difficult to extract and write. At this time, we can use the command line tool genstring to scan the following macros from the m or mm file, and output the string to the local file.

CFCopyLocalizedString

CFCopyLocalizedStringFromTable

CFCopyLocalizedStringFromTableInBundle

CFCopyLocalizedStringWithDefaultValue

NSLocalizedString

NSLocalizedStringFromTable

NSLocalizedStringFromTableInBundle

NSLocalizedStringWithDefaultValue

Macros starting with CF correspond to macros starting with NS. Macros starting with NS are based on Objective-C language in the Foundation framework, and macros starting with CF are based on C language in the Core Foundation framework. The NSLocalizedStringFromTable and NSLocalizedStringFromTableInBundle functions are used to customize string resource file names.

The following is the basic syntax for genstrings naming:

Genstrings [-a] [-q] [-o <outputDir>] sourcefile

Parameters:

-A: Append content to an existing file

-Q: Warning for disabling multiple key/value pairs

-O specifies the output directory

Therefore, if we want to output data to the en. lproj directory, the Code is as follows:

Genstrings-o en. lproj *. m

In this case, en. localizable is generated under the lproj directory. in the strings file, note that the above command overwrites Localizable every time it is run. in the strings file, if the content does not want to overwrite, you can use the-a parameter and modify it in the file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.