Runtime change the language/localization in Three20

霸气de小男生 提交于 2019-12-29 06:29:10

问题


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

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


回答1:


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.

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

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

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

#import "AppDelegate.h"

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


来源:https://stackoverflow.com/questions/8351445/runtime-change-the-language-localization-in-three20

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!