views:

291

answers:

3

How can you make your cocoa app use a specific locale / localization thats different from what the current locale says?

My app has a number of localizations and I would like to be able to choose the localization the app uses in a config file. How can I tell Cocoa which of the localizations to use?

+1  A: 

Set the AppleLanguages array in the application's user defaults to contain the one you'd prefer to use, and set an appropriate AppleLocale string in the defaults too. Having said that, why use a localisation which isn't the one the user prefers?

Graham Lee
The concept of a "user" is misleading for this app, as it assumes the user owns the machine. This is a kiosk style app run on my companies hardware, which is always set to the same locale. There is a central configuration which dictates all settings, including the GUI language.
Prof. MAAD
One reason is to test localization changes, such as to make sure a string received from a localizer fits into the control where it's displayed in the UI, or to make sure you didn't break anything while repeating a non-string edit across multiple localizations of a nib.
Peter Hosey
@Peter: yes, I do that myself. But that's not the use case that I inferred from the question.
Graham Lee
A: 

I tried setting "AppleLocale" and "AppleLanguages" with something like the following:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:@"en_US" forKey:@"AppleLocale"];

It did nothing though, still the language and locale from the system preferences are used.

Prof. MAAD
Seems to work, but only after restarting the program.Meaning: First time the program starts with changed settings it sets the user defaults using the above code. Subsequent calls to NSLocalizedString dont use the newly set language/locale though, until I restart the program.
Prof. MAAD
A: 

according to MAAD's answer. I tested in my app and found the only key that affects is AppleLanguages

so

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];

is enough.

Remember to restart your app to make user defaults take effects.

xhan