views:

61

answers:

2

Changing the language of an iPhone app has been discussed many times, but I'm stuck with this one: The provided solutions require the app to be restarted to make the changes take effect. But how do you achieve this on an iPhone 4? Pressing "home" sends the app to the background and clicking the app icon just brings it back. The user has experience as if the app had been restarted and wonders why the language hasn't changed. Thanks for your help.

A: 

Take a look at Apple's sample code project AppPrefs. It shows how to handle changes made in the Settings app when bringing your app back to the foreground. The sample code handles settings specific to the app, but could also be used for 'system-wide' setting changes. Hope this helps.

petert
Thanks for the advice. Unfortunately that didn't work for me: Listening to NSUserDefaultsDidChangeNotification works, but I don't see how to make NSLocalizedString use the new language. Changing the language in my app causes the AppleLanguages array to be rebuilt:[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:[language isoCode]] forKey:@"AppleLanguages"];But I see the changes only after restarting the app, not when it comes back to the foreground.
Oliver
A: 

You could write your own replacement for NSLocalizedString. In the header file, do something like

#undef NSLocalizedString
#define NSLocalizedString(key, comment) MyLocalizedStringFunction(key)

In your function, load the correct .lproj bundle using NSBundle, then call

[bundle localizedStringForKey:key value:nil table:nil];

on it to get the string you want.

I don't know if that's the best way: we had to use it because we wanted to support a language that didn't exist on the iPhone, so your problem may have a simpler solution.

Amorya
That worked perfect for me. Thanks a lot for the advice.
Oliver