2 Letter ISO Country Code to CultureInfo

后端 未结 2 1331
梦如初夏
梦如初夏 2021-01-03 15:30

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

相关标签:
2条回答
  • 2021-01-03 15:51
    var isoCountryCode = "GB"; // Replace with the one you got from GeoLocation.
    var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures)
                              .Where(c => c.Name.EndsWith("-" + isoCountryCode )).First;
    

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

    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.

    0 讨论(0)
  • You can just search all cultures by 2-letter code:

    CultureInfo.GetCultures(CultureTypes.AllCultures)
               .Where(cu => cu.TwoLetterISOLanguageName == code);
    

    Just note that there may be multiple results.

    0 讨论(0)
提交回复
热议问题