Bijington
Bijington

Reputation: 3751

How do you find the country code from NSLocale preferredLanguages?

I am in the process of developing an iOS application that needs to tell the server it communicates with the current language being used on the iPhone/iPad. I am currently using:

[[NSLocale preferredLanguages] objectAtIndex:0]

However because the App is a word based game in the case of English being used (en) this is not enough to go on, I need to be able to distinguish between en_GB and en_US (and potentially other languages in the future).

Currently I have two ways of doing this:

1) Use the currentLocale setting to get the current country code of the device and combine that with the preferredLanguage, however this is rather hacky.

2) Roll my own page to allow the user to change the language in App.

I am leaning towards option 2 but it will involve a fair chunk of work.

My question is, is there a better way of achieving this?

Upvotes: 2

Views: 906

Answers (1)

Clafou
Clafou

Reputation: 15410

iOS does include British English (en_GB), as opposed to US English (en), so in this particular example you could rely on the user's iOS language setting alone. If the user has selected British English and your application includes it in its list of localizations, then you should see en_GB using your line of code.

But of course, if you wish to support a language that iOS doesn't itself expose, you're stuck with having to provide your own picker.

Upvotes: 1

Related Questions