简体   繁体   中英

How to get the main CultureInfo for a country in c#?

I'm working on a asp.net mvc application where i need to localize the currency format. so i decided to let the user choose the country from where i can use the RegionInfo to find the currency symbol and run the query on CultureInfo and set the first result as the culture for the user. Here is my code:

        public string GetPrimaryUICulture(string CountryCd) {
        CultureInfo Info;
        var _CultureInfox = new System.Globalization.RegionInfo(CountryCd);
        var _CultureInfo = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Where(c => c.Name.EndsWith(CountryCd)).ToList();
        if (_CultureInfo.Count == 0)
        {
            Info = new CultureInfo("en-US");
        }
        else
        {

            Info = _CultureInfo.Where(p => p.NumberFormat.CurrencySymbol == _CultureInfox.CurrencySymbol).FirstOrDefault();
           // Info = _CultureInfo.Where(p => p.TwoLetterISOLanguageName.Equals("en")).FirstOrDefault();
            if (Info == null)
            {
                Info = _CultureInfo.Where(p => p.TwoLetterISOLanguageName.Equals("en")).FirstOrDefault();
                if (Info == null)
                {
                    Info = _CultureInfo.FirstOrDefault();
                }
            }
        }
        return Info.Name;
    }

Now the problem is , if i set the CountryCd as "IN", the result i get from the above function is as-IN, for which i dont have the angularjs i18n file; what i have is hi-IN.

Another solution is to retrive the CultureInfo, that matches as en-{{CountryName}}, but there is a currency problem. For India, this works, but for Malaysia(en-MY), it gives $ instead of RM.

if things come to worse i have to hard code as : if IN then hi-IN, if MY, then ms-MY. if CH, then ZH-CN etc...which i really dont wanna do.

I hope you understand the problem i'm facing, sorry about my bad english, if you need further clarifications, please let me know.

While it's web application u can simply analyze HTTP language/locale headers and not try to guess it from custom field.

Most browsers in setup set valid locale at first place.

Detailed here:

http://matthewwittering.com/blog/internationalization-localization/http-headers.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM