作者:Love@YR
链接:http://blog.csdn.net/jingqiu880905/article/details/52193039
请尊重原创,谢谢!
应用程序名称:
1. Project-Info-Localizations那边加语言
2. 新建InfoPlist.strings文件
3. 点击此文件,右方Localization里选择语言
4. 生成多个InfoPlist.strings,写上”CFBundleDisplayName” =”myProgram”; 还有”CFBundleDisplayName” =”我的应用名称”; 设置工程名字(注意加引号),不然默认就是你的工程名字
Application has localized display name:YES
字符串:
创建Localizable.strings文件
分别在strings文件里:”alertTitle” = “title”;
“alertTitle” = “标题”;
用的时候:NSLocalizedString(@”alertTitle”, @”这是一句注解,根据情况写”)
图片:
一张图片,右边file inspector那里localization,生成几张图,替换掉要替换的,文件名不变即可。
xib, stroyboard等资源文件一样的做法。
想国际化icon图标,不行。
不更改系统的设置,不重启app,在app里做个设置语言的切换,切换完了就让页面重新load:
plist里弄两个dic,一个en,一个ch的。如上面例子的话里面key为:”alertTitle” value分别为”title”和”标题”
创建一个i18n类(i18n是internationalization的简写),这个类的实现:
@implementation i18n
+ (NSString *)getI18nStringWithKey:(NSString *)theKey andLanguageCode:(NSString*)theLanguageCode {
NSString *returnStr = [NSString string];
NSString *path = [[NSBundle mainBundle] pathForResource:@"i18n" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
if ([theLanguageCode isEqualToString:kEnglishLanuageCode])
{
returnStr = [(NSDictionary *)[dict objectForKey:@"en"] objectForKey:theKey];
}
else
returnStr = [(NSDictionary *)[dict objectForKey:@"ch"] objectForKey:theKey];
return returnStr;
}
@end
而在设置语言的时候设置成一个单例。
用的时候就:
if ([[[Language sharedInstance] getCurrentLanguageCode] isEqualToString:kEnglishLanuageCode]) {
self.title = [NSString stringWithFormat:@"%@ to %@",self.fromStation,self.endStation];
}else{
self.title = [NSString stringWithFormat:@"%@ 到 %@",self.fromStation,self.endStation];
}