1.语言国际化
project-->Info-->Localizations,点击“+”号添加新的国际化的语言;
点击+号会出现以下界面,然后选择你想要添加的语言;
这里面我添加了简体中文、繁体中文、英文、法语、意大利语、日语和韩语;
2.应用国际化
在不同的语言设置下,显示相应的语言。
新建一个Strings File,命名Localizable,在这里一定要选中Info.plist,现在新建文件;
选中创建好的Localizable.strings然后点击右边工具栏的Localizable
在弹出框内选选择自己要本地化的语言;
添加完成以后你会发现Localizable.strings多了几个文件;
3.应用名称国际化
与创建Localizable.strings时一样,创建InfoPlist.strings文件,然后添加支持的语言;
然后在对应的语言文件里面添加字段;
//英语
CFBundleDisplayName = "hello world";
//中文繁体
CFBundleDisplayName = "你好,世界";
//中文简体
CFBundleDisplayName = "你好,世界";
//日语
CFBundleDisplayName = "こんにちは、世界";
//韩语
CFBundleDisplayName = "안녕하세요,세계";
//意大利语
CFBundleDisplayName = "Ciao,Mondo!";
//法语
CFBundleDisplayName = "Bonjour,tout le monde";
4.内容国际化
我在Localizable.strings对应的语言文件里面设置对应的键值对,不过这里面的key和value不像上面设置APP的名字,这里面的key也要加上双引号;
例如,我设置首页标题和首页按钮的键值对;
//英语
"Home_Title" = "Home";
"Home_Button" = "switch language";
//中文繁体
"Home_Title" = "首頁";
"Home_Button" = "切換語言";
//中文简体
"Home_Title" = "首页";
"Home_Button" = "切换语言";
//日语
"Home_Title" = "トップページ";
"Home_Button" = "切替言語";
//韩语
"Home_Title" = "홈";
"Home_Button" = "전환 언어";
//以此类推 等等
这这里我写了一个ZFJLanguageManager管理类,来管理和存储国际化语言设置;到时候这个类会放在下面供大家下载,这里面我直接展示如何调用;
我先注册一个通知,来接收语言发生了改变;
//注册通知,用于接收改变语言的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLanguage) name:ChangeLanguageNotificationName object:nil];
#pragma mark - 给界面UI控件赋值 和 通知刷新界面UI
- (void)changeLanguage{
self.title = kLocalizedString(@"Home_Title", @"语言");
[self.switchLanguageBtn setTitle:kLocalizedString(@"Home_Button", @"切换语言") forState:UIControlStateNormal];
}
效果如下:
5.语言列表
我的设计思路是用户可以手动该表APP的语言,所以先要获取APP支持的语言列表,然后可以手动设置;
先设置语言列表;
- (void)uiConfig{
self.dataArray = @[@"zh-Hans-CN", //中文简体
@"zh-Hant-CN", //中文繁体
@"en-CN", //英语
@"ko-CN", //韩语
@"ja-CN", //日语
@"fr-CN", //法语
@"it-CN"]; //意大利语;
self.tableView = [[UITableView alloc]init];
self.tableView.frame = [UIScreen mainScreen].bounds;
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];
//回调 改变界面UI
[ZFJLanguageManager shareInstance].completion = ^(NSString *currentLanguage) {
[self changeLanguage];
};
}
通过数据源数组的字段获取对应国家的语言;
#pragma mark - 对应国家的语言
- (NSString *)ittemCountryLanguage:(NSString *)lang {
NSString *language = [kLanguageManager languageFormat:lang];
NSString *countryLanguage = [[[NSLocale alloc] initWithLocaleIdentifier:language] displayNameForKey:NSLocaleIdentifier value:language];
return countryLanguage;
}
当前语言下的对应国家语言翻译#pragma mark - 当前语言下的对应国家语言翻译
- (NSString *)ittemCurrentLanguageName:(NSString *)lang {
NSString *language = [kLanguageManager languageFormat:lang];
//当前语言
NSString *currentLanguage = kLanguageManager.currentLanguage;
//当前语言下的对应国家语言翻译
NSString *currentLanguageName = [[[NSLocale alloc] initWithLocaleIdentifier:currentLanguage] displayNameForKey:NSLocaleIdentifier value:language] ;
return currentLanguageName;
}
在tableview里面展示语言列表
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifierStr = @"cellStr";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierStr];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierStr];
}
NSString *language = [kLanguageManager languageFormat:self.dataArray[indexPath.row]];
NSString *countryLanguage = [self ittemCountryLanguage:self.dataArray[indexPath.row]];
//当前语言下的对应国家语言翻译
NSString *currentLanguageName = [self ittemCurrentLanguageName:self.dataArray[indexPath.row]] ;
cell.textLabel.text = countryLanguage;
cell.detailTextLabel.text = currentLanguageName;
//当前语言
NSString *currentLanguage = kLanguageManager.currentLanguage;
if([currentLanguage rangeOfString:language].location != NSNotFound){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
选中某一行,改变整个工程的语言风格;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *language = self.dataArray[indexPath.row];
[kLanguageManager setUserlanguage:language];
[self backBtnPress];
}
6.运行效果
7.DEMO下载