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

Hello,

Please use the following:

NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];

I hope that helps!

Hi,


Thank you for your help.


I also have tried this and does not give the information I want, the answer is "pt_PT"


Thanks,

JFontes

>set to the location in Portugal "PT"

Did you mean 'region' (for currency/date formats, etc.)...?

If testing the language returns PT, then that is the language set on the device...sounds like you didn't really set the device to EN? Did you delete old versions being tested, option-clean the build and then test to a device configured for language EN?

Hi,


Thank you for your help.


I could not find a way to insert a photo of the screen, but the settings I have is:


- Setting - General - International

-> Language = English

-> Voice Control = English

-> Keyboards -> 3

-> Region Format = Portugal

-> Calendar = Gregorian


Thanks,

JFontes

Which iOS are you testing? I don't see the same settings you do, sorry.

Hi,


iOS 7.1.2 and 8.4, the same result 😢


Thanks,

JFontes

The "system language" is of a tricky concept. In most cases what you what is the language your app is running in, and that's available via the main bundle's

preferredLocalizations
property.

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.


I want to change the language of my App, based on the iPhone's language, that's what I'm not getting, the liguagem that is selected in the iPhone settings.


Thanks,

JFontes

If you app is properly localised, the system will automatically do the right thing, that is, use the user's configured language settings to find the first localisation that's supported by your app.

Share and Enjoy

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

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

Hi eskimo,


I only find pt_PT or PT, but the iPhone setting are Language = English


NSString* userLinguagem = [[[NSLocale currentLocale] localeIdentifier] substringToIndex:2];


Thanks,

JFontes

Alas, you're really off in the weeds here.

+[NSLocale currentLocale]
returns the current locale, that is, the value set by Settings > General > Language & Region > Region Formats. It has nothing to do with the language that your app is running in. It's perfectly reasonable, and in fact quite common, for users in the field to have their locale and language set to 'conflicting' values. For example, a native English speaker living in France would have the language set to English but might choose to set the locale to French (so they get metric weights and measures, 24 time, and so on).

The language that your app runs in is determined by the language setting, that is, Settings > General > Language & Region > Preferred Language Order. When the system runs your app it takes this list of languages (the preferred list) and matches it against the list of languages that your app is localised into (the app list). The first language in the preferred list that exists in the app list is the language chosen for the app. This is what you'll find in the first entry of the main bundle's

preferredLocalizations
array.

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.


JFontes

Hi eskimo,


Still does not work properly!

I changed the language on iPhone (7.1.2) from English to Portuguese and continue to give "en"!


Help.

JFontes

I changed the language on iPhone (7.1.2) from English to Portuguese and continue to give "en"!

What continues to give "en"? The last post I made describes two mechanisms, so I need know which one you're actually testing.

Share and Enjoy

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

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

Hi eskimo,


[[NSBundle mainBundle] preferredLocalizations]


Only gives me one object and is "en", has set the iPhone with the language in EN or PT


Thanks,

JFontes

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

Get the system language
 
 
Q