Cultureinfo: how to get only languagecode
I'm developing a Wind开发者_开发技巧ows 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.
Have you tried using the TwoLetterISOLanguageName
property?
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.
精彩评论