[IOS] how to switch the Display language in iOS development to internationalize

Source: Internet
Author: User

[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));


 

 

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.