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 nut
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).
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.
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];
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.
Instead of querying defaults directly using an undocumented key, ask the NSLocale class for the array of preferred languages.
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.