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

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

问题描述

如何在Xcode 5中更改语言

How to change the language in Xcode 5

我在ChangeLanguageViewController

这是:

  • 英语
  • 简体中文
  • 马来西亚语

这是代码:

#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];

我建议将此代码放在返回UIApplicationMain"之前的"int main"函数中的main.m文件中.但是此方法要求您杀死该应用程序或告诉用户重新启动该应用程序以使其生效.

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.

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

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