Cultural specific data annotations

前端 未结 2 1885
無奈伤痛
無奈伤痛 2021-01-21 02:34

I\'m trying to get cultural specific data annotations.

[DisplayFormat(DataFormatString = \"{0:d}\")]
public DateTime Date{ get; set; }

I though

2条回答
  •  感情败类
    2021-01-21 03:25

    You have to actually set the current culture, the MVC framework doesn't set it automatically (remember that a page's language might depend on factors like the URL, not just the browser settings.

    To set the culture, use Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture. You can get the browser settings from HttpRequest.UserLanguages (keep in mind that this value is just what comes through the HTTP request).

    Edit

    Per this answer, it seems there is a way to instruct ASP.Net to set the language based on the HTTP request header (set web.config globalization -> "culture" and "uiCulture" to "auto").

提交回复
热议问题