NSLocale returning wrong value in iOS 11
Asked Answered
W

1

8

Starting from iOS 11.0, the following code returns "de_US" instead of "en_US":

// => Returns "de_US"
NSString *regionCode = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
NSLog(@"Region code: %@", regionCode);

Below iOS 11, it returns "en_US".

My device has for language and region English / United States. Preferred languages (despite I do not use them in my code) are in order:

  1. English
  2. Deutsch
  3. French

Is it a known issue of iOS 11? Has the API changed?

Worl answered 6/12, 2017 at 14:39 Comment(2)
Does this happen on a device or in the simulator? And furthermore: the app has an English localization?Megaspore
It happens on a device: iPhone 6 Plus. App has only one localization: "Localizable.strings" file in Xcode target. Values are in German in this file, but I didn't specified it was German file in Xcode. So I'm wondering where this "de_" comes from.Worl
W
16

Found it!

It's a change behaviour starting from iOS 11.

Under iOS 11, [NSLocale currentLocale] only returns languages supported by your app’s localizations. If your app only supports English (as the base localization), then no matter what language the user selects on the device, currentLocale will always return English.

Under iOS 10 and earlier, currentLocale would directly represent the user’s chosen language and region, regardless of what localizations your app supports.

More information here:

Worl answered 6/12, 2017 at 16:23 Comment(5)
If those localisations are the same then I guess your application is entirely about angst, kindergartens and schadenfreude? Jokes aside, Apple still documents .current/-currentLocale to be a function only of "the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified" but notes that a locale represents "linguistic, cultural, and technological conventions", not merely a language. So are you using kilometres, the 24-hour clock, normal dd/mm/yyyy date display, etc?Louque
Please note that if both your German and English Localizable.strings files are exactly the same then you are going to have very upset users. The App Store will claim that your app supports both German and English. But since you didn't actually localize both strings files (you stated they were the same), then both German and English speaking users are going to see which ever language you actually have (I can't tell whether you have both files in German or English).Incongruent
You are right, I didn't notice German AND English would be listed as supported on the store. Which is actually false as both of the translations are the same... Saw your stackoverflow link. So it's truly a change of iOS 11. iOS will detect that I only have German, and so it will always return German... Don't know what to do here.Worl
I am currently facing the same problem in one of my Today Widgets. My App is localized in many different languages but the widget keeps showing the EN text on my DE device. Any idea?Hemipterous
how about iOS 14?Subbase

© 2022 - 2024 — McMap. All rights reserved.