Today I learned the complete process of language localization and wrote this article to remember.
first, create the. strings file, and localize it.
1. Create a. strings file by creating a new project.
2. Localization
3. Add a supported language
Select Projects under Project->localization, click + Add. Here you add the. strings file for Simplified Chinese.
With three files checked, a file named Language.strings (Chinese (Simplified)) is created in the project.
A folder named Zh-hans.lproj is created under the project directory in the Finder.
The same way you can create english.strings.
second, the use of language localization
1. Add a key-value pair in the Language.strings (Chinese (Simplified)) file and the Language.strings (中文版) file.
2. How to use localization in your code.
Build the UI and add Uilabel on storyboard to display the text displayed in different system languages.
Appdelegate.m
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions { 1. Get the system current language nsarray *languages = [Nslocale preferredlanguages]; NSLog (@ "languages==%@", languages); NSString *currentlan = [languages firstobject]; Nsrange range = [Currentlan rangeofstring:@ "-C"]; NSString *string = [Currentlan substringToIndex:range.location]; [[Nsuserdefaults standarduserdefaults]setobject:string forkey:@ "language"];}
Viewcontroller.m
@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet UILabel*Lab;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //1. Get the current languageNSString *name = [[Nsuserdefaults standarduserdefaults] Objectforkey:@"language"]; //2. Get the files in the current languageNSString *pathstr = [[NSBundle mainbundle]pathforresource:name OfType:@"lproj"]; //3. Convert to the current language based on key. Key name in the. strings File Table: file name for. StringsNSString *valuestr = [[NSBundle bundlewithpath:pathstr] Localizedstringforkey:@"Key"Value:nil table:@"Language"]; Self. Lab.text= Valuestr;
}
It is recommended that the code in the VIEWCONTROLLER.M file be written as a global macro definition because it can be used in many places. Macros are defined and used as follows:
#define Localize (key) [[NSBundle Bundlewithpath:[[nsbundle Mainbundle] Pathforresource:[[nsuserdefaults Standarduserdefaults] objectforkey:@ "language"] oftype:@ "lproj"] Localizedstringforkey: (key) Value:nil table:@ "Language"]; @interface Viewcontroller () @property (weak, nonatomic) Iboutlet UILabel *lab; @end @implementation Viewcontroller -(void ) viewdidload {[Super viewdidload]; NSString *value = Localize (@ " key " ); Self. Lab.text = value;}
3. The language of the mobile phone system is set to Simplified Chinese and English respectively.
Note:
When you get the system language
Nsarray *languages = [Nslocale preferredlanguages];
NSLog (@ "languages==%@", languages); NSString *currentlan = [languages firstobject];
Languages array
Printing results:
The content here is corresponding to the Preferend languaged in the language and region pages, general-----on the phone, settings.
IOS supports language localization in Chinese and English