简体   繁体   English

如何在C#中获取某个国家的主要CultureInfo?

[英]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. 我正在研究需要本地化货币格式的asp.net mvc应用程序。 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. 因此,我决定让用户选择可以使用RegionInfo查找货币符号的国家/地区,并在CultureInfo上运行查询,并将第一个结果设置为用户的文化。 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; 现在的问题是,如果我将CountryCd设置为“ IN”,则从上述函数中得到的结果是as-IN,为此我没有angularjs i18n文件; what i have is hi-IN. 我所拥有的是嗨。

Another solution is to retrive the CultureInfo, that matches as en-{{CountryName}}, but there is a currency problem. 另一个解决方案是检索与en-{{CountryName}}匹配的CultureInfo,但是存在货币问题。 For India, this works, but for Malaysia(en-MY), it gives $ instead of RM. 对于印度,这有效,但对于马来西亚(en-MY),它给出$而不是RM。

if things come to worse i have to hard code as : if IN then hi-IN, if MY, then ms-MY. 如果情况变得更糟,我必须硬编码为:如果输入IN,然后输入Hi-IN,如果输入MY,则输入MS-MY。 if CH, then ZH-CN etc...which i really dont wanna do. 如果CH,然后ZH-CN等...我真的不想做。

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. 在Web应用程序中,您可以简单地分析HTTP语言/区域标头,而不必尝试从自定义字段中猜测它。

Most browsers in setup set valid locale at first place. 安装程序中的大多数浏览器会首先设置有效的语言环境。

Detailed here: 详细在这里:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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