今天项目中添加了一个需求,App设置中有个语言切换,需要实现的效果是中英繁切换,针对APP国际化进行操作,使APP内部控件显示语言可以不随系统语言做改变,自由切换:效果简约如下:
核心方法:
NSLocalizedStringFromTable(<#key#>,<#tbl#>,<#comment#>)
原理即在工程内部建立 .String,在内部采用键值对方式对所需要切换语言的文字进行赋值
1.工程内部创建.String文件,命名为Language.String
2.右侧菜单栏点击Localize,弹出菜单选择English,确定
3.第二步结束后选择工程,选择PROJECT,下方Localizations菜单中点击+号选择添加中文
4.此时Language.String中会显示两个文件,Language.String(Chinese) 和 Language.String(English),这一步像这两个文件中添加键值对
英文文件中:
"denglu" = "Login";
"account" = "Phone number/email address";
"password" = "Password";
"verification" = "Verification code";
"message" = "SMS verification code";
"forget" = "Forget?";
"newregist" = "Registration";
"question" = "Problems";
中文文件中:
"denglu" = "登录";
"account" = "请输入手机号/邮箱";
"password" = "请输入登录密码";
"verification" = "请输入验证码";
"message" = "通过短信验证码登录";
"forget" = "忘记密码?";
"newregist" = "新用户注册";
"question" = "常见问题";
5.新建一个单例类,ChangeLanguage,.h中添加属性方法
#import <Foundation/Foundation.h>
@interface ChangeLanguage:NSObject
#define FGGetStringWithKeyFromTable(key, tbl) [[ChangeLanguage sharedInstance] getStringForKey:key withTable:tbl]
@property(nonatomic,strong)NSString * language;
+(id)sharedInstance;
/**
* 返回table中指定的key的值
*
* @param key key
* @param table table
*
* @return 返回table中指定的key的值
*/
-(NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table;
/**
* 改变当前语言
*/
-(void)changeNowLanguage;
/**
* 设置新的语言
*
* @param language 新语言
*/
-(void)setNewLanguage:(NSString *)language;
@end
6.在.m中实现方法:
#import"ChangeLanguage.h"
#import"AppDelegate.h"
#define CNS @"zh-Hans"
#define EN @"en"
#define LANGUAGE_SET @"langeuageset"
@interface ChangeLanguage()
@property(nonatomic,strong)NSBundle * bundle;
@end
@implementation ChangeLanguage
static ChangeLanguage * sharedModel;
+(id)sharedInstance{
if(!sharedModel){
sharedModel=[[ChangeLanguage alloc]init];
}
return sharedModel;
}
-(instancetype)init{
self=[super init];
if(self){
[self initLanguage];
}
return self;
}
-(void)initLanguage{
NSString*tmp=[[NSUserDefaults standardUserDefaults]objectForKey:LANGUAGE_SET];
NSString*path;
//默认是中文
if([tmp isEqualToString:CNS]||!tmp){
tmp=CNS;
}else{
tmp=EN;
}
self.language=tmp;//更改之前语言
path=[[NSBundle mainBundle]pathForResource:self.language ofType:@"lproj"];
self.bundle=[NSBundle bundleWithPath:path];
}
-(NSString*)getStringForKey:(NSString*)key withTable:(NSString*)table{
if(self.bundle){
return NSLocalizedStringFromTableInBundle(key,table,self.bundle,@"");
}
return NSLocalizedStringFromTable(key,table,@"");
}
-(void)changeNowLanguage{
if([self.language isEqualToString:EN]){
[self setNewLanguage:CNS];
}else{
[self setNewLanguage:EN];
}
}
-(void)setNewLanguage:(NSString*)language{
if([language isEqualToString:self.language]){
return;
}
//找到需要改成的语言路径
if([language isEqualToString:EN]||[language isEqualToString:CNS]){
NSString*path=[[NSBundle mainBundle]pathForResource:language ofType:@"lproj"];
self.bundle=[NSBundle bundleWithPath:path];
}
self.language=language;NSLog(@"%@",language);
[[NSUserDefaults standardUserDefaults]setObject:language forKey:LANGUAGE_SET];
[[NSUserDefaults standardUserDefaults]synchronize];
[self resetRootViewController];
}
//重新设置
-(void)resetRootViewController{
AppDelegate * appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
UIStoryboard * SB=[UIStoryboard storyboardWithName:@"Main"bundle:nil];
UITabBarController * tabBarController=[SB
instantiateViewControllerWithIdentifier:@"tabBarController"];
tabBarController=(UITabBarController *)appDelegate.window.rootViewController;
}
@end
7.具体使用:在具体方法中先调用changeNowLanguage方法,然后用FGGetStringWithKeyFromTable赋值即可;