您的位置:首页 > 其它

应用内动态更换语言

2016-07-05 00:00 183 查看
摘要: 基本思路:根据路径bundle取不同的语言文件。

[code=language-objectivec]1.定义文件路径宏:(所有文本都用宏)
#define CustomLocalizedString(key, comment) \
[[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"userLanguage"]] ofType:@"lproj"]] localizedStringForKey:(key) value:@"" table:@"AppLanguage"]

2.根据alert选项取不同文件:
if (IOS8_OR_LATER)
{

UIAlertController *languageAlert = [UIAlertController alertControllerWithTitle:@"" message:CustomLocalizedString(@"language_mode", nil) preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"language_cancelBtn", nil) style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *tcAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_TC", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant" forKey:@"userLanguage"];
[self refreshHomeView];
}];
UIAlertAction *scAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_SC", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"userLanguage"];
[self refreshHomeView];
}];
UIAlertAction *enAction = [UIAlertAction actionWithTitle:CustomLocalizedString(@"login_language_item_EN", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"userLanguage"];
[self refreshHomeView];
}];
[languageAlert addAction:cancelAction];
[languageAlert addAction:tcAction];
[languageAlert addAction:scAction];
[languageAlert addAction:enAction];
[self presentViewController:languageAlert animated:YES completion:nil];
}
else
{
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:CustomLocalizedString(@"language_mode", nil)
delegate:self
cancelButtonTitle:CustomLocalizedString(@"language_cancelBtn", nil)
destructiveButtonTitle:nil
otherButtonTitles:CustomLocalizedString(@"login_language_item_TC", nil),
CustomLocalizedString(@"login_language_item_SC", nil),
CustomLocalizedString(@"login_language_item_EN", nil),
nil];
[sheet showInView:self.view];

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hant" forKey:@"userLanguage"];
[self refreshHomeView];
break;

case 1:
[[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:@"userLanguage"];
[self refreshHomeView];
break;
case 2:
[[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:@"userLanguage"];
[self refreshHomeView];
break;
default:
break;
}
}

图片其他要区分语言的文件,相同原理。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: