[IOS] how to switch the Display language in iOS development to internationalize
1. Add Chinese and English languages in Project Settings:
2. Create a Localizable. strings file to store multiple languages as a dictionary corresponding to multiple languages. Click Localization on the right and check the Chinese and English letters:
3. Add a field,
In English, add: SUBMIT_BTN_TITLE = Go;
In Chinese, add: SUBMIT_BTN_TITLE = start;
4. A tool class GDLocalizableController is used to switch the local language:
/// GDLocalizableController. h // guide-book /// Created by why on 7/16/14. // copy right (c) 2014 why. all rights reserved. // # import
@ Interface GDLocalizableController: NSObject + (NSBundle *) bundle; // obtain the current resource file + (void) initUserLanguage; // The initialization language file + (NSString *) userLanguage; // obtain the current language of the application + (void) setUserlanguage :( NSString *) language; // set the current language @ end /// GDLocalizableController. m // guide-book /// Created by why on 7/16/14. // copy right (c) 2014 why. all rights reserved. // # import GDLocalizableController. h @ implementation GDLocalizableControllerstatic NSBundle * bundle = nil; + (NSBundle *) bundle {return bundle;} + (void) initUserLanguage {NSUserDefaults * def = [NSUserDefaults standardUserDefaults] NSString * string = [def valueForKey: @ userLanguage]; if (string. length = 0) {// obtain the current language version of the system. NSArray * ages = [def objectForKey: @ AppleLanguages]; NSString * current = [ages objectAtIndex: 0]; string = current; [def setValue: current forKey: @ userLanguage]; [def synchronize]; // persistence, if this parameter is not added, the file will not be saved.} // obtain the file path NSString * path = [[NSBundle mainBundle] pathForResource: string ofType: @ lproj]; bundle = [NSBundle bundleWithPath: path]; // generate bundle} + (NSString *) userLanguage {NSUserDefaults * def = [NSUserDefaults standardUserDefaults]; NSString * language = [def valueForKey: @ userLanguage]; return language ;} + (void) setUserlanguage :( NSString *) language {NSUserDefaults * def = [NSUserDefaults standardUserDefaults]; // 1. step 1: Change the bundle value NSString * path = [[NSBundle mainBundle] pathForResource: language ofType: @ lproj]; bundle = [NSBundle bundleWithPath: path]; // 2. persistence [def setValue: language forKey: @ userLanguage]; [def synchronize];} @ end
5. Customize a macro for processing:
// ----- Multilingual settings # define CHINESE @ zh-Hans # define ENGLISH @ en # define GDLocalizedString (key) [[GDLocalizableController bundle] localizedStringForKey :( key) value: @ table: nil]
6. Use:
[GDLocalizableController setUserlanguage:CHINESE]; NSLog(GDLocalizedString(@SUBMIT_BTN_TITLE)); [GDLocalizableController setUserlanguage:ENGLISH]; NSLog(GDLocalizedString(@SUBMIT_BTN_TITLE));