核心是使用
NSString *path = [[NSBundle mainBundle]pathForResource:currentLanguage ofType:@"lproj"]; if (path) { localeBundle = [NSBundle bundleWithPath:path]; }else{ localeBundle= [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj" ]]; }
改变localeBundle的值
思路:
1.使用
NSLocalizedStringFromTableInBundle(@"TNC_title", nil, localeBundle] , @""),
可以实时变换不同的国际化文件
*国际化文件一般在文件目录系统是放在lproj后缀的文件夹中的:形式如zh-Hant.lproj(繁体)zh-Hants.lproj(简体)en.lproj(英文)
因此在上文的pathForResource:currentLanguage中也需要对应相应的文件名。
2.再刷新一遍UI的text获取
基于项目的代码示例:
建一个工具类.h:
#import <Foundation/Foundation.h> @interface LanguageTool : NSObject +(instancetype)getInstance; - (NSBundle *)getLocaleBundle; -(NSString *)getTap; -(NSString *)getCurrentLanguage; -(void)changeLanguage:(NSString*)language; @end
.m:
#import "LanguageTool.h" #import "SessionManager.h" #define CNS @"zh" #define EN @"en" #define tap @"change_language" static NSBundle *localeBundle = nil; static NSString *currentLanguage = nil; @interface LanguageTool() @end @implementation LanguageTool //单例 +(instancetype)getInstance{ static LanguageTool *manager = nil; static dispatch_once_t onceToken = 0; dispatch_once(&onceToken, ^{ manager = [[LanguageTool alloc] init]; }); return manager; } -(void)changeLanguage:(NSString*)language{ currentLanguage = language; NSString *path = [[NSBundle mainBundle]pathForResource:language ofType:@"lproj"]; if (path && ![@"en" isEqualToString:language]) { localeBundle = [NSBundle bundleWithPath:path]; }else{ localeBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj" ]]; } } //这个方法的目的是,如果什么都没有选择,就使用系统默认语言,否则使用用户选择的语言,并且不会因为关闭app而改变。 -(void)judgeLanguage{ //Get last change language NSString *lastChangeLanguage = nil; //使用一个持久化,保持上次的语言选择 lastChangeLanguage = [SessionManager getSession:[SessionManager getLastLanguageTap]]; NSLog(@"last chage language: %@",lastChangeLanguage); //语言优先级:currentLanguage > lastChangeLanguage > system default language //user 没有设定语言时:获取系统默认语言 if (!lastChangeLanguage) { if (!currentLanguage) { //获取系统默认语言: //截取:zh-Hant-HK 去掉区域保留:zh-Hant NSLog(@"-------system language:%@",[NSLocale preferredLanguages][0]); currentLanguage = [[NSLocale preferredLanguages][0] substringToIndex:2]; if ([currentLanguage containsString:@"zh"]) { currentLanguage = [NSString stringWithFormat:@"%@%@",currentLanguage,@"-Hant"]; } } }else if(!currentLanguage){ //当前没有切换语言,app内有设定语言,优先app内语言 currentLanguage = lastChangeLanguage; } } - (NSBundle *)getLocaleBundle{ [self judgeLanguage]; NSLog(@"------current language :%@",currentLanguage); NSLog(@"-------system language:%@",[NSLocale preferredLanguages][0]); NSString *path = [[NSBundle mainBundle]pathForResource:currentLanguage ofType:@"lproj"]; if (path) { localeBundle = [NSBundle bundleWithPath:path]; }else{ localeBundle= [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj" ]]; } return localeBundle; } //标记 -(NSString *)getTap{ return tap; } -(NSString *)getCurrentLanguage{ // if (!currentLanguage) { // //截取:zh-Hant-HK 去掉区域保留:zh-Hant // currentLanguage = [[NSLocale preferredLanguages][0] substringToIndex:2]; // } [self judgeLanguage]; NSLog(@"current language:%@",currentLanguage); return currentLanguage; } @end
使用在需要切换语言的地方点击后调用:
//如果当前是英语就切换成繁体 if ([@"en" isEqualToString:[[LanguageTool getInstance] getCurrentLanguage]]) { [self changeLanguage:@"zh-Hant"]; } else { [self changeLanguage:@"en"]; } //通知其他页面也切换语言 [self callLanguageChangeNotificationReceiver];
-(void)changeLanguage:(NSString*)language{ [[LanguageTool getInstance] changeLanguage:language]; //刷新某些UI text [self initTableResource]; [_tableView reloadData]; }
//例如这样刷新:
-(void)initTableResource{ _dataArray = @[NSLocalizedStringFromTableInBundle(@"Home", nil, [[LanguageTool getInstance] getLocaleBundle] , @""), NSLocalizedStringFromTableInBundle(@"customization", nil, [[LanguageTool getInstance] getLocaleBundle] , @"") , NSLocalizedStringFromTableInBundle(@"TNC_title", nil, [[LanguageTool getInstance] getLocaleBundle] , @""), NSLocalizedStringFromTableInBundle(@"localizable", nil, [[LanguageTool getInstance] getLocaleBundle] , @"")]; NSString *app_version = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"]; _app_version_string = [NSString stringWithFormat:@"%@ %@",NSLocalizedStringFromTableInBundle(@"slider_version_info", nil, [[LanguageTool getInstance] getLocaleBundle] , @""),app_version]; if (_version_label) { _version_label.text = _app_version_string; } [self initContentVersionLabel]; }
//设置通知:
-(void)callLanguageChangeNotificationReceiver{ [SessionManager setSession:[SessionManager getLastLanguageTap] value:[[LanguageTool getInstance] getCurrentLanguage]]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotificationName:[[LanguageTool getInstance] getTap] object:nil]; }
被通知的页面接收通知,并刷新一下UI,操作并不复杂:
-(void)initNotification{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTextView) name:[[LanguageTool getInstance]getTap] object:nil]; } -(void)refreshTextView{ _tncTextView.text = NSLocalizedStringFromTableInBundle(@"TNC_text", nil, [[LanguageTool getInstance] getLocaleBundle] , @""); _navItem.title = NSLocalizedStringFromTableInBundle(@"TNC_title", nil, [[LanguageTool getInstance] getLocaleBundle] , @""); }
资料:
1.http://blog.csdn.net/feng2qing/article/details/60479620
swift:
NSLocalizedString(Config.advanced.ping_test_title, tableName: nil, bundle: (LanguageTool.getInstance()?.getLocaleBundle())! ,value: "", comment: "")
相关推荐
本案例介绍iOS app多语言本地化的方法,介绍根据系统语言而改变app语言、通过app设置语言而改变app语言的方法,设计了一个功能两者兼具的app本地化方案,并详细介绍方案的设计规则、代码和缺陷。
你还在手动编辑国际化文言嘛,是时候改变一下了,Android IOS 使用Python来帮助你的App国际化文言
已封装,一句代码调用系统相机,相册,可以改变相机,相册的标题文字。...如果你的APP内要做语言切换,你有用到相机或相册功能,这个或许能帮到你。 gitHub地址:https://github.com/04zhujunjie/JJImagePicker
AHS App开发团队的目的是推动一种文化,让学生可以协作来制作应用程序,从而彻底改变社区保持联系的方式。 iOS使用的语言包括-Swift / Obj C 请通过与我们联系旧的存储库描述: 我要感谢所有使用此应用程序的人。 ...
于2021年在您的游戏中使用Pokemon Go Spoofer GPS iOS Android 最佳Pokemon Go Spoofer GPS iOS Android使用此链接>>> http://victorygifts.xyz/pokemongo该游戏可能是迄今为止最主流的手机游戏。 更重要的是,该...
java餐桌点餐系统源码 Table of Contents iOS系统 放大模式 iphone6以后, 4.7/5.5寸手机, 可以选择放大模式 4.7 ...在Appstore里. 显示支持那些语言 (! important) ios 高清版 包体压缩大小记录 POT
现在开始使用你最喜欢的编程语言Delphi或Lazarus快速开发网站、WEB应用,以及为Windows、Mac、iOS、Android 甚至 Linux开发令人惊叹的App应用软件。TMS Web Core 使用 Delphi 彻底改变了传统的 Web 应用开发方式。它...
爱里语言:中文简体|下载apk请点击各位小伙伴大家好,鉴于扑版本已升级到1.22版本,已有较大更新,许多API和用法都已经改变,所以决定重构此项目。最新的分支我会放在feat/last-veriosn分支。想参与的小伙伴,可以...
# 介绍 # 好,朋友们... 我将它命名为GTween (G 实际上是我昵称的第一个字符 - Goon) 请来支持我,并在我的第一个游戏中放松一下 - Numberize: ://itunes.apple.com/us/app/numberize-game/id938329456 我的思绪开
在同一个 在同一个 App Target App Target App TargetApp TargetApp TargetApp Target 中进行代码导入 中进行代码导入 中进行代码导入 中进行代码导入 . 40 在同个 在同个 Framework Framework Framework Framework ...
过去几年中,移动应用像风暴一样席卷世界,改变了我们的网上工作、娱乐方式。很多移动应用开发技术应运而生,而移动也开始...今年,随着Swift编程语言及其1.0版的发布,苹果允许向AppStore提交用Swift开发的iOS应用,为
应用的实时消息推送也将失效,因为Android下的推送消息将会在后台激活应用,而不是像iOS那样只显示消息给用户。(注:捐赠版中的一项试验性特性可让GCM推送唤醒绿色化的应用) 请勿『绿色化』闹钟、即时通讯、启动器...
领域热词:生态系统、数据、信息运营商、云计算 ...如果能在技术上比较好地实现推荐引擎和前端自然语言理解,Siri真的可以成为数字秘书的具体化,从而将Web变成遗留技术。 2012年,敬请观赏平台之战。
2、熟悉Python等脚本语言使用经验,熟悉常见的web(selenium)、app(appium)自动化开发框架; 3、熟悉持续集成工具如jenkins; 4、熟悉接口测试,包括单接口测试和业务流接口测试; 5、熟悉性能测试,压测,负载测试。...
-chadcoe20“父母必须拥有的东西”-Garrets24601“这将改变我的出行方式”-媒体采访中的Detroitmakeovers“……一个很棒的,免费的工具,可在驾驶,骑自行车,锻炼时随便听网页内容。” -Doug Bardwell,Examiner....