本地化(不基于系统语言)
来源:互联网 发布:unity3d找工作好难 编辑:程序博客网 时间:2024/05/16 07:21
本地化分为基于系统语言的,即根据系统的语言来确定显示的内容,语言通过手机的设置来定。以及非基于系统的语言,即语言由软件来定,通过用户设置的语言来确定显示的内容。
一下将是第二种。
首先创建一个名字叫“Hello”的stringFile,至于怎么创建的,可以自行去百度,和第一种创建方法一样。
话不多说,直接上代码。
首先是InternationalControl类(继承与NSObject),控制本地化设置。
//// InternationalControl.h// 本地化(不基于系统语言)//// Created by 鲁杜杨 on 15/9/29.// Copyright © 2015年 鲁杜杨. All rights reserved.//#import <Foundation/Foundation.h>@interface InternationalControl : NSObject+(NSBundle *)bundle;//获取当前资源文件+(void)initUserLanguage;//初始化语言文件+(NSString *)userLanguage;//获取应用当前语言+(void)setUserlanguage:(NSString *)language;//设置当前语言@end
//// InternationalControl.m// 本地化(不基于系统语言)//// Created by 鲁杜杨 on 15/9/29.// Copyright © 2015年 鲁杜杨. All rights reserved.//#import "InternationalControl.h"#define USERLANGUAGE @"userLanguage"static NSBundle *bundel=nil;@implementation InternationalControl//获取当前资源文件+(NSBundle *)bundle{ return bundel;}//初始化语言文件+(void)initUserLanguage{ NSUserDefaults *def=[NSUserDefaults standardUserDefaults]; NSString *string=[def valueForKey:@"userLanguage"]; if (string.length==0) { //获取系统当前语言(简中zh-Hans,英文en) string=@"en"; [def setValue:string forKey:@"userLanguage"]; [def synchronize]; } //获取文件路径 NSString *path=[[NSBundle mainBundle] pathForResource:string ofType:@"lproj"]; bundel=[NSBundle bundleWithPath:path];}//获取应用当前语言+(NSString *)userLanguage{ NSUserDefaults *def=[NSUserDefaults standardUserDefaults]; NSString *string=[def valueForKey:@"userLanguage"]; return string;}//设置当前语言+(void)setUserlanguage:(NSString *)language{ NSUserDefaults *def=[NSUserDefaults standardUserDefaults]; //1.第一步改变bundle的值 NSString *path=[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]; bundel=[NSBundle bundleWithPath:path]; //2.持久化 [def setValue:language forKey:@"userLanguage"]; [def synchronize];}@end
下面是使用:
//// ViewController.m// 本地化(不基于系统语言)//// Created by 鲁杜杨 on 15/9/29.// Copyright © 2015年 鲁杜杨. All rights reserved.//#import "ViewController.h"#import "InternationalControl.h"@interface ViewController ()@property(nonatomic,retain) UIButton *kBtn;@property(nonatomic,retain) UILabel *kLabel;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageHasChanged) name:@"changeLanguage" object:nil]; [self configViews];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning];}-(void)configViews{ [self.view addSubview:self.kBtn]; [self.view addSubview:self.kLabel]; [InternationalControl initUserLanguage]; [self languageHasChanged];}-(void)languageHasChanged{ NSBundle *bundle=[InternationalControl bundle]; NSString *inviteMsg=[bundle localizedStringForKey:@"Invite" value:nil table:@"Hello"]; NSString *buttonIfon=[bundle localizedStringForKey:@"buttonInfo" value:nil table:@"Hello"]; [self.kBtn setTitle:buttonIfon forState:UIControlStateNormal]; self.kLabel.text=inviteMsg;}-(void)changeLanguage{ NSString *lan=[InternationalControl userLanguage]; if ([lan isEqualToString:@"en"]) { //判断当前语言 [InternationalControl setUserlanguage:@"zh-Hans"]; }else{ [InternationalControl setUserlanguage:@"en"]; } //改变完成之后发送通知,刷新页面 [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:nil userInfo:nil];}#pragma mark - Setter And Getter-(UIButton *)kBtn{ if (_kBtn==nil) { _kBtn=[UIButton buttonWithType:UIButtonTypeCustom]; _kBtn.frame=CGRectMake(50, 100, 300, 40); [_kBtn setBackgroundColor:[UIColor cyanColor]]; [_kBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_kBtn addTarget:self action:@selector(changeLanguage) forControlEvents:UIControlEventTouchUpInside]; } return _kBtn;}-(UILabel *)kLabel{ if (_kLabel==nil) { _kLabel=[[UILabel alloc] initWithFrame:CGRectMake(50, 200, 300, 40)]; _kLabel.textColor=[UIColor blackColor]; _kLabel.backgroundColor=[UIColor cyanColor]; _kLabel.textAlignment=NSTextAlignmentCenter; } return _kLabel;}@end
0 0
- 本地化(不基于系统语言)
- 本地化(基于系统语言)
- iOS本地化国际化(续):不更随系统,手动选择APP语言 本地化和国际化的实现
- iOS本地化(跟随系统语言变化)
- xcode语言本地化(中文)
- 语言本地化
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(30)-本地化(多语言)
- setting bundle 多语言,本地化,系统设置,ios
- Silverlight中实现多语言(本地化)
- iOS 语言本地化(中文显示)
- iOS 本地化(多语言支持)
- iOS项目本地化-(多语言处理)
- NGUI UI Localize本地化(语言切换)
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)
- java语言本地化示例
- Xcode4语言本地化
- Localizations程序本地化语言
- "GeForce GTX 850M" 的CUDA参数
- java--SAX解析xml
- Codeforces Round #322 (Div. 2) A、B、C
- 股市热点与周期的规律(转载)
- matlab一些基本重要的指令
- 本地化(不基于系统语言)
- nginx 优化(突破十万并发)
- android四种更新UI的方法
- 定义文件流对象,统计字符个数(C/C++源程序)
- 追MM与设计模式
- EditText,TextView,Button,ImageVIew样式
- Solr 3.5 + Tomcat7 + mmseg4j + 搜狗词库 -配置并运行
- 队列的连续与链式实现
- nyoj 最长公共子序列 36 (简单LCS模板)