运行时更改Three20中的语言/本地化 [英] Runtime change the language/localization in Three20

查看:100
本文介绍了运行时更改Three20中的语言/本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时更改Three20语言/本地化而无需重新启动应用程序?

Is it possible to change the Three20 language/localization at runtime without restarting the app?

目前,我设法通过更改<$的值来更改语言在main.m中c $ c> AppleLanguages

Currently, I managed to change the language via altering the value of AppleLanguages in the main.m

推荐答案

这是一个黑客。您可以使用本地化文本加载自己的NSBundle并使用该NSBundle。请注意,如果缺少本地化语言文件,则应用程序将无法运行,因此请确保设置正确的语言。

There's a "hack" for it. You can load your own NSBundle with the localized text and use that NSBundle instead. Note that if the localized language file is missing, the app won't run, so make sure you set a correct language.

在AppDelegate实现之上,添加自定义NSBundle声明:

Above your AppDelegate implementation, add a custom NSBundle declaration:

static NSBundle *bundle = nil;

然后将您想要的语言加载到该捆绑包中:

And then load the language you desire into that bundle:

[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"he", nil] forKey:@"AppleLanguages"];

NSLocale* locale = TTCurrentLocale();  
NSString *path = [[NSBundle mainBundle] pathForResource:[locale localeIdentifier] ofType:@"lproj" ];
bundle = [[NSBundle bundleWithPath:path] retain];

您将在AppDelegate中添加自定义函数以获取本地化文本(而不是NSLocalizedString)

You will add a custom function in your AppDelegate to get the localized text too (instead of NSLocalizedString)

///////////////////////////////////////////////////////////////////////////////////////////////////
+ (NSString*)get:(NSString*)key  {
   return [bundle localizedStringForKey:key value:nil table:nil];
}

为了方便起见,您可以在pch文件中添加静态函数:

To make things easier, you can add a static function in the pch file:

#import "AppDelegate.h"

#define MyLocalizedString(key, alt) [AppDelegate get:key]

这篇关于运行时更改Three20中的语言/本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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