多國語系-MVC Client端的數值驗證訊息文字

  繼前一篇多國語言-MVC Client端的數值驗證,此篇是關於預設的數值錯誤訊息預設.netFramework裝完後會有系統預設的語系訊息,以我裝的是繁體中文為例,如下圖我故意將數字欄位輸入英,會出現中文的錯誤訊息
ps:這不是Require的錯誤訊息,我一開始誤以為是..

image

而其他國家的語系就會全部顯示英文訊息 ps:因找不到.net語系檔預設全用英文

image

問題

若我想要這些系統預設的訊息跟著使用者的語系顯示該語系的訊息該如何作?


解決方法

  1. 在App_GlobalResources加入語系檔並新四個預設的Resource
    ResourceName Value Remark
    FieldMustBeDate The field {0} must be a date. 日期格式訊息
    FieldMustBeNumeric The field {0} must be a number. 數值格式訊息
    PropertyValueInvalid The value '{0}' is not valid for {1}. 不合法格式訊息
    PropertyValueRequired A value is required. 必要輸入訊息

    範例:加入一個FieldMustBeNumeric為簡体中文
    image
  2. 在Global.asax指定ResourceClassKey ps:”Resources”為Resources.resx的檔名
        protected void Application_Start()
        {
            DefaultModelBinder.ResourceClassKey = "Resources";
            ClientDataTypeModelValidatorProvider.ResourceClassKey = "Resources";
        }
ps:PropertyValueRequired需另外設定
http://stackoverflow.com/questions/12545176/mvc-4-ignores-defaultmodelbinder-resourceclasskey/12545997

顯示結果

image


參考文章
http://weblogs.asp.net/imranbaloch/archive/2013/03/31/localizing-default-error-messages-in-asp-net-mvc-and-web-form.aspx

另一篇值得參考的自訂RequireAttributeAdapter
http://stackoverflow.com/questions/16789961/localization-in-asp-net-mvc-4-using-app-globalresources

這個網誌中的熱門文章

IIS 設定只允許特定IP進入