我也是找的别人的,只是加上了自己之前遇到问题解决的方法
自定义个继承于NSObject的类
SSKJLocalized.h
//语言切换
static NSString * const AppLanguage = @"appLanguage";
#define ZBLocalized(key, comment) [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:AppLanguage]] ofType:@"lproj"]] localizedStringForKey:(key) value:@"" table:nil]
+ (SSKJLocalized *)sharedInstance;
//初始化多语言功能
- (void)initLanguage;
//当前语言
- (NSString *)currentLanguage;
//设置要转换的语言
- (void)setLanguage:(NSString *)language;
//设置为系统语言
- (void)systemLanguage;
这是.h里面需要写的,注释也有,我感觉已经很清楚了
下面是.m文件代码
+ (SSKJLocalized *)sharedInstance{
static SSKJLocalized *language=nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
language = [[SSKJLocalized alloc] init];
});
return language;
}
- (void)initLanguage{
NSString *language=[self currentLanguage];
if (language.length>0) {
NSLog(@"自设置语言:%@",language);
}else{
[self systemLanguage];
}
}
- (NSString *)currentLanguage{
NSString *language=[[NSUserDefaults standardUserDefaults]objectForKey:AppLanguage];
return language;
}
- (void)setLanguage:(NSString *)language{
[[NSUserDefaults standardUserDefaults] setObject:language forKey:AppLanguage];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)systemLanguage{
NSString *languageCode = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"][0];
NSLog(@"系统语言:%@",languageCode);
if([languageCode hasPrefix:@"zh-Hant"]){
languageCode = @"zh-Hant";//繁体中文
}else if([languageCode hasPrefix:@"zh-Hans"]){
languageCode = @"zh-Hans";//简体中文
}else if([languageCode hasPrefix:@"pt"]){
languageCode = @"pt";//葡萄牙语
}else if([languageCode hasPrefix:@"es"]){
languageCode = @"es";//西班牙语
}else if([languageCode hasPrefix:@"th"])
{ languageCode = @"th";//泰语
}else if([languageCode hasPrefix:@"hi"]){
languageCode = @"hi";//印地语
}else if([languageCode hasPrefix:@"en"]){
languageCode = @"en";//英语
}else{
languageCode = @"en";//英语
}
[self setLanguage:languageCode];
}
可以选择多个语言
下面是需要在APPDelegate里面所需要写的代码
下面是controller里面所需要的代码 我这里是测试用的,大家需要根据实施情况进行修改
下面是最重要的部分
添加类的方法:
这里就添加完了 然后需要最后一部 就是把需要转换的文字写到刚才创建的类里面