应用内动态更换语言
2016-07-05 00:00
183 查看
摘要: 基本思路:根据路径bundle取不同的语言文件。
[code=language-objectivec]1.定义文件路径宏:(所有文本都用宏) #define CustomLocalizedString(key, comment) \ [[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userLanguage"]] ofType:@"lproj"]] localizedStringForKey:(key) value:@"" table:@"AppLanguage"] 2.根据alert选项取不同文件: if (IOS8_OR_LATER) { UIAlertController *languageAlert = [UIAlertController alertControllerWithTitle:@"" message:CustomLocalizedString(@"language_mode", nil) preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"language_cancelBtn", nil) style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *tcAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_TC", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant" forKey:@"userLanguage"]; [self refreshHomeView]; }]; UIAlertAction *scAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_SC", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"userLanguage"]; [self refreshHomeView]; }]; UIAlertAction *enAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_EN", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"userLanguage"]; [self refreshHomeView]; }]; [languageAlert addAction:cancelAction]; [languageAlert addAction:tcAction]; [languageAlert addAction:scAction]; [languageAlert addAction:enAction]; [self presentViewController:languageAlert animated:YES completion:nil]; } else { UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:CustomLocalizedString(@"language_mode", nil) delegate:self cancelButtonTitle:CustomLocalizedString(@"language_cancelBtn", nil) destructiveButtonTitle:nil otherButtonTitles:CustomLocalizedString(@"login_language_item_TC", nil), CustomLocalizedString(@"login_language_item_SC", nil), CustomLocalizedString(@"login_language_item_EN", nil), nil]; [sheet showInView:self.view]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (buttonIndex) { case 0: [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant" forKey:@"userLanguage"]; [self refreshHomeView]; break; case 1: [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"userLanguage"]; [self refreshHomeView]; break; case 2: [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"userLanguage"]; [self refreshHomeView]; break; default: break; } } 图片其他要区分语言的文件,相同原理。
相关文章推荐
- 502 Bad Gateway 错误的解决办法
- carbon配置
- carbon进程
- Spring扫描配置文件的两种方式及获取配置文件属性参数的方式
- MWC 2016:F5发布运营商级的移动互联安全解决方案
- linux 打包 解压 tar zip tgz
- gulp-cached让你的gulp不再慢如牛
- Tommcat源码学习(三)--Tomcat_7.0.70停止服务过程分析
- [zookeeper]1.zookeeper安装
- [zookeeper]2.zookeeper原理特性以及典型使用案例
- CentOS添加和删除用户
- 查看centos中的用户和用户组
- linux下查看tomcat和jdk版本号、重启tomcat、查看tomcat日志的命令
- spring 事务管理的bug
- 微博首席架构师杨卫华:新浪微博技术架构分析
- PHP数组常用函数分类整理
- IDEA上搭建jeesite框架
- 网站演化整理
- sql分析web可视化平台-viuSQL测试版V1.0
- [mybatis]Mybatis 数据处理