How to force culture/region on RegionInfo.DisplayName

别来无恙 提交于 2019-12-11 17:08:33

问题


I'm displaying a country as part of some other information. The country is read out of a database as a country code.

This is done like so:

Location location = new Location()
{                                
    Company = reader.GetString("Company"),
    Address1 = reader.GetString("Address"),
    Address2 = reader.GetString("Address2", string.Empty),
    ZipCity = reader.GetString("ZipCity"),
    Country = new RegionInfo(reader.GetString("Country", string.Empty)).DisplayName,
    CountryCode = new RegionInfo(reader.GetString("Country", string.Empty)).TwoLetterISORegionName,
    Att = reader.GetString("Att", string.Empty),
    Phone = reader.GetString("Phone", string.Empty)
};

My problem is that I would really like to force the display name to be in danish. Mind you, the country will now always be denmark, so using native name is not an option.

Thank you very much in advance


回答1:


From MSDN:

The DisplayName property displays the country/region name in the language of the localized version of .NET Framework. For example, the DisplayName property displays the country/region in English on the English version of the .NET Framework, and in Spanish on the Spanish version of the .NET Framework.

So, if you install the Danish version of .NET, it should work as you want.

However, it may be better to not depend on that and just create your own table of Danish country names.




回答2:


So after checking into the possibility of installing a language pack for the .NET Framework, which for some reason wasn't possible for us, we found the following solution.

Add this line to your web.config under the system.web node.

<globalization enableClientBasedCulture="false" culture="da-DK" uiCulture="da"/>

If you have a OS language pack installed on the machine running your software, it will now be forced to that language.




回答3:


You can solve that by writing that line before instanciating RegionInfo :

Thread.CurrentThread.CurrentUICulture = new CultureInfo("da-DK");


来源:https://stackoverflow.com/questions/28408154/how-to-force-culture-region-on-regioninfo-displayname

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