标签:add http .com erlang 刷新 array after 系统 efault
1.新建工程之类的就不用说了,配置项目本地化支持简体中文和英文
工程-PROJECT-info->Localizations,点“+”,选择(Chinese(Simplified))添加简体中文,英文Xcode自带有(English),所以不需要再次添加。(点击添加弹出语言菜单,即可选择你所需要的语言)
2.现在可以开始添加多语言文件了。
注意:需要手动切换语言,而不是根据系统语言变换,所以命名不能与系统语言文件同名,即Localizable.strings;
自己随意命名,后缀是.strings,一定要记得Targets要勾选项目。
3.配置English.strings本地化,现在支持中文和英文。
(1)选中English.strings文件,查看右边栏中的信息;(2)点击Localization中的按钮,弹出框中显示出前面添加的语言;(3)选择中文和英文,base选项可有可无,
勾选之后,English.strings中多出了两个文件
完成之后便是完成了项目内多语言环境的配置,下面看一下这两个文件中应该怎样写
4.文件中添加两个需要的字符串
中文:
"change_language" = "大家好,我叫陈明";
"button" = "切换语言";
"change_language" = "Hello,My name is Magellan";
"button" = "Change Language";
5.配置和语言配置表写好后,接下来开始具体运用到业务中。
(1)新创建工具类,继承自NSObject(在这里我命名为SwichLanguage)
(2)SwichLanguage.h
+(NSBundle *)bundle;//获取当前资源文件
+(void)initUserLanguage;//初始化语言文件
+(NSString *)userLanguage;//获取应用当前语言
+(void)setUserlanguage:(NSString *)language;//设置当前语言
SwichLanguage.m 文件中
#import "SwichLanguage.h"
static NSString*LocalLanguageKey = @"SwitchLanguage";
@implementation SwichLanguage
static NSBundle *bundle = nil;
+ ( NSBundle * )bundle{
return bundle;
}
//首次加载的时候先检测语言是否存在
+(void)initUserLanguage{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *currLanguage = [def valueForKey:LocalLanguageKey];
if(!currLanguage){
NSArray *preferredLanguages = [NSLocale preferredLanguages];
currLanguage = preferredLanguages[0];
if ([currLanguage hasPrefix:@"en"]) {
currLanguage = @"en";
}else if ([currLanguage hasPrefix:@"zh"]) {
currLanguage = @"zh-Hans";
}else currLanguage = @"en";
[def setValue:currLanguage forKey:LocalLanguageKey];
[def synchronize];
}
//获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:currLanguage ofType:@"lproj"];
bundle = [NSBundle bundleWithPath:path];//生成bundle
}
//获取当前语言
+(NSString *)userLanguage{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSString *language = [def valueForKey:LocalLanguageKey];
return language;
}
//设置语言
+(void)setUserlanguage:(NSString *)language{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *currLanguage = [userDefaults valueForKey:LocalLanguageKey];
if ([currLanguage isEqualToString:language]) {
return;
}
[userDefaults setValue:language forKey:LocalLanguageKey];
[userDefaults synchronize];
NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj" ];
bundle = [NSBundle bundleWithPath:path];
}
(2)ViewController.m
#import "ViewController.h"
#import "SwichLanguage.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *changeButton;
@property (weak, nonatomic) IBOutlet UILabel *languageLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLanguage) name:@"changeLanguage" object:nil];
//初始化应用语言
[SwichLanguage initUserLanguage];
NSBundle *bundle = [SwichLanguage bundle];
/*
localizedStringForKey:@"change_language" value:nil table:@"English"
localizedStringForKey:各个语言文件中共同的名称
table: English.strings 多语言文件夹的名称
*/
NSString *str = [bundle localizedStringForKey:@"change_language" value:nil table:@"English"];
_languageLabel.text = str;
}
//切换语言的点击方法
- (IBAction)changeLanguage:(id)sender {
//修改语言
NSString *language = [SwichLanguage userLanguage];
if ([language isEqualToString:@"en"]) {
[SwichLanguage setUserlanguage:@"zh-Hans"];
}else{
[SwichLanguage setUserlanguage:@"en"];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage"object:self];
}
//点击改变语言,代理方法刷新界面
- (void)changeLanguage{
[_changeButton setTitle:[[SwichLanguage bundle] localizedStringForKey:@"button"value:nil table:@"English"] forState:UIControlStateNormal];
_languageLabel.text =[[SwichLanguage bundle] localizedStringForKey:@"change_language"value:nil table:@"English"];
}
//适当的位置移除通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
7.一切完成之后,我们可以运行看下效果
切换成功之后,推出程序重新运行就可以看到保留了上次选择的语言。。
标签:add http .com erlang 刷新 array after 系统 efault
原文地址:http://www.cnblogs.com/magellan/p/7216148.html