iOS app multi-language free switch implementation

Source: Internet
Author: User

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

All the controllers in the language to be replaced are notified. Notifies when a language is modified after the user has selected one. After the controller receives the language modification notification, the control that needs to modify the language is reset.

/** 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. If the software language includes the system language default to the system language , if you do not forgive the default setting to 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 change , reload

-(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 a string in the corresponding directory

+ (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:nil table:(table)];

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

return localizedstring;

}


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.