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" = "Quietness is my farewell music, silence is Cambridge tonight";
"button" = "Change Language";
5.配置环境已经完成,现在看看代码怎么写????
(1)新创建工具类,继承自NSObject(在这里我命名为ChangeLanguage)
(2)ChangeLanguage.h
+(NSBundle *)bundle;//获取当前资源文件
+(void)initUserLanguage;//初始化语言文件
+(NSString *)userLanguage;//获取应用当前语言
+(void)setUserlanguage:(NSString *)language;//设置当前语言
ChangeLanguage.m
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];
}
6.现在方法已经写完,下面就来检测一下是否好用吧
(1)拖拽storyboard,设置点击事件(storyboard方便,简单)
(2)ViewController.m
- (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];
//初始化应用语言
[ChangeLanguage initUserLanguage];
NSBundle *bundle = [ChangeLanguage bundle];
/*
localizedStringForKey:@"change_language" value:nil table:@"English"
localizedStringForKey:各个语言文件中共同的名称
table: English.strings 多语言文件夹的名称
*/
NSString *str = [bundle localizedStringForKey:@"change_language" value:nil table:@"English"];
NSString *buttonStr = [bundle localizedStringForKey:@"button" value:nil table:@"English"];
[_changeButton setTitle:buttonStr forState:UIControlStateNormal];
_languageLabel.text = str;
}
//切换语言的点击方法
- (IBAction)change:(id)sender {
//修改语言
NSString *language = [ChangeLanguage userLanguage];
if ([language isEqualToString:@"en"]) {
[ChangeLanguage setUserlanguage:@"zh-Hans"];
}else{
[ChangeLanguage setUserlanguage:@"en"];
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"changeLanguage" object:self];
}
//点击改变语言,代理方法刷新界面
- (void)changeLanguage{
[_changeButton setTitle:[[ChangeLanguage bundle] localizedStringForKey:@"button" value:nil table:@"English"] forState:UIControlStateNormal];
_languageLabel.text =[[ChangeLanguage bundle] localizedStringForKey:@"change_language" value:nil table:@"English"];
}
//适当的位置移除通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
7.下面就来两张运行的界面界面吧
语言切换成功之后,推出程序重新运行就可以看到保留了上次选择的语言。。
转载:http://blog.csdn.net/ywm04050712/article/details/53319464
http://blog.csdn.net/jackjia2015/article/details/51554375
http://www.cocoachina.com/ios/20150114/10911.html
http://www.jianshu.com/p/627da5a9992c
http://www.jianshu.com/p/c7e8e999c8c7