Get NativeEnglishName from iso currency symbol without dependency to the current culture of logged in user

梦想与她 提交于 2019-12-12 13:30:59

问题


what I have is the currencyIsoCode "EUR". This Property can also be found in the

RegionInfo class => ri.ISOCurrencySymbol. But the RegionInfo class depends on the current logged in user.

What I want is to get the NativeEnglishName like "Euro" even when the user is from en-De, en-Us, cn-CN etc...

How can I do that?


回答1:


Awkward problem, there is no easy way to iterate the available RegionInfo. You'd have to iterate all available specific cultures and create their RegionInfo to compare. This code gets the job done:

public static string GetEnglishCurrencyName(string iso) {
  foreach (var c in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) {
    var reg = new RegionInfo(c.LCID);
    if (string.Compare(iso, reg.ISOCurrencySymbol, true) == 0)
      return reg.CurrencyEnglishName;
  }
  throw new ArgumentException();
}

Beware that this generates a lot of garbage. If you do this often, be sure to cache the region info in a Dictionary<> first.




回答2:


You can do:

string currencyName = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                   .Where(c => new RegionInfo(c.LCID).ISOCurrencySymbol == "EUR")
                   .Select(c => new RegionInfo(c.LCID).CurrencyEnglishName)
                   .FirstOrDefault();

which will return "Euro".

Note however that this gets the first RegionInfo that matches the currency symbol provided - not really an issue in this specific case, but could be if you are using the native currency name because the first country using the currency gets a match, which may have a different regional name for the currency than another country using the same currency (although probably not very likely).




回答3:


I notice too that the "CurrencyEnglishName" isn't the ISO Standard name either. e.g. RegionInfo where ISOCurrencySymbol = "GBP" returns "UK Pound Sterling"

However, the actual ISO Standard Name is simply "Pound Sterling".

I think the best way to go is to add the ISO values into a DB table (or as a config file/section if you want) and read from there.



来源:https://stackoverflow.com/questions/2469540/get-nativeenglishname-from-iso-currency-symbol-without-dependency-to-the-current

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!