如何覆盖一些默认的CurrentCulture值

时间:2012-09-26 08:19:53

标签: asp.net-mvc cultureinfo

我需要覆盖当前(或指定的)CurrentCulture对象的默认DayNames。

我试图通过以下几种方式实现这一目标:

1)在Global.asax级别:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) {
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(XXX);
    string[] newNames = { YYY };
    System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames = newNames;
}

2)在控制器级别:

public HomeController() {
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(XXX);
    string[] newNames = { YYY };
    System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames = newNames;
}

正确应用指定的“CurrentCulture”对象(根据指定的XXX文化名称)。我可以通过@DateTime.Now.ToString("ddd")表达式查看特定于文化的日期名称。 但是,指定的“DayNames”对象似乎被忽略(但是,我可以看到“CurrentCulture”和“DayNames”属性都在调试器中正确应用)。

1 个答案:

答案 0 :(得分:0)

有必要改写默认的AbbreviatedDayNames:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) {
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(...);
    System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.AbbreviatedDayNames = ...;
}

@DateTime.Now.ToString("ddd")