Ios application multi-language free switch implementation

Source: Internet
Author: User

Ios application multi-language free 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;

}


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.