运行时更改Three20中的语言/本地化

时间:2011-12-02 03:26:10

标签: iphone objective-c ios xcode three20

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

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

1 个答案:

答案 0 :(得分:3)

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

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

static NSBundle *bundle = nil;

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

[[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)

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

为了简化操作,您可以在pch文件中添加静态函数:

#import "AppDelegate.h"

#define MyLocalizedString(key, alt) [AppDelegate get:key]
相关问题