iOS app multi-language free switch implementation

Source: Internet
Author: User

Get the native language for the first time into the app. 1. Assume that the native language is in the Software implementation Language list. The native language is selected by default, but the user is still able to select a different language. 2. If the native language is not in the list of software implementation languages, English is selected by default. The user can still select a different language.

All the controllers in the language to be replaced. Notification of language changes sent when a user selects a language. After the controller receives the language change notification, the control that needs to change the language is set again.

/** Initialization Language */

-(void) initlanguage{

//1. get system-supported languages

_languages = [nsmutablearrayarraywithcapacity:5 ];

    nsurl * URL = [[ nsbundle Mainbundle] urlforresource: Klanguageplist withextension: ktypeplist ];

nsdictionary *dic = [nsdictionarydictionarywithcontentsofurl: URL ];


//2 Generating a Language object

[DiCenumeratekeysandobjectsusingblock: ^ (ID key,ID obj,BOOL *stop) {

dycxlanguage *language = [dycxlanguagelanguagewithkey: Keyandvalue: obj];

[_languagesaddobject: language];

}];

//3. get the user-saved language [0] = Simplified Chinese , [1] = Zh-hans;

nsstring *userlanguagevalue = [selfgetuserlanguage];

if (!userlanguagevalue) {

Userlanguagevalue =Kdefaultuserlanguage;

//3.1 getting the system language

nsstring * locallanguage = [selfgetlocallanguagevalue];

//3.2. determining the default language

for (dycxlanguage *languagein_languages) {

//3.3. Suppose the software language includes the system language , which assumes that the default setting is English

if ([Locallanguageisequaltostring: Language. Languagevalue]) {

Userlanguagevalue = Locallanguage;

}

}

}

//4. set the current language

for (dycxlanguage *languagein _languages) {

if ([Userlanguagevalueisequaltostring: Language. Languagevalue]) {

Language. used =YES;

_userlanguage = language;

break;

}

}

}


-(nsstring *) getlocallanguagevalue{

nsuserdefaults *defaults = [nsuserdefaultsstandarduserdefaults];

nsarray *languages = [Defaultsobjectforkey:@ "Applelanguages"];

return [Languagesobjectatindex:0];

}



#pragma mark- language changes , again loading

-(void) reloadstringwithlanguage{

    [ _confirm settitle:[ NSString localizedwithstring : @ "Dialog_positive_key" ] forstate: Uicontrolstatenormal];

_selectlanguage. text = [nsstringlocalizedwithstring:@ "Select_language_key"];

if ([self. Delegate respondstoselector:@selector(onlanguagechange)]) {

[self. Delegate Onlanguagechange];

}

}


#pragma mark- nsstring, loading the string in the corresponding folder

+ (nsstring *) localizedwithstring: (nsstring *) key{

nsstring *area = [dycxconfigshareddycxconfig]. Userlanguage. Languagevalue;

if (!area) {

Area = kdefaultuserlanguage;

}

nsstring *table = [nsstringstringwithformat:@ "%@.lproj/localizable", area];

nsstring *localizedstring = [[nsbundlemainbundle]localizedstringforkey:(key) value:niltable:(table)];

nsassert(localizedstring,@ "localized error, table =%@, key =%@", table, key);

return localizedstring;

}



To obtain the display name of internationalized information in a localized manner

NSLocale *curLocal = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"] ;NSLog(@"%@",[curLocal displayNameForKey:NSLocaleIdentifier value:@"fr_FR"] );// 法文(法国)curLocal = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hant"] ;NSLog(@"%@",[curLocal displayNameForKey:NSLocaleIdentifier value:@"fr_FR"] );//法文(法國)

iOS app multi-language free switch implementation

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.