Introduction to NSLocalizedString for internationalization in iPhone

Source: Internet
Author: User

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:

 
 
  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.

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.

 
 
  1. [_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:

 
 
  1. genstrings -o English.lproj  ./classes/*.m  
  2. 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.

 
 
  1. "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!

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.