DiscussionIPhoneUsed inNSLocalizedStringImplementationInternationalizationIt is the content to be introduced in this article. Let's look at the content first.
1. Obtain the international information supported by the system
InInternationalizationBefore, you canIphoneIn "Settings-> General-> multi-language environment-> language" to view yourIphoneWhich languages are supported? You can also write a piece of code to test yourIphoneLanguages are supported. The test code is as follows:
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
- NSLog(@"%@", languages);
Note: The NSUserDefaults class is used to obtain the default user information.
2. Create multi-language documents in Xcode
1. Create a document under the Resources category (right pane/Add/New File ...)
2. Select Other in the template dialog box, and then select Strings File
3. Set the file storage name to Localizable. strings.
4. Right-click the Localizable. strings file and select Get Info.
5. Click Make File Localizable on the information interface, and then switch the Tab to General.
6. Enter the new language name zh and press Add. In some cases, there are two languages: English and zh. You can also Add other languages.
3. Use NSLocalizedString in source code to reference international files
// The first parameter in the brackets is the content to be displayed, which corresponds to the IDs in each Localizable. strings.
// The second parameter is a comment on the first parameter, which can be a null string.
- [_alertView setTitle:NSLocalizedString(@"Submitted successfully",@"")];
4. Use the Terminal genstrings command to generate a resource file
Open Terminal, cd to the directory where the project is located, and use genstrings to generate the source file automatically generated from the source code.
For example, if the project directory is/user/project/test01, the command is as follows:
- genstrings -o English.lproj ./classes/*.m
- genstrings -o zh.lproj ./classes/*.m
5. Edit the Localizable. strings files.
In step 4, we get the resource file corresponding to the code. Finally, we need to translate these resource files into the corresponding language. for example, in Localizable. in strings (zh), compile the text after the equal sign into Chinese.
- "Submitted successfully" = "Submitted successfully"
After the project is re-compiled, the corresponding language is displayed in different language environments.
Summary:IPhoneUsed inNSLocalizedStringImplementationInternationalizationI hope this article will help you!