2个字母ISO国家/地区代码致CultureInfo [英] 2 Letter ISO Country Code to CultureInfo

查看:78
本文介绍了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天全站免登陆