IPhoneAbout Developing applicationsInternationalizationThe implementation of the case is the content to be introduced in this article.IPhoneHow can IInternationalization? Is it necessary to write a source code in English and a source code in Chinese for the same project? Actually, this is not necessary. We can useIPhoneMulti-language support for local and international projects.
1. Create a Windows-base Application. Open the main. m file and add the following code to the main function:
- // Obtain the default user information
- NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
- // Obtain all language settings supported by the iPhone
- NSArray * ages = [defaults objectForKey: @ "AppleLanguages"];
- NSLog (@ "% @", ages );
Run the program and output the following content from the console:
- "zh-Hans",
- en,
- fr,
- de,
- "zh-Hant",
- ……
You can see that all languages supported on the iPhone are English, French, and German, and there are two simplified/Traditional Chinese languages starting with "zh ). We know that Chinese characters start with "zh.
2. Right-click Resources, Add a New File, select Resource> Strings File for the File type, and the File name is Localizable. strings. Use the getInfo command on the File and click Make File Localizable in the following interface,
Then, you can go to the Finder to view the project directory. You will find that there is an additional English. lproj directory, and there is a Localizable. strings file under it, but the content is blank ).
Open the getInfo window again and the following page is displayed:
We can see that there is only one language in the Localizations list. We need to click the Add Localization button to Add the Chinese zh_CN,
In this case, you can go to the Finder to check the project directory and find that there is another directory named zh_CN.lproj, and there is also a Localizable. strings file with empty content ).
At the same time, there are two more Localizabel. strings files of different language versions in the Copy Bundle Resources directory under Target, one English and one zh_CN ),
3. Open MainWindow. xib and drag a UILable to the window.
4. Open the AppDelegate. h file, declare the UILable variable in it, and add an IBOutlet:
- UILabel * label ;
- ……
- @property ( nonatomic , retain ) IBOutlet UILabel* label;
5. Open the AppDelegate. m file,
- @synthesize window,label;
Add the code in (BOOL) applicationv: application didfinishlaunchingwitexceptionsv: method:
- // Use the NSLocalizedString function to obtain the internationalized String Based on the iPhone's multilingual settings
- Label. text = NSLocalizedString (@ "Localizable Test ",@"");
6. Return to IB and connect the UILable control with the IBOutlet variable label,
7. Open the terminal program, enter the project directory, and run the following command:
- genstrings -o English.lproj ./classes/*.m
You will find a line of code added to the Localizable. strings file under the English. lproj directory:
- "Localizable Test" = "Localizable Test" ;
Run the following command:
- genstrings -o zh_CN.lproj ./classes/*.m
Similarly, a line of code will be added to the Localizable. strings file under the zh_CN.lproj directory:
- "Localizable Test" = "Localizable Test" ;
Double-click Localizable. strings under the zh_CN.lproj directory.
- "Localizable Test" = "localization Test ";
In this way, whenever the source code changes, we need to use the genstrings command to re-generate the Localizable. strings file. However, after each re-generation, the original edited translation results are lost, and we must re-translate the results. This is a big headache. We recommend that you back up the files before each genstrings operation to reduce repetitive work.
8. Run the program. The effect is as follows)
When you change the multilingual settings to English, the text in the label is displayed as English.
Summary:IPhoneAbout Developing applicationsInternationalizationThe implementation of the case is complete. I hope this article will help you!