Get the system language

Hi,


I have an iOS device (iPhone) set to the location in Portugal "PT" and the system language in English "EN".

When I try to know the language that is configured on iPhone


[[[NSLocale currentLocale] localeIdentifier] substringToIndex:2]


the answer is "PT" and not "EN"!

Can anyone help me how I can properly get the language that is configured in the system settings?


Thank you for your help.

JFontes

Replies

Have you had a look at the internationalization guide?

https://developer.apple.com/library/prerelease/watchos/documentation/MacOSX/Conceptual/BPInternational/InternationalizingYourCode/InternationalizingYourCode.html


From the documentation for preferredLocalizations:

An array of NSString objects containing language IDs for localizations in the bundle. The strings are ordered according to the user's language preferences and available localizations.


When I read that, I get the impression that if you only have "en" localization in your application bundle, then you're only going to get "en" back.

When I read that, I get the impression that if you only have "en" localization in your application bundle, then you're only going to get "en" back.

Right, that's the way I interpret things as well.

Share and Enjoy

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

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

"Hi eskimo,


My App is in English and Portuguese, the primary language is English, I can change the language of my App without problem, everything is good and proper. What I want is to know what is the language that is configured the iPhone system (setting), after I know that I change my App to English or Portuguese. My App works well to detect the language and changes automatically, the problem I detected was that I am seeking localization and this leads into error when it comes to location in Portugal but the iPhone is set to English. There has to be a way to know the settings that are on the iPhone without being compared to the languages of my app have, I think that's what makes "[[NSBundle mainBundle] preferredLocalizations]". The link that you sent me is more about the internationalization of the App, that I have correct.


Sorry my bad English (I not who write the English part of my App, lol).


Thanks,

JFontes

I'm sorry, I'm still really confused as to your goals. I suspect that there's some subtlety here that, ironically, is not making it across the language barrier.

If we walk through some specific cases, perhaps you can tell me what you're looking for:

  • Your app is localised into English and Portugese. One of these, it doesn't matter which, is your development language.

  • If the user's preferred language is Portugese, you launch in Portugese. That's good.

  • If the user's preferred language is English, you launch in English. That's also good.

  • If the user's preferred language is neither Portugese nor English, you launch in your development language. That's less than ideal, but the best you can do.

What other case are you trying to solve?

Share and Enjoy

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

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

Hi eskimo,


Once again I apologize for my English, I try to be simple and direct.


iPhone with iOS 7.1.2


With:

- Setting - General - International

-> Language = English

-> Voice Control = English

-> Keyboards -> 3

-> Region Format = Portugal

-> Calendar = Gregorian


I have:

[[NSLocale currentLocale] localeIdentifier] -> pt_PT

[[NSBundle mainBundle] preferredLocalizations] -> one object with pt-PT


With:

- Setting - General - International

-> Language = Português (Portugal)

-> Voice Control = Português

-> Keyboards -> 3

-> Region Format = Portugal

-> Calendar = Gregorian


I have:

[[NSLocale currentLocale] localeIdentifier] -> pt_PT

[[NSBundle mainBundle] preferredLocalizations] -> one object with pt-PT



iPhone with iOS 8.4

With:

- Setting - General - Language & Region

-> iPhone Language = English

-> Preferred Language Order = English, Português (Portugal)

-> Keyboards -> 3

-> Region = Portugal

-> Calendar = Gregorian

-> Advanced = Automatic - On


I have:

[[NSLocale currentLocale] localeIdentifier] -> en_PT (!?)

[[NSBundle mainBundle] preferredLocalizations] -> one object with pt-PT


With:

- Setting - General - Language & Region

-> iPhone Language = Português (Portugal)

-> Preferred Language Order = Português (Portugal), English

-> Keyboards -> 3

-> Region = Portugal

-> Calendar = Gregorian

-> Advanced = Automatic - On


I have:

[[NSLocale currentLocale] localeIdentifier] -> pt_PT

[[NSBundle mainBundle] preferredLocalizations] -> one object with pt-PT



I can not correctly know the iPhone Language !?


Thanks,

JFontes


Update:

The first time I run the app, these method give the correct information, and keep it.

Then give me the same information, change or not the iPhone language.


Update #2: I found the solution:

[[[NSLocale preferredLanguages] objectAtIndex:0] substringToIndex:2] gives a NSString* with the first two letters of the language that is configured iPhone.

BUT not save the language into standardUserDefaults with key @"AppleLanguages".


Thanks to everyone who helped me.

JFontes

Once again I apologize for my English [...]

No worries; it's better than my Portugese, or any other language for that matter )-:

My theory here is that the system has not picked up that your app is meant to be available in English. Specifically, in cases 1 and 3 -preferredLocalizations is coming back as Portugese because the system doesn't think your app can do English.

Have you looked at QA1828 How iOS Determines the Language For Your App? That explains, in some detail, how iOS decides what langage to run your app in.

Share and Enjoy

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

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

Hi eskimo,


Thank you for your help.


Now I have everything ok.


JFontes