以编程方式更改应用中的语言 [英] Change Language in the app programmatically

查看:93
本文介绍了以编程方式更改应用中的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Xcode 5中更改语言

How to change the language in Xcode 5

我在 ChangeLanguageViewController上有3个按钮

这是:


  • 英语

  • 简体中文

  • Bahasa Malaysia

以下是代码:

#import <UIKit/UIKit.h>
@interface ChangeLanguageViewController :UIViewController
-(IBAction)changeEnglish:(id)sender;
-(IBAction)changeChinesesimplified:(id)sender;
-(IBAction)changeBahasaMalaysia:(id)sender;

-(IBAction)changeEnglish:(id)sender{

[[NSUSerDefaults standardUSerDefaults] setObject:[NSArray arrayWithObjects:@"en", @"zh-Hans", @"ms", nil] forKey:@"AppleLanguage"];
}
-(IBAction)changeChinesesimplified:(id)sender{
[[NSUSerDefaults standardUSerDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", @"ms", @"en", nil] forKey:@"AppleLanguage"];
}
-(IBAction)changeBahasaMalaysia:(id)sender{
[[NSUSerDefaults standardUSerDefaults] setObject:[NSArray arrayWithObjects:@"ms",@"en",@"zh-Hans", nil] forKey:@"AppleLanguage"];
}

我希望当用户按下按钮时语言会发生变化不要杀死应用程序,但它会更改应用程序内的语言

I want to see the language change when the user presses a button and it will not kill the application but it changes the language of the app inside

推荐答案

通常当您支持Apple支持iOS的官方语言时,没有理由在应用程序内提供语言切换,只需在项目中正确设置翻译,界面语言就会自动切换到系统。但是既然你想从应用程序中获取它,有几种方法可以解决这个问题:

Usually when you support official languages that Apple supports in iOS, there is no reason to provide language switching within the app, just properly set up translations in your project and interface language will automatically switch with the system. But since you want it from the app, there are few ways to go about this:

1)您可以使用以下代码强制为您的应用程序使用特定语言:

1) You could force a specific language for just your app with the following code:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", @"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

我建议将此代码放在main.m文件中的int main函数中返回UIApplicationMain。但是这种方法要求你杀死应用程序或告诉用户重新启动应用程序才能生效。

I would suggest putting this code inside main.m file in "int main" function just before the "return UIApplicationMain". But this method requires you to kill the app or tell user to restart the app for it to take effect.

你可以杀死应用程序而无需用户强制退出应用程序使用 exit(0),但请确保用户有机会使用 UIAlertView 或类似内容中止操作,或Apple可能拒绝您的应用。

You can kill the app without having user to force quit the app using exit(0), but make sure user has chance to abort the action with UIAlertView or similar, or Apple might reject your app.

2)替代方案是实施您自己的本地化逻辑,您只需从您自己的语言文件中获取翻译。 就是这个例子的一种方式,它从官方lproj文件中翻译。这样,您可以在不重新启动的情况下即时更改语言,但您必须手动加载代码中的所有标签文本。当您更改翻译时,您必须在屏幕上重新填充文本。

2) Alternative is implementing your own localization logic, where you just take translations from your own language file. One way is this example which takes translations from official lproj files. This way you can change the language on the fly without a restart, but you have to manually load all label texts from the code. When you change the translation, you have to repopulate the text on screen.

这篇关于以编程方式更改应用中的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆