Cultureinfo: how to get only languagecode

2.1k Views Asked by At

I'm developing a Windows Phone app.

How can I get the language code from CultureInfo.CurrentCulture?

I'm using CultureInfo.CurrentCulture.Name and I getting 'en-US'. I only need en.

2

There are 2 best solutions below

1
On BEST ANSWER

Have you tried using the TwoLetterISOLanguageName property?

1
On

I'm not sure exactly what you are trying to achieve. If all you want is to remove the region, retaining the script distinction (if you are interested in zh-Hans for example and not just zh) then you will want to use the Parent property (). Though this can return legacy (zh-CHS) so you would want to use the IetfLanguageTag property to resolve that:

CultureInfo.CurrentCulture.Parent.IetfLanguageTag

en-US -> en
zh-CN -> zh-Hans
zh-TW -> zh-Hant

Sometimes it still isn't going to give you the expected answer since it will only language tags that are supported (but this isn't any different from the TwoLetterISOLanguageName property):

az-Cyrl-AZ -> az
az-Latn-AZ -> az

And it seems like some of the chains were omitted:

sr-Cyrl-BA -> (Invariant)

You can check for invariant and then return the TwoLetterISOLanguageName property to work around that.