iOS 国际化手动切换语言

1. 首先我们需要一个建一个工具类

//.h中

@interface LJMInternationalController : UIViewController

+(NSBundle *)bundle;//获取当前资源文件

+(void)initUserLanguage;//初始化语言文件

+(NSString *)userLanguage;//获取应用当前语言

+(void)setUserlanguage:(NSString *)language;//设置当前语言

@end

//.m中

static NSBundle *bundle = nil;

+ ( NSBundle * )bundle{

return bundle;

}

+(void)initUserLanguage{

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

NSString *string = [def valueForKey:@"userLanguage"];

     if(string.length == 0)

    {

         //获取系统当前语言版本(中文zh-Hans,英文en)

         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]; 

}

2.我们需要创建一个切换视图,其实只要创建两个Button就足够了,我们需要的只是在其点击事件里切换语言并且发送通知

- (void)viewDidLoad

{

    //注册通知,用于接收改变语言的通知

    [[NSNotificationCenter defaultCenter] addObserver:self          selector:@selector(changeLanguage) name:@"changeLanguage" object:nil];

     [LJMInternationalController initUserLanguage];//初始化应用语言

    NSBundle *bundle = [LJMInternationalController bundle];

     NSString *buttonInfo = [bundle localizedStringForKey:@"English" value:nil table:@"String"];//

    NSString *buttonInfo1 = [bundle localizedStringForKey:@"Chinese" value:nil table:@"String"];

    [_changeToChinese setTitle:buttonInfo1 forState:UIControlStateNormal];

    [_changeToEnglish setTitle:buttonInfo forState:UIControlStateNormal];

    [super viewDidLoad];

}

-(void)changeLanguage

{

    [_changeToChinese setTitle:[[LJMInternationalController bundle]    localizedStringForKey:@"Chinese" value:nil table:@"String"]    forState:UIControlStateNormal];

    [_changeToEnglish setTitle:[[LJMInternationalController bundle] localizedStringForKey:@"English" value:nil table:@"String"] forState:UIControlStateNormal];

}

- (IBAction)ToEnglish:(id)sender 

{

    [LJMInternationalController setUserlanguage:@"en"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:nil];

}

- (IBAction)toChinese:(id)sender

 {

    [LJMInternationalController setUserlanguage:@"zh-Hans"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:nil];

}

3.我看到有很多文章说要在每一个需要国际化的页面都要接收通知,其实还有更简单地方法,我们只需要在一个地方接收通知就好了,那就是在我们的tabbarController里面,只需要在ViewWillAppear里面接收通知就好了

-(void)viewWillAppear:(BOOL)animated

{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeLanguage) name:@"changeLanguage" object:nil];

     [super viewWillAppear:animated];

}

-(void)ChangeLanguage

{

    self.viewControllers=nil;

    [self makeTabBarItem];//重新定制tabbar

}

4.当然每个页面需要国际化的地方要做处理,这里就只举一个例子好了

//#import "LJMInternationalController.h"//引入头文件

//将所需要显示的内容设置成属性

@property(nonatomic,copy)NSString * ModeifyPassWord;

@property(nonatomic,copy)NSString * MyIntegral;

@property(nonatomic,copy)NSString * ChangeLanguage;

@property(nonatomic,copy)NSString * Exit;

//在viewDidLoad方法里初始化

- (void)viewDidLoad

 {

     [super viewDidLoad];

     [LJMInternationalController initUserLanguage];//初始化应用语言

    NSBundle *bundle = [LJMInternationalController bundle];

    _ModeifyPassWord = [bundle localizedStringForKey:@"ModeifyPassWord" value:nil table:@"String"];

    _MyIntegral = [bundle localizedStringForKey:@"MyIntegral" value:nil table:@"String"];

    _ChangeLanguage = [bundle localizedStringForKey:@"ChangeLanguage" value:nil table:@"String"];

    _Exit = [bundle localizedStringForKey:@"Exit" value:nil table:@"String"];

}

5最后完成

你可能感兴趣的:(iOS 国际化手动切换语言)