2 字母 ISO 国家代码到 CultureInfo [英] 2 Letter ISO Country Code to CultureInfo

查看:26
本文介绍了2 字母 ISO 国家代码到 CultureInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Xamarin.Forms 应用程序,它正在获取 GeoLocation 的 2 个字母的 ISO 国家/地区代码.我需要该应用程序以当地风格显示货币.因此,如果我将手机从英国带到日本,它会显示以日元格式化的货币字段.

I have a Xamarin.Forms app that is obtaining a 2-letter ISO country code for GeoLocation. I need the app to display currency in the local style. So if I took my phone from the UK to Japan it would display currency fields formatted up in Yen.

格式化十进制货币的最直接方法是使用 CultureInfo 对象.

The most straight forward way to format a decimal currency is to use a CultureInfo object.

如何从 2 个字母的 ISO 国家/地区代码访问 CultureInfo 对象.我已经尝试实现这一目标好几天了,但找不到转换路径.

How can I get to a CultureInfo object from a 2-letter ISO country code. I have been trying to achieve this for quite a few days, but cannot find a path of conversions.

有什么指点吗?

推荐答案

var isoCountryCode = "GB"; // Replace with the one you got from GeoLocation.
var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures)
                          .Where(c => c.Name.EndsWith("-" + isoCountryCode )).First;

如果您查看 CultureCodes 列表由 microsoft 提供,您可以看到文化的前两个字母表示语言,后两个字母表示国家/地区.例如,对于日语,您将有ja"和ja-JP").

If you check the list of CultureCodes provided by microsoft you can see that the first two letters of the culture refer to language and the last two to country. For example, for japanese you would have "ja" and "ja-JP").

因此,如果您在文化列表中搜索以您想要的国家/地区代码结尾的文化,您应该会获得您想要的国家/地区所有可用 CultureInfo 的列表.

Therefore if you search through the list of cultures for the ones that end with your desired country code, you should get a list of all available CultureInfo for your desired country.

然后您可以再次浏览它们以找到用户选择的语言(如果有)或任意选择一种.

Then you can either go through them again to find the one for the users selected language (if available) or pick one arbitrarily.

这篇关于2 字母 ISO 国家代码到 CultureInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆