VansFannel
VansFannel

Reputation: 45941

Cultureinfo: how to get only languagecode

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.

Upvotes: 2

Views: 2218

Answers (2)

Eric MSFT
Eric MSFT

Reputation: 3276

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503140

Have you tried using the TwoLetterISOLanguageName property?

Upvotes: 4

Related Questions