Languages & Locales are different

If you’re writing a Mac or iPhone application that supports localization, you should be aware that the language & locale could be different. The preferred language can be changed in the International preference pane by changing the order of languages in the list.

International.jpg

However, when you change the language order, it doesn’t change the locale, which defines the date, time, and currency formats.

In your application, you can find the preferred language using this bit of code (note that I’m using functions that are available in 10.3 or later):

CFArrayRef locs = CFBundleCopyBundleLocalizations(CFBundleGetMainBundle());
CFArrayRef preferred = CFBundleCopyPreferredLocalizationsFromArray(locs);
CFStringRef lang = CFLocaleCreateCanonicalLocaleIdentifierFromString(NULL,CFArrayGetValueAtIndex(preferred, 0));

With French selected as the preferred language, as shown above, it will return ‘fr’ as expected. However, you’re working with a web service that takes a locale name instead of a language, so you do something like this to obtain the locale:

CFLocaleRef locale = CFLocaleCopyCurrent();
CFStringRef lstr = CFLocaleGetIdentifier(locale);

The locale you get is ‘en_US’, which may seem counter-intuitive. If you pass that to your web service, instead of French language text, it will appear in English.

Leave a Reply