Internationalization of the switch display language in IOS development Xcode

Source: Internet
Author: User

1. In project settings, add Chinese and English to two languages:

2. New Localizable.strings file, as a multilingual dictionary, store multiple languages, click on the right localization, check the English:

3. Add a field,

In 中文版, add: Submit_btn_title = Go;

In Chinese, add: submit_btn_title = start;

4. A tool class Gdlocalizablecontroller, used to switch local languages:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283 ////  GDLocalizableController.h//  guide-book////  Created by why on 7/16/14.//  Copyright (c) 2014 why. All rights reserved.//#import <foundation foundation.h="">@interface GDLocalizableController : NSObject+(NSBundle *)bundle;//获取当前资源文件+(void)initUserLanguage;//初始化语言文件+(NSString *)userLanguage;//获取应用当前语言+(void)setUserlanguage:(NSString *)language;//设置当前语言@end////  GDLocalizableController.m//  guide-book////  Created by why on 7/16/14.//  Copyright (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){        //获取系统当前语言版本        NSArray* languages = [def objectForKey:@AppleLanguages];        NSString *current = [languages objectAtIndex:0];        string = current;        [def setValue:current forKey:@userLanguage];        [def synchronize];//持久化,不加的话不会保存    }        //获取文件路径    NSString *path = [[NSBundle mainBundle] pathForResource:string ofType:@lproj];    bundle = [NSBundle bundleWithPath:path];//生成bundle}+(NSString *)userLanguage{        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];    NSString *language = [def valueForKey:@userLanguage];    return language;}+(void)setUserlanguage:(NSString *)language{        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];        //1.第一步改变bundle的值    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@lproj ];    bundle = [NSBundle bundleWithPath:path];        //2.持久化    [def setValue:language forKey:@userLanguage];    [def synchronize];}@end</foundation>



5. Customize a macro for easy handling:

1234 // ----- 多语言设置#define CHINESE @zh-Hans#define ENGLISH @en#define GDLocalizedString(key) [[GDLocalizableController bundle] localizedStringForKey:(key) value:@ table:nil]


6. Use:

1234 [GDLocalizableController setUserlanguage:CHINESE];NSLog(GDLocalizedString(@SUBMIT_BTN_TITLE));[GDLocalizableController setUserlanguage:ENGLISH];NSLog(GDLocalizedString(@SUBMIT_BTN_TITLE));

 

Internationalization of the switch display language in IOS development Xcode

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.