How can I internationalize an iPhone project? 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 use the iPhone multi-language support to implement local (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 ",
......
We can see that all languages supported on the iPhone are English, French, and German, and there are two languages starting with "zh" (simplified/traditional ). 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 an additional English. lproj directory with a Localizable. strings file under it (but the content is empty ).
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 another directory named zh_CN.lproj. There is also a Localizable. strings file under it (the content is empty ).
At the same time, there are two more Localizabel. strings files (one English and one zh_CN) in the Copy Bundle Resources directory under Target ):
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 ";
Open Localizable. strings in the zh_CN.lproj directory (double-click) and change it
"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 editing (translation) results are lost, and we must re-translate them. 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 (if the multi-language of the simulator is set to simplified Chinese ):
When you change the multilingual settings to English, the text in the label is displayed as English.