iOS应用内语言切换&程序实现国际化localizabl
1、demo代码和iOS应用内语言切换后期补上(2016.12.15)
2、demo代码已加上,因重新写的demo所以会跟截图不一样(2016.12.19)
程序实现国际化
根据手机使用语言加载对应的语言:
1、创建.strings文件,系统默认加载Localizable.strings:

2、设置多语言,这边设置了中文、英文:

3、.strings文件添加语言,点击Localizable.strings右侧勾选:

4、在各自的响应文件中,按照"key" = "value"的格式配置:
Localizable.strings( Chinese(Simplified) )中,对中文进行适配
"登录"="登录";
Localizable.strings( English )中,对英文进行适配
"登录"="Login";
5、使用:
系统提供的调用方法,一般只使用到前面2个下面进行说明
#define NSLocalizedString(key, comment) \
[NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:nil]
#define NSLocalizedStringFromTable(key, tbl, comment) \
[NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
[bundle localizedStringForKey:(key) value:@"" table:(tbl)]
#define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
[bundle localizedStringForKey:(key) value:(val) table:(tbl)]
//默认调用Localizable.strings
self.title = NSLocalizedString(@"登录",@“这是注释无影响可填写nil”);
//调用自定义myName.strings
self.title = NSLocalizedStringFromTable(@"登录",@"myName",@“这是注释无影响可填写nil”);
设置模拟器语言,Product->Scheme->Edit Scheme...

图片、xib、storyboard同理设置(不同语言图片切换可使用NSBundle)




改变手机语言效果展示:


iOS应用内语言切换
重点:通过刷新界面或者重新设置rootViewController
通过更改bundle来获取不同语言,下面是主要代码
NSString *tmp = [[NSUserDefaults standardUserDefaults]objectForKey:languageKey];
if (tmp.length == 0 || tmp == nil) {
tmp = @"zh-Hans";//默认中文
}
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:tmp ofType:@"lproj"]];
if (bundle)
{
return NSLocalizedStringFromTableInBundle(key, table, bundle, @"");
}
点击按钮切换显示效果如下:

