Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLocale currentLocale always returns "en_US" not user's current language

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* userDefaults = [NSUserDefaults standardUserDefaults]; NSArray* languages = [userDefaults objectForKey:@"AppleLanguages"]; NSString* preferredLanguage = [languages objectAtIndex:0]; NSLog(@"preferredLanguage: %@", preferredLang); 

Peter's answer seems to be a better solution:

NSArray* preferredLanguages = [NSLocale preferredLanguages]; NSLog(@"preferredLanguages: %@", preferredLanguages); 
like image 207
prairiedogg Avatar asked Oct 05 '09 20:10

prairiedogg


People also ask

What is Currentlocale?

A locale representing the user's region settings at the time the property is read.

What is NSLocale?

An object representing information about linguistic, cultural, and technological conventions that bridges to Locale ; use NSLocale when you need reference semantics or other Foundation-specific behavior.


1 Answers

[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.

like image 129
Darren Avatar answered Sep 18 '22 21:09

Darren