ri.ISOCurrencySymbol. But the RegionInfo class depends on the current logg开发者_JAVA技巧ed in u" />
开发者

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

开发者 https://www.devze.com 2022-12-24 09: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 logg开发者_JAVA技巧ed in u

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 logg开发者_JAVA技巧ed 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?


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.


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消