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