[IOS] Localization

Source: Internet
Author: User

In IOS development applications, we will encounter a well-developed application. How can we become internationalized, that is, to support multiple languages? The following is a simple demonstration of how to use a Demo to implement both Chinese and English. Implementation steps: 1. view of xib in the localization project 1. Add several labels to the view to bind the corresponding attributes. 2. ViewController. h:

#import <UIKit/UIKit.h>    @interface ViewController : UIViewController  @property (retain, nonatomic) IBOutlet UILabel *languageee;  @property (retain, nonatomic) IBOutlet UILabel *label1;  @property (retain, nonatomic) IBOutlet UILabel *label2;    @end  

 

3. ViewController. m:
#import "ViewController.h"    @interface ViewController ()    @end    @implementation ViewController    - (void)viewDidLoad  {      [super viewDidLoad];            NSLocale * locale = [NSLocale currentLocale];      NSString * str = [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier]];      self.languageee.text = str;      self.label1.text = NSLocalizedString(@"name",nil);      self.label2.text = NSLocalizedString(@"sex", nil);  }    - (void)dealloc {      [_languageee release];      [_label1 release];      [_label2 release];      [super dealloc];  }  

 

@ End 4. create versions of xib in other languages. Specific operations: 1. click PROJECT (blue icon), click Open PROJECT icon, open Localizations, check Use Base Internationalizatino, A Basexib file is automatically created, and then click +, add other language versions. The default version is english, so we are creating a Chinese, the first tab. 2. Click xib, move the cursor to the rightmost, select the leftmost Tab Of the attribute column, open Localization, and check the corresponding options to support multilingual files. 3. click modify corresponding ViewController. data in strings (Chinese): For example:/* Class = "IBUILabel"; text = "Label"; ObjectID = "8"; */"8. text "=" Label ";/* Class =" IBUILabel "; text =" Label "; ObjectID =" 11 "; */" 11. text "=" Label ";/* Class =" IBUILabel "; text =" Label "; ObjectID =" 14 "; */" 14. text "=" Label ";/* Class =" IBUILabel "; text =" name "; ObjectID =" 20 "; */" 20. text "=" name ";/* Class =" IBUILabel "; text =" sex "; ObjectID =" 24 "; */" 24. text "=" gender ";/* Class =" IBUIButton "; highlightedTitle =" HaHa "; ObjectID =" 28 "; */" 28. highlightedTitle "=" HaHa ";/* Class =" IBUIButton "; normalTitle =" Button "; ObjectID =" 28 "; */" 28. normalTitle "=" Button ";/* Class =" IBUISegmentedControl "; 32. segmentTitles [0] = "First"; ObjectID = "32"; */"32. segmentTitles [0] "=" First ";/* Class =" IBUISegmentedControl "; 32. segmentTitles [1] = "Second"; ObjectID = "32"; */"32. segmentTitles [1] "=" Second "; the corresponding English file is also modified to the English version or later. It is the data in the static label control in the localized view. If it is dynamic, if. the data control associated with the H file is now necessary. write the following in the m file:
 self.label1.text = NSLocalizedString(@"name",nil);  self.label2.text = NSLocalizedString(@"sex", nil);  

 

In this way, the corresponding registered object is automatically generated in the configuration file automatically created later. 4. automatically generate the corresponding dynamic data file open terminal, enter cd, drag and drop the file in the terminal, automatically generate the corresponding path, and then enter genstrings. /LocateDemo /*. m will find that a Localizable is automatically generated in the project. strings file, and then in the corresponding en. lproj and zh-Hans.lproj are two corresponding language package copy a copy of this file, and then import to the project, click the corresponding file and the corresponding field/* No comment provided by engineer is automatically generated. */"name" = "James";/* No comment provided by engineer. */"sex" = "male"; change the files in the corresponding Chinese and English packages to the corresponding language 2. A LocateDemo-Info.plist file is found after the localized application name creates a SingleView project, double-click to open the file, right-click-> Show R Ow Keys/Values, copy CFBundleDisplayName to InfoPlist. in strings, the file is also localized to generate files in the two languages, and then modify CFBundleDisplayName = "English" in the files corresponding to each language; and "Chinese ", in subsequent tests, we will find that if the simulator is set in different languages, the application names corresponding to this program will be different. 3. localized images are like two languages. I want to import different national flags, American and Chinese flags based on different English and Chinese environments. Drag and Drop a ViewImage on the interface, then, drag and drop the image resource near the project, click localization, generate an image in another language environment, and modify the image in the corresponding file, however, you must ensure that the image names in the two files are the same.

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.