iOS多语言以及在应用中设置语言

首先怎么创建多语言就不说了 创建多个语言文件 根据key找到对应的value以对应不同的语言。

说一下怎么在应用中设置

首先创建一个工具类 用于管理应用中的语言

在单例方法中

+(instancetype)sharedTool{

staticdispatch_once_tonceToken;

dispatch_once(&onceToken, ^{

sharedTool= [[FBLanguageToolalloc]init];

NSArray*array = [selfgetFontsArray];

sharedTool.fontsArray= array;

NSUserDefaults*def = [NSUserDefaultsstandardUserDefaults];

NSString*string = [defvalueForKey:userLanguage];

if(string.length==0){

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

NSArray* languages = [defobjectForKey:@"AppleLanguages"];

NSString*current = [languagesobjectAtIndex:0];

sharedTool.current= current;

//确定是否能在预置的语言中找到本机的语言如未找到则用默认语言

if(![selfcanChangeCurrent:current]) {

//设置默认语言是英语

sharedTool.current=@"en";

}

string =sharedTool.current;

[defsetValue:currentforKey:userLanguage];

[defsynchronize];

}

//获取文件路径

NSString*path = [[NSBundlemainBundle]pathForResource:stringofType:@"lproj"];

sharedTool.bundle= [NSBundlebundleWithPath:path];//生成bundle

});

returnsharedTool;

}


其中getFontsArray方法用于获取预置的几种语言

+(NSArray*)getFontsArray{

FBLanguageObject*zhhans = [[FBLanguageObjectalloc]initWithTitle:@"简体中文"language:@"zh-Hans"];

FBLanguageObject*en = [[FBLanguageObjectalloc]initWithTitle:@"english"language:@"en"];

FBLanguageObject*ko = [[FBLanguageObjectalloc]initWithTitle:@"한국어"language:@"ko"];

FBLanguageObject*idl = [[FBLanguageObjectalloc]initWithTitle:@"Indonesia"language:@"la"];

FBLanguageObject*jan = [[FBLanguageObjectalloc]initWithTitle:@"日本語"language:@"ja"];

FBLanguageObject*th = [[FBLanguageObjectalloc]initWithTitle:@"ภาษาไทย"language:@"th"];

FBLanguageObject*ru = [[FBLanguageObjectalloc]initWithTitle:@"русский"language:@"ru"];

NSArray*array = [NSArrayarrayWithObjects:zhhans,en,jan,idl,ko,th,ru,nil];

returnarray;

}

canChangeCurrent用于判断当前机器所在环境是否包含在预置的语言中,如果不存在则选择默认语言。

+(BOOL)canChangeCurrent:(NSString*)current{

for(FBLanguageObject*objectinsharedTool.fontsArray) {

if([currenthasPrefix:object.language]) {

sharedTool.current= object.language;

returnYES;

}

}

returnNO;

}

languageWithLocalTitle用于通过key查找对应环境下的value

-(NSString*)languageWithLocalTitle:(NSString*)localTitle{

return[self.bundlelocalizedStringForKey:localTitlevalue:niltable:@"hello"];

}

在应用中设置语言 通过setUserLanguage进行修改。

-(void)setUserLanguage:(NSString*)language{

NSUserDefaults*def = [NSUserDefaultsstandardUserDefaults];

//1.第一步改变bundle的值

NSString*path = [[NSBundlemainBundle]pathForResource:languageofType:@"lproj"];

self.bundle= [NSBundlebundleWithPath:path];

//2.持久化

[defsetValue:languageforKey:userLanguage];

//3.同步

[defsynchronize];

[[NSNotificationCenterdefaultCenter]postNotificationName:languageDidChangeobject:nil];

}

到这里 工具类就写好了


在相应的需要变换文字的控件中添加消息的监听

-(instancetype)initWithFrame:(CGRect)frame{

self= [superinitWithFrame:frame];

if(self) {

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(changeTitle)name:languageDidChangeobject:nil];

}

returnself;

}

-(void)changeTitle{

[selfsetTitle:[[FBLanguageToolsharedTool]languageWithLocalTitle:_localTitle]forState:UIControlStateNormal];

}

-(void)setLocalTitle:(NSString*)localTitle{

_localTitle= localTitle;

[selfchangeTitle];

}

-(void)dealloc{

[[NSNotificationCenterdefaultCenter]removeObserver:self];

}

到此问题解决


代码就放在https://github.com/slamdunk0414/Localization

你可能感兴趣的:(iOS多语言以及在应用中设置语言)