Cultureinfo:如何仅获取语言代码
我正在开发一个 Windows Phone 应用程序。
如何从 CultureInfo.CurrentCulture 获取语言代码?
我正在使用 CultureInfo.CurrentCulture.Name,并且得到“en-US”。我只需要 en.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过使用
TwoLetterISOLanguageName
属性?Have you tried using the
TwoLetterISOLanguageName
property?我不确定你到底想要实现什么目标。如果您想要的只是删除该区域,保留脚本区别(例如,如果您对 zh-Hans 感兴趣而不仅仅是 zh),那么您将需要使用 父 属性 ()。尽管这可能会返回旧版本(zh-CHS),因此您可能需要使用 IetfLanguageTag 属性来解决这个问题:
CultureInfo.CurrentCulture.Parent.IetfLanguageTag
有时它仍然不会给你预期的答案,因为它只会支持支持的语言标签(但这不是任何与 TwoLetterISOLanguageName 属性不同):
并且似乎省略了一些链:
您可以检查不变性,然后返回 TwoLetterISOLanguageName 属性来解决该问题。
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
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):
And it seems like some of the chains were omitted:
You can check for invariant and then return the TwoLetterISOLanguageName property to work around that.