countryCode and preferredLanguages are different

Hi



I am using Xcode 9.4 and testing our iOS application on real device (iOS 11.4 installed) for localizations. Our device region is Turkey and device language is Turkish. On this case;



[NSLocale preferredLanguages] -> tr-TR
[NSLocale currentLocale].countryCode  -> TR
[NSLocale currentLocale].languageCode  -> tr


When I change the language to Espanol LatinoAmerica, it becomes;



[NSLocale preferredLanguages] -> es-419, tr-TR
[NSLocale currentLocale].countryCode  -> TR
[NSLocale currentLocale].languageCode  -> es



And then I change the region of the device to Espanol, it becomes;



[NSLocale preferredLanguages] -> es-ES, tr-TR
[NSLocale currentLocale].countryCode  -> ES
[NSLocale currentLocale].languageCode  -> es


After this strange inconsistency, I change the region to Turkey again, it becomes weirdly;



[NSLocale preferredLanguages] -> es-TR, tr-TR
[NSLocale currentLocale].countryCode  -> TR
[NSLocale currentLocale].languageCode  -> es


I think there is a bug for preferredLanguages and 2nd or 4th case must have same results, but not. It creates unstable cases for the application. How can I pass this bug? Do I have to look countryCode and languageCode instead of preferredLanguages? Looking them save us from the buggy cases (on different regions or languages) or not?



On iOS 10+ devices the problem is the same also.



Thanks

Replies

How are you using these values? In what way is it causing problems for your app?


It's certainly no surprise that language and locale country are independent of each other. I agree with you that it's surprising that the 2nd and 4th cases differ.


When you changed the region to España, did that perhaps automatically change the language to plain Español (not the Latin American variant)?

Oh sorry, I forgot to mention about the case for us. We have to use the country and language codes on our request parameters for changing localization strings on our app and for making a translation automatically. This app has to know the device language for it at launch and we translate sth user writes to any language.


Yes they are independent, one of them gives us chosen languages for the user but the other is for device language. With this bug, on iOS devices, main languages can be chosen as a device language but sub-languages cant be chosen like that. They can be get from preferred languages. But yes 2nd - 4th cases have buggy case. They somehow match or correct language and its country's language together.


When I change country to Espana, device language doesnt change to Espanol, it keeps previous device language. If you change to Espanol, it is same with the 3rd case, no problem.


Do we have to always use countryCode, languageCode for using them on localization of all texts on our app? If we want to customize languages with more one, I think we can use preferred one? But again "es-419" case is a little strange thing.

Maybe it's different on iOS — I don't do iOS development — but the locale generally does not indicate the "device language" or anything about language at all. It indicates which conventions to use for formatting numbers, dates, currency, etc.


I don't understand what distinction you're trying to make between the user's language(s) and the "device language". I don't know what the device language even is (but I'm willing to confess ignorance). Why do you care for your app? Why can't you just use the user's language?

the locale generally does not indicate the "device language" or anything about language at all.

Right. If you want to tell your server to return strings that match the language that your app is running in, you should get the

preferredLocalizations
property of the main bundle.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"