修改现有文化?

时间:2010-10-12 08:21:07

标签: c# .net

 CultureInfo culture = new CultureInfo("en-US");

        culture.DateTimeFormat.DateSeparator = "/";
        culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";

        //dekadikoi arithmoi
        culture.NumberFormat.NumberDecimalSeparator = ".";
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

因为它需要在任何地方工作!问题是,new CultureInfo("en-US");所有其他特定于计算机的设置都被忽略了...... 有没有办法复制CurrentCulture? 我试图修改当前的文化,但我只读了错误...

5 个答案:

答案 0 :(得分:14)

CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();

culture.DateTimeFormat.DateSeparator = "/";
culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";

//dekadikoi arithmoi
culture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

您可能希望使用:

CultureInfo culture = (CultureInfo)CultureInfo.CurrentUICulture.Clone();

代替。

答案 1 :(得分:4)

只需更改一行:

CultureInfo culture = CultureInfo.GetCultureInfo("en-US");

答案 2 :(得分:1)

这是怎么回事: CultureInfo culture=CultureInfo.CurrentCulture;

答案 3 :(得分:1)

只需获取CurrentCulture课程的副本,然后根据需要对其进行修改。如果确实需要更改线程本身的CultureInfo(而不是使用副本),则需要为代码提供安全权限并将ControlThread属性设置为true。 (例如,参见链接)

答案 4 :(得分:0)

根据Jon Hanna的解决方案,我建议从特定文化开始,因为CurrentCulture可以设置为任何内容。

public static CultureInfo GetCustomCulture(string baseCulture = "en-ZA", char numberDecimalSeperator = '.', char listSeperator = ',') {
    var culture = CultureInfo.GetCultureInfo(baseCulture).Clone();

    culture.NumberFormat.NumberDecimalSeparator = numberDecimalSeperator + "";
    culture.TextInfo.ListSeparator = listSeperator + "";

    return culture;
}

然后将此行添加到Program.cs

的开头
System.Globalization.CultureInfo.CurrentCulture = GetCustomCulture();