NSLocalizedString is used in iPhone applications for internationalization

Source: Internet
Author: User

IPhoneUsed in applicationsNSLocalizedStringImplementationInternationalizationIt is the content to be introduced in this article. If you do not need to talk about it, go directly to the topic. ObtainInternationalizationInformation inInternationalizationPreviously, you could choose "Settings"> "general"> "multiple" in the iphone.LanguageEnvironment->Language"To see what your iphone supportsLanguage,You can also write a code to test what your iphone supports.LanguageThe test code is as follows:

 
 
  1. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];   
  2. NSArray *languages = [defaults objectForKey:@"AppleLanguages"];   
  3. NSLog(@"%@", languages);  

Note: The NSUserDefaults class is used to obtain the default user information.

1. Create a multi-language document 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.

2. Use NSLocalizedString in the source code to reference international files

 
 
  1. // The first parameter in the brackets is the content to be displayed, which corresponds to the IDs in each Localizable. strings.
  2. // The second parameter is a comment on the first parameter, which can be a null string.
  3. [_ AlertView setTitle: NSLocalizedString (@ "Submitted successfully", @ "")];

3. 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/*. mgenstrings-o zh. lproj./classes/*. m

4. 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 entire project is re-compiledLanguageEnvironmentLanguageDisplay.

Summary:IPhoneUsed in applicationsNSLocalizedStringImplementationInternationalizationThis article is helpful to you!

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.