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