iOS Multi-language (internationalization) development (trailing system + in-app manual setup)

Source: Internet
Author: User

One: Trailing system switching language1> after you have created a project, create a new multilingual file:

2> Join the language type you want to set:

3> Join success

A careful friend may send EnglishIt says in the back 3 Files Localized, this is because English is added by default, and at the same time Main.storyboardAnd LaunchScreen.storyboardSet up multiple languages, I did not check these two items when I joined .3> use multilingual files to set the appropriate language within the app
"label"="label";"label"="标签";"label"="ラベル";
4> Assigning a value to a control in a class NSLocalizedStringUse, there is a necessary condition, that is: The multilingual file name Word must be Localizable, otherwise the last display can only be key, the user can only let the application of the trailing system language version number to switch, and users can not manually switch
NSLocalizedString("label"nil)
Related macro Description
//Trailing system switch, multilingual file name must be localizableNslocalizedstring ("Label",Nil);each of the following three can be manually set to multi-lingual//First parameter: is a key in multiple languages//second parameter: is the name of the multilingual file///Third parameter: is the gaze description of key, General pass nilNslocalizedstringfromtable (@"Label", @"Stlocalizable",Nil);//Specify Multilingual file name and bundleNslocalizedstringfromtableinbundle (@"Label", @"Stlocalizable", Bundles,Nil);//Add a reference to the default value on the above basisNslocalizedstringwithdefaultvalue (@"Label", @"Stlocalizable", Bundle, @"Label",Nil);
can also use NSLocalizedStringFromTable
NSLocalizedStringFromTable(@"label", @"STLocalizable"nil);
After switching the system language, the language within the app is switched after the app is launched. Second: Manually switch languages within the app1> Project Show in Finder, it is found that each language has a corresponding directory, and the suffix is .lproj,

When you switch languages within the app, you're actually fetching the contents of the STLocalizable.stringsFile

2> get STLocalizable.stringsGets the file's valueIf you want to switch languages, switch directly pathForResourceThe following number of references can be
NSString *path = [[NSBundle mainBundle] pathForResource:@"zh-Hans" ofType:@"lproj"];NSString *labelString = [[NSBundle bundleWithPath:path] localizedStringForKey:@"label" value:nil table:@"STLocalizable"];NSLog(@"result: %@", labelString);//输出:MultiLanguage[1887:72599] result: 标签
3> switching languages within the app, using NSUserDefaultsStore the current language
- (ibaction) Changelanguage: (UIButton*) Sender {Switch(Sender. Tag) { Case Ten: {//Chinese[[NsuserdefaultsStandarduserdefaults] setobject:@"Zh-hans"Forkey:applanguage]; } Break; Case  One: {//Japanese[[NsuserdefaultsStandarduserdefaults] setobject:@"Ja"Forkey:applanguage]; } Break; Case  A: {//English[[NsuserdefaultsStandarduserdefaults] setobject:@"en"Forkey:applanguage]; } Break;default: Break; }}
4> program exits, the next time you go to continue using the language of the last shutdown program
//According to Nsuserdefaults key to get multi-language typeNSString*laguagetype =[NSStringstringwithformat:@"%@", [[NsuserdefaultsStandarduserdefaults] objectforkey:@"Applanguage"]];//Identify the appropriate path for the multilingual type in the file directoryNSString*path = [[NSBundleMainbundle] Pathforresource:laguagetype oftype:@"Lproj"];//Find the appropriate value for the label in a multilingual fileNSString*labeltext = [[NSBundleBundlewithpath:path] localizedstringforkey:@"Label"ValueNiltable:@"Aslocalized"]; Self. Label. Text= LabelText;
ExtendedWhat language is displayed when the user launches the app for the first time 1> default setting a language, for example, in English, APPDelegateOf didFinishLaunchingWithOptionsIn
if (![[NSUserDefaults standardUserDefaults] objectForKey:appLanguage]) {     //默认设为英文    [[NSUserDefaults standardUserDefaults] setObject:@"en"  forKey:appLanguage];}
2> trailing system language needs to get the preferred language order of the system, then take the first preferred language
//获取第一个首选语言NSString *language = [NSLocale preferredLanguages][0];
Or infer whether a good language version number exists in the preferred language array
if(! [[Nsuserdefaults Standarduserdefaults] objectforkey:applanguage])    {Nsarray *languages = [Nslocale preferredlanguages]; NSString *language = [Languages Objectatindex:0];if([Language hasprefix:@"Zh-hans"]) {[[Nsuserdefaults standarduserdefaults]Setobject:@"Zh-hans"  forKey:applanguage]; }Else if([Language hasprefix:@"Ja"]) {[[Nsuserdefaults standarduserdefaults]Setobject:@"Ja"  forKey:applanguage]; }Else{[[Nsuserdefaults standarduserdefaults]Setobject:@"en"  forKey:applanguage]; }}
Change the system default development language (default English) possible ways to develop multiple languages
#define Languagekey @ "Applanguage"//Get current language[[NsuserdefaultsStandarduserdefaults] Objectforkey:languagekey];//Get Preferred Language order[Nslocale Preferredlanguages];//Get Preferred language first language[[Nslocale preferredlanguages] Objectatindex:0];//Get system All languages[Nslocale Availablelocaleidentifiers];//Get the current language file pathNSString*currentlanguage = [[NsuserdefaultsStandarduserdefaults]; Objectforkey:languagekey];NSString*path = [[NSBundleMainbundle] Pathforresource:currentlanguage oftype:@"Lproj"];get the local string corresponding to the current language based on key (parameter value: If key is nil or key cannot find a localized string in the table, the value returned.) )NSString*value = [[NSBundleBundlewithpath:path] localizedstringforkey:@"Label"ValueNiltable:@"Tilocalizable"];

iOS Multi-language (internationalization) development (trailing system + in-app manual setup)

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.