views:

2729

answers:

2

I'm in the processes of internationalizing an iPhone app - I need to make programmatic changes to certain views based on what the user's current locale is. I'm going nuts because no matter what the language preference on the iPhone simulator or actual hardware are, locale always evaluates to "en_US":

NSString *locale = [[NSLocale currentLocale] localeIdentifier];
NSLog(@"current locale: %@", locale);

The crazy thing is that the rest of the application behaves as expected. The correct strings are selected from the Localization.strings file and used in the interface, and the correct .xib files for the selected locale are used.

I have also tried the following, to no avail and with the same result:

NSString *locale = [[NSLocale autoupdatingCurrentLocale] localeIdentifier];
NSLog(@"current locale: %@", locale);

Is there something simple I'm missing? A preference or an import perhaps?

What I used to do:

As Darren's answer suggests, the preference I'm looking for is not in NSLocale, rather it is here:

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];
NSLog(@"preferredLang: %@", preferredLang);

Peter's answer seems to be a better solution:

NSArray* preferredLangs = [NSLocale preferredLanguages];
NSLog(@"preferredLangs: %@", preferredLangs);
+5  A: 

[NSLocale currentLocale] is based on the device's Region Format settings, not the language. If the region is set to United States you will get en_US regardless of which language you're using.

Darren
Thanks Darren, that got me started, I also figured out how to get the current language setting, which is what I was really trying to do.
Prairiedogg
Hi Prairiedogg,how did you get the current language setting? i have the same problem also..all returns EN value for language.
ZaldzBugz
+3  A: 

Instead of querying defaults directly using an undocumented key, ask the NSLocale class for the array of preferred languages.

Peter Hosey