1

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?

  • Only add one localization for the config file. –  Aug 20 '09 at 11:38
  • [This][1] can help you. It's the best way I think! Good luck. [1]: http://stackoverflow.com/questions/26594222/detect-localization-xcode-6-mac-cocoa –  Nov 05 '14 at 07:17

4 Answers4

2

Hmmm could it be that AppleLocale expects an 'en' and not an "en_US"?

'en' works for me in the terminal so... I don't see why it should not be the same string here for you as well when setting it in your app

defaults write com.mycompany.myproduct AppleLocale 'en'

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
1

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?

  • 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. –  Aug 20 '09 at 12:17
  • 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 Aug 20 '09 at 12:44
  • @Peter: yes, I do that myself. But that's not the use case that I inferred from the question. –  Aug 20 '09 at 16:13
1

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
  • 6,057
  • 4
  • 33
  • 47
0

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.

  • 2
    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. –  Aug 20 '09 at 12:37