多语言选择,默认跟随系统语言,选择的时候发出通知更改控制器页面
StringsHelper.h文件
//定义取字符串宏给外界调用
#define StringsHelperGetStringWithKeyFromTable(key,tb) [[StringsHelper shareInstance] getStringWithKey:key table:tb]
#define LANGUAGEHADCHANGED @"languageHadChanged"
//app支持的语言的枚举
typedef NS_ENUM(NSUInteger,Language){
Language_zh_Hans = 0, //中文
Language_en, //英文
Language_ja ///<日文
};
@interface StringsHelper : NSObject
/**
单例
@return 实例
*/
+ (instancetype)shareInstance;
//通过枚举值获取string
- (NSString*)getStringWithLanguage:(Language)language;
//获取bundle
-(NSBundle *)getBundleWithLanguage:(Language)language;
/**
根据key和table获取文本
@param key key
@param table table(实际上是对应的strings的文件名)
@return 文本
*/
- (NSString *)getStringWithKey:(NSString *)key table:(NSString*)table;
/**
传入语言切换app语言并发出通知
@param language 待切换的语言
*/
- (void)changeLanguage:(Language)language;
StringsHelper.m文件
#import "StringsHelper.h"
#define CURRENTLANGUAGE @"currentLanguage"
@interface StringsHelper()
@property (nonatomic,strong) NSBundle *currentBundle; ///<当前文件包
@property (nonatomic,assign) Language currentLanguage; ///<当前语言
@end
@implementation StringsHelper
/**
单例
@return 实例
*/
+ (instancetype)shareInstance{
static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc]init];
});
return instance;
}
//初始化方法
- (instancetype)init{
if (self = [super init]) {
[self initLanguage];
}
return self;
}
//初始化语言,默认是系统默认排序
-(void)initLanguage{
NSNumber *number = [[NSUserDefaults standardUserDefaults] valueForKey:CURRENTLANGUAGE];
if (!number) {
Language lan;
//默认是当前系统的语言
NSString *language = [NSLocale preferredLanguages][0];
if ([language hasPrefix:@"zh_Hans"])
{
lan = Language_zh_Hans;
}
else if ([language hasPrefix:@"en"])
{
lan = Language_en;
}
else if ([language hasPrefix:@"ja"])
{
lan = Language_ja;
}
else
{
//默认中文
lan = Language_zh_Hans;
}
//储存当前语言
[[NSUserDefaults standardUserDefaults]setValue:@(lan) forKey:CURRENTLANGUAGE];
//重新获取一下
number = [[NSUserDefaults standardUserDefaults] valueForKey:CURRENTLANGUAGE];
}
self.currentLanguage = [number integerValue]; ///<默认
[self setUpCurrentBundle:self.currentLanguage];
}
//获取bundle
-(NSBundle *)getBundleWithLanguage:(Language)language
{
//获取当前的语言所在路径并且获取bundle,如果bundle不存在则创建
NSString *language_Str = [self getStringWithLanguage:language];
NSAssert(language_Str != nil, @"language的值有误");
//在mainbundle下每种语言都会产生一个 语言名字.lproj 的文件,文件里面包含了对应的table(即strings文件)
NSString *path = [[NSBundle mainBundle] pathForResource:language_Str ofType:@"lproj"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
return bundle;
}
//获取当前的语言所在路径并且获取bundle
- (void)setUpCurrentBundle:(Language)language{
self.currentBundle = [self getBundleWithLanguage:language];
[[NSUserDefaults standardUserDefaults] setValue:@(language) forKey:CURRENTLANGUAGE];
}
//通过枚举值获取string
- (NSString*)getStringWithLanguage:(Language)language{
switch (language) {
case Language_zh_Hans:
return @"zh-Hans";
break;
case Language_en:
return @"en";
break;
case Language_ja:
return @"ja";
break;
default:
return nil;
break;
}
}
/**
在当前的bundle下,根据key和table获取文本
@param key key
@param table table(实际上是对应的strings的文件名)
@return 文本
*/
- (NSString *)getStringWithKey:(NSString *)key table:(NSString*)table{
if (self.currentBundle) {
return NSLocalizedStringFromTableInBundle(key, table, self.currentBundle, @"");
}
else{
return NSLocalizedStringFromTable(key, table, @"");
}
}
/**
传入语言切换app语言并发出通知
@param language 待切换的语言
*/
- (void)changeLanguage:(Language)language{
if (language == self.currentLanguage) {
return;
}
self.currentLanguage = language;
//改变当前的bundle
[self setUpCurrentBundle:language];
//发出通知,让其他控制器重新加载控制器(可以在APPDelagate里面设置)
[[NSNotificationCenter defaultCenter] postNotificationName:LANGUAGEHADCHANGED object:self userInfo:nil];
}
我是在appDelegate接受通知,接受通知界面添加方法,重新加载控制器