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
591 views
in Technique[技术] by (71.8m points)

localization - language change only after restart on iphone

I'm trying to change app language, but when I run this code in main.h language chages after I shut down an App and run it again. Is this possible to change language without restarting?

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSArray *languages = [NSArray arrayWithObject:@"en_GB"];
    [[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Update the answer "How to change the languages within the app"

NSLocalizedString() (and variants thereof) access the "AppleLanguages" key in NSUserDefaults to determine what the user's settings for preferred languages are. This returns an array of language codes, with the first one being the one set by the user for their phone, and the subsequent ones used as fallbacks if a resource is not available in the preferred language.

You can override the global setting for your own application if you wish by using the setObject:forKey: method to set your own language list just like you've done it. This will take precedence over the globally set value and be returned to any code in your application that is performing localization. The code for this would look something like:

[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"]; 

Note: To be on safe side make sure that you use the appropriate pre-defined languages name.

Below is the code snippet, but you MUST have all localization files in your project.

@implementation LocalizeLanguage

static NSBundle *bundle = nil;

+(void)initialize {
     NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
     NSArray* languages = [defs objectForKey:@"AppleLanguages"];
     NSString *current = [[languages objectAtIndex:0] retain];
     [self setLocalizeLanguage:current];
}

/*
  [LocalizeLanguage setLocalizeLanguage:@"en"];
  [LocalizeLanguage setLocalizeLanguage:@"fr"];
*/

+(void)setLocalizeLanguage:(NSString *)lang {
     NSLog(@"preferredLang: %@", lang);
     NSString *path = [[ NSBundle mainBundle ] pathForResource:lang ofType:@"lproj" ];
     bundle = [[NSBundle bundleWithPath:path] retain];
}

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

@end

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

...