Get language and country code (ISO 639-1)

3.3k views Asked by At

Is there a solution for xamarin-ios to get the current culture in the format like (en_US, de_CH, ..)? I read so many articles in the web and here on stackoverflow. But I can't find a nice solution for this.

Following code snippet returns not this format everytime. Sometimes I get "gsw_DE" and so on (ISO 639-2 instead of 639-1)

NSLocale.CurrentLocale.Identifier;

My current solution is to do it by myself:

var languagecode = NSLocale.PreferredLanguages[0];

var result = String.Format("{0}_{1}", languagecode.Substring(0, 2), NSLocale.CurrentLocale.CountryCode);

Above workaround is from: Get iOS current language (including country code)

I need to make sure that I get the value in this format. Is there another way? Thanks

1

There are 1 answers

3
Artur Shamsutdinov On

We are still in the C# world, aren't we? You can use CultureInfo.CurrentUICulture.Name and it'll give you "en-US" for "default" English and "en-GB" for English UK. Language settings are in General - Language & Region - iPhone Language.