NSLocale currentLocale always returns "en_US" not user's current language
IphoneObjective CCocoaInternationalizationIphone Problem Overview
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);
Iphone Solutions
Solution 1 - Iphone
[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.
Solution 2 - Iphone
Instead of querying defaults directly using an undocumented key, ask the NSLocale class for the array of preferred languages.
Solution 3 - Iphone
To get the current device language use this instead:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
As described here: https://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios
Solution 4 - Iphone
For us the issue was that we were overriding the application language and application region in our dev scheme. Make sure that the Application Language is set to System Language in the scheme options (Edit Scheme -> Options).
Solution 5 - Iphone
for me, both
NSString *localeString = [[NSLocale currentLocale] localeIdentifier];
and
NSArray *array = [NSLocale preferredLanguages];
self.label.text = array[0];
yield the same result when you're in a simulator.
Solution 6 - Iphone
I had an issue where formatting month names came out in english on a device set to french language.
My solution was to use this:
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0] ];
[self.monthNameFormatter setLocale:locale];
[self.monthNameFormatter setDateFormat:@"MMMM"];
[self.monthNameFormatter stringFromDate:myDate];
Solution 7 - Iphone
i found that if i leave "en_US" out , but have a "en" localization that is a copy of "en_US" the simulator automagically starts respecting the language settings, but as soon as "en_US" is an option it always picks it regardless of the settings.
Solution 8 - Iphone
Question is too old, but this code may help to many:
The main.m should look like this:
NSString *localecode=[[NSLocale currentLocale] localeIdentifier];
localecode=[localecode substringToIndex:2]; //en_GB -> en
NSArray *arr = [NSLocale preferredLanguages];
NSMutableArray * mutable = [arr mutableCopy];
NSString *prefered = [mutable firstObject];
if(![localecode isEqualToString:prefered]){
if(![prefered isEqualToString:@"es"] && ![prefered isEqualToString:@"en"] && ![prefered isEqualToString:@"pt"]){
int index = [mutable indexOfObject:@"en"];
[mutable replaceObjectAtIndex:0 withObject:@"en"];
[mutable replaceObjectAtIndex:index withObject:prefered];
[[NSUserDefaults standardUserDefaults] setObject:mutable forKey:@"AppleLanguages"];
}
else{
int index = [mutable indexOfObject:localecode];
[mutable replaceObjectAtIndex:0 withObject:localecode];
[mutable replaceObjectAtIndex:index withObject:prefered];
[[NSUserDefaults standardUserDefaults] setObject:mutable forKey:@"AppleLanguages"];
}
}
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
What do this? If the current language of the device es Spanish, English or Portuguese use the the application uses the localized strings, by the other hand if the current language is not one of those and is not supported by application is set to English.
Solution 9 - Iphone
It shows the correct locale when on a real iPad. However, the simulator has it´s own iOS which by default is set to en-US. Go to the simulator iOS preferences and switch language & region to the one you want.
That does the trick.