是否可以在运行时更改 Three20 语言/本地化而无需重新启动应用程序?
目前,我设法通过更改 main.m 中 AppleLanguages
的值来更改语言
最佳答案
它有一个“hack”。您可以使用本地化文本加载您自己的 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]
关于iphone - 运行时更改 Three20 中的语言/本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8351445/