Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

iphone - Runtime change the language/localization in Three20

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...