iOS Multi-language (internationalization) development (follow the system + in-app manual settings)

Source: Internet
Author: User

One: Follow the system to switch languages1> After creating a project, create a new multilingual file:

2> Add the type of language you want to set:

3> Add success

Attentive friends may find in EnglishIt says in the back 3 Files Localized, because English is added by default and is Main.storyboardAnd LaunchScreen.storyboardI have set up multiple languages, I have not checked these two items when I add them .3> use multilingual files to set the corresponding language within the app
"label"="label";"label"="标签";"label"="ラベル";
4> Assigning a value to a control in a class NSLocalizedStringThe use of, there is a necessary condition, that is: The multilingual file name must be Localizable, otherwise the last display can only be key, the user can only be set to follow the language version of the system to switch, and users can not manually switch
NSLocalizedString("label"nil)
Related macro Description
//Follow system switch, multilingual filename must be localizableNslocalizedstring ("Label",Nil);///below three can be manually set multi-lingual///First parameter: is a key in multiple languages///second parameter: is the name of the multilingual file///Third parameter: is the note description of key, General pass nilNslocalizedstringfromtable (@"Label", @"Stlocalizable",Nil);//Specify Multilingual file name and bundleNslocalizedstringfromtableinbundle (@"Label", @"Stlocalizable", Bundles,Nil);//Add a default value to the above based on the parameterNslocalizedstringwithdefaultvalue (@"Label", @"Stlocalizable", Bundle, @"Label",Nil);
You 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 Engineering Show in Finder, you find that each language corresponds to a folder, and the suffix is .lproj,

When you switch languages within the app, you're actually taking the STLocalizable.stringsFile

2> get STLocalizable.stringsGets the file's valueIf you want to switch languages, switch directly pathForResourceThe following parameters 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 the multi-language typeNSString*laguagetype =[NSStringstringwithformat:@"%@", [[NsuserdefaultsStandarduserdefaults] objectforkey:@"Applanguage"]];//Verify the path of the multilingual type in the file directoryNSString*path = [[NSBundleMainbundle] Pathforresource:laguagetype oftype:@"Lproj"];//Find the value of a 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 of a language, such as English, in APPDelegateOf didFinishLaunchingWithOptionsIn
if (![[NSUserDefaults standardUserDefaults] objectForKey:appLanguage]) {     //默认设为英文    [[NSUserDefaults standardUserDefaults] setObject:@"en"  forKey:appLanguage];}
2> follows the system language needs to obtain the system's preferred language order, then takes the first preferred language
//获取第一个首选语言NSString *language = [NSLocale preferredLanguages][0];
Or to determine whether a good language version 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]; }}
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 all languages of the system[Nslocale Availablelocaleidentifiers];//Get the current language file pathNSString*currentlanguage = [[NsuserdefaultsStandarduserdefaults]; Objectforkey:languagekey];NSString*path = [[NSBundleMainbundle] Pathforresource:currentlanguage oftype:@"Lproj"];gets the local string corresponding to the current language based on key (parameter value: The value returned if key is nil or key cannot find a localized string in the table.) )NSString*value = [[NSBundleBundlewithpath:path] localizedstringforkey:@"Label"ValueNiltable:@"Tilocalizable"];

iOS Multi-language (internationalization) development (follow the system + in-app manual settings)

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.