views:

904

answers:

2

Is there a way to change the application language during runtime?

So, after the change NSLocalizedString immediately returns the string for the new language.

What I'm doing now is changing the language using the code below:

- (void)onChangeLanguage: (id)sender {
 NSArray *lang = [NSArray arrayWithObjects:((InfoWhatever *)sender).language, nil];
 [[NSUserDefaults standardUserDefaults] setObject:lang forKey:@"AppleLanguages"];
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
 NSString *currentLanguage = [languages objectAtIndex:0];

 NSLog(@"Current language: %@", currentLanguage);
}

The language will change but only after restarting the app.

A: 

I doubt you can do this, even the Settings app cannot do it.

(When you change the language in the Settings app, the screen goes black, and displays "setting language..." and a progress wheel. After a long wait, you are back in Springboard. It almost looks like the phone reboots.)

oefe
Okay then, the reboot sounds acceptable. Is there any apple policy against quitting?
shinjin
I agree with your suppositions - but this is not an answer. Do you have any information about performing a runtime language change?
Brandon
@Brandon: Why the downvote? You may not like the answer, but it's how things are. Or do you know it better? Then why don't you show us your better answer?
oefe
Again, you did not provide an answer. You made a guess.This is an answer:http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language
Brandon
@Brandon: that's an answer to a different question. And the wording of my answer makes it obvious enough that it is based on assumptions and observations, not on some first-hand information (which I would have cited, if I had it). And nobody provided a different (better) answer, so I still think it's the correct answer.
oefe
Yes, that is a different question. Check out the answer, the answerer clearly had some first-hand knowledge of the subject matter - even if the question was not completely resolved. Your answer provides absolutely no useful information. In addition to being simply wrong, the assertion that 'the Settings app cannot do it' is misleading. Why does it matter that the settings app does not (as opposed to cannot) change the presentation language at runtime? The use case that the asker is pursuing does not require redisplaying localized icons or even involve other applications. The answer is inane.
Brandon
@Brandon: why don't you share *your* answer with us?
oefe
It took quite a while for me to devise a solution. I actually ended up using the 'NSLocalizedStringFromTable' macro and the 'AppleLanguages' setting to set and check language settings. I'll write up an actual answer if I have the chance. Maybe you should delete this answer? Seeing an inane yet accepted answer doesn't exactly inspire anyone to spend a whole lot of effort.
Brandon