Ios application multi-language free switch implementation, ios language switch implementation
The first time you enter the application to obtain the local language. 1. if the local language is in the software implementation language list. the local language is selected by default, but you can select other languages. 2. if the local language is not in the software implementation language list, English is selected by default. you can select other languages.
Register the notification in all the controllers that want to change the language. When the user selects a language and sends a language change notification, the controller receives the language change notification and re-sets the control for the language to be modified.
/** Initialization language */
-(Void) initLanguage {
// 1. Obtain the language supported by the system
_ Ages = [NSMutableArrayarrayWithCapacity: 5];
NSURL * url = [[NSBundlemainBundle] URLForResource: kLanguagePlistwithExtension: kTypePlist];
NSDictionary * dic = [NSDictionarydictionaryWithContentsOfURL: url];
// 2 generate a language object
[DicenumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL * stop ){
DYCXLanguage * language = [dycxw.agelanguagewithkey: keyAndValue: obj];
[_ LanguagesaddObject: language];
}];
// 3. Obtain the language [0] = Simplified Chinese, [1] = zh-Hans;
NSString * userLanguageValue = [selfgetUserLanguage];
If (! User‑agevalue ){
UserLanguageValue = kDefaultUserLanguage;
// 3.1 obtain the System Language
NSString * localLanguage = [selfgetLocalLanguageValue];
// 3.2. Determine the default language
For (DYCXLanguage * languagein_languages ){
// If the software language contains the system language by default, the default value is English.
If ([locallanguageis+tostring: language. languageValue]) {
User‑agevalue = localLanguage;
}
}
}
// 4. Set the current language
For (DYCXLanguage * languagein_languages ){
If ([user‑agevalueis‑tostring: 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 {
[_ ConfirmsetTitle: [NSStringlocalizedWithString: @ "dialog_positive_key"] forState: UIControlStateNormal];
_ SelectLanguage. text = [NSStringlocalizedWithString: @ "select_language_key"];
If ([self. delegaterespondsToSelector: @ selector (onLanguageChange)]) {
[Self. delegateonLanguageChange];
}
}
# Pragma mark-NSString classification, load the string in the corresponding directory
+ (NSString *) localizedWithString :( NSString *) key {
NSString * area = [DYCXConfigsharedDYCXConfig]. userLanguage. languageValue;
If (! Area ){
Area = kdefauseruserlanguage;
}
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;
}