iOS--App应用内切换语言
核心方法:
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \ [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
原理即在工程内部建立 .String,在内部采用键值对方式对所需要切换语言的文字进行赋值
1.工程内部创建.String文件,命名为Language.String
2.右侧菜单栏点击Localize,弹出菜单选择English,确定
3.第二步结束后选择工程,选择PROJECT,下方Localizations菜单中点击+号选择添加中文
4.此时Language.String中会显示两个文件,Language.String(Chinese) 和 Language.String(English),这一步像这两个文件中添加键值对
英文文件中:
"12hour" = "12-Hour Time"; "24hour" = "24-Hour Time"; "am" = "AM"; "pm" = "PM";
中文文件中
"12hour" = "12小时制"; "24hour" = "24小时制"; "am" = "上午"; "pm" = "下午";
5.新建一个单例类,ChangeLanguage,.h中添加属性方法
#import <Foundation/Foundation.h> //app应用内切换语言 #define HLLocalizedString(key, tbl) [LocalizationManager getStringForKey:key withTable:tbl] //#define HLLocalizedString(key, tbl) NSLocalizedString(key, tbl) @interface LocalizationManager : NSObject /// 获取当前资源文件 + (NSBundle *)bundle; /// 初始化语言文件 + (void)initUserLanguage; /// 获取应用当前语言 + (NSString *)userLanguage; /// 设置当前语言 + (void)setUserlanguage:(NSString *)language; /// 通过Key获得对应的string + (NSString *)getStringByKey:(NSString *)key; + (NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table; @end
6.在.m中实现方法
#import "LocalizationManager.h" #import "AppDelegate.h" @implementation LocalizationManager static NSString *TableName = @"localization"; static NSBundle *bundle = nil; static NSString *UserLanguage = @"UserLanguage"; + (NSBundle *)bundle { if(!bundle) { [self initUserLanguage]; } return bundle; } + (void)initUserLanguage{ NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; NSString *string = [def valueForKey:UserLanguage]; if(string.length == 0) { //获取系统当前语言版本(中文zh-Hans,英文en) NSArray *languages = [NSLocale preferredLanguages]; NSString *current = [languages objectAtIndex:0]; string = [self languageFormat:current]; [def setValue:current forKey:UserLanguage]; [def synchronize];//持久化,不加的话不会保存 } //获取文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:[self languageFormat:string] ofType:@"lproj"]; bundle = [NSBundle bundleWithPath:path];//生成bundle } + (void)setUserlanguage:(NSString *)language{ NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; //1.第一步改变bundle的值 NSString *path = [[NSBundle mainBundle] pathForResource:[self languageFormat:language] ofType:@"lproj" ]; bundle = [NSBundle bundleWithPath:path]; //2.持久化 [def setValue:language forKey:UserLanguage]; [def synchronize]; [self resetRootViewController]; } + (NSString *)userLanguage { NSString *userLanguage = [[NSUserDefaults standardUserDefaults] objectForKey:UserLanguage]; return userLanguage; } + (NSString *)getStringByKey:(NSString *)key { return [[LocalizationManager bundle] localizedStringForKey:key value:nil table:TableName]; } ///语言和语言对应的.lproj的文件夹前缀不一致时在这里做处理 + (NSString *)languageFormat:(NSString*)language { if([language rangeOfString:@"zh-Hans"].location != NSNotFound) { return @"zh-Hans"; } else if([language rangeOfString:@"zh-Hant"].location != NSNotFound) { return @"zh-Hant"; } else { //字符串查找 if([language rangeOfString:@"-"].location != NSNotFound) { //除了中文以外的其他语言统一处理@"ru_RU" @"ko_KR"取前面一部分 NSArray *ary = [language componentsSeparatedByString:@"-"]; if (ary.count > 1) { NSString *str = ary[0]; return str; } } } return language; } + (NSString *)getStringForKey:(NSString *)key withTable:(NSString *)table { if ([LocalizationManager bundle]) { return NSLocalizedStringFromTableInBundle(key, table, [LocalizationManager bundle], @""); } return NSLocalizedStringFromTable(key, table, @""); } //重新设置 + (void)resetRootViewController { AppDelegate *delegate = [AppDelegate getAPPDelegate]; [delegate initTabbarCV]; } @end
7:使用
#切换时使用 if([[LocalizationManager userLanguage] isEqualToString:@"en"]) { [LocalizationManager setUserlanguage:@"zh-Hans"]; } else { [LocalizationManager setUserlanguage:@"en"]; } #注意,多语言的地方需要使用如下定义才有效 HLLocalizedString(@"Setlang", nil)