2013-05-28 29 views
7

我試圖完成兩兩件事:本地化ASP.NET MVC 4使用App_GlobalResources文件

  1. 本地化「內置的」錯誤消息「FieldMustBeDate」和「FieldMustBeNumeric」。
  2. 本地化您可能遇到的其他一些錯誤消息,例如「PropertyValueRequired」。

通過使用問題1的http://forums.asp.net/t/1862672.aspx/1和問題2的MVC 4 ignores DefaultModelBinder.ResourceClassKey我設法讓本地工作。

但是,只要我發佈到網站,「內置」錯誤消息默認返回英語,而其他錯誤消息保持本地化。

我已經讀了幾個地方,應該避免使用App_GlobalResources,但是我無法完成問題1而不使用它。

我已經創建了名爲「WebResources.resx」的.resx文件,將生成操作設置爲「嵌入」,將複製到輸出目錄設置爲「不要複製」,將自定義工具設置爲「PublicResXFileCodeGenerator」並將自定義工具名稱空間設置爲「資源」。 項目本身被設置爲只發布需要的文件。

我的Global.asax.cs包含以下(相關)代碼:

ClientDataTypeModelValidatorProvider.ResourceClassKey = "WebResources"; 
    DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(RequiredAttribute), 
    typeof(MyRequiredAttributeAdapter)); 

和類MyRequiredAttributeAdapter包含以下代碼:

public class MyRequiredAttributeAdapter : RequiredAttributeAdapter 
{ 
    public MyRequiredAttributeAdapter(
     ModelMetadata metadata, 
     ControllerContext context, 
     RequiredAttribute attribute 
    ) 
     : base(metadata, context, attribute) 
    { 
     if (attribute.ErrorMessageResourceType == null) 
     { 
      attribute.ErrorMessageResourceType = typeof(Resources.WebResources); 
     } 
     if (attribute.ErrorMessageResourceName == null) 
     { 
      attribute.ErrorMessageResourceName = "PropertyValueRequired"; 
     } 
    } 
} 

這是在本地工作但沒有任何人有任何關於如何在發佈之後讓「內置」信息起作用的想法?

謝謝你的幫助!

最好的問候, 安德烈亞斯

回答

5

我想通了這出自己。如果您嘗試完成上述操作,則必須分隔本地化的錯誤消息。

爲其他錯誤消息fx「PropertyValueRequired」創建一個* .resx文件,並將生成操作設置爲「Embedded」,將Copy to Output Directory設置爲「Do no Copy」,將Custom Tool設置爲「PublicResXFileCodeGenerator」並將自定義工具名稱空間設置爲「資源」。

在我來說,我提出「PropertyValueRequired」到一個叫LocalDanish.resx(仍然在App_GlobalResources文件夾)的文件,並改變了線路在我的「MyRequiredAttributeAdapter」從

attribute.ErrorMessageResourceType = typeof(Resources.WebResources); 

attribute.ErrorMessageResourceType = typeof(Resources.LocalDanish); 

爲了使「內置」錯誤信息起作用,您必須創建兩個* .resx文件。我已經創建了WebResources.resx和WebResources.da.resx。請勿更改任何內容,將其設置保留爲默認值(構建操作爲「內容」等)。我猜這個網站會自動尋找* .da。在我的情況resx文件,因爲我已經在我的WebConfig中設置全球化:

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

希望這可以幫助任何人。

最好的問候, 安德烈亞斯

+0

謝謝!這個問題一直讓我瘋狂! – KTW

1

我已經取得了一些小的補充,原來的職位,這並沒有我的情況下翻譯的所有消息。 (字符串長度和無效的屬性值)

按照上述步驟,以創建* .resx文件,設置其屬性,然後設置區域設置在web.config中,如由Andreas說明。

然後創建了幾個適配器:

// As described in original post: 
public class LocalizedRequiredAttributeAdapter : RequiredAttributeAdapter 
{ 
    public LocalizedRequiredAttributeAdapter(
     ModelMetadata metadata, 
     ControllerContext context, 
     RequiredAttribute attribute 
    ) 
     : base(metadata, context, attribute) 
    { 
     if (attribute.ErrorMessageResourceType == null) 
      attribute.ErrorMessageResourceType = typeof(Resources.Resources); 
     if (attribute.ErrorMessageResourceName == null) 
      attribute.ErrorMessageResourceName = "PropertyValueRequired"; 
    } 
} 

// Addition to original post: 
public class LocalizedStringLengthAttributeAdapter : StringLengthAttributeAdapter 
{ 
    public LocalizedStringLengthAttributeAdapter(
     ModelMetadata metadata, 
     ControllerContext context, 
     StringLengthAttribute attribute 
    ) 
     : base(metadata, context, attribute) 
    { 
     if (attribute.ErrorMessageResourceType == null) 
      attribute.ErrorMessageResourceType = typeof(Resources.Resources); 
     if (attribute.ErrorMessageResourceName == null) 
      attribute.ErrorMessageResourceName = "StringLengthAttribute_ValidationError"; 
    } 
} 

而且在Global.asax.cx:

// Addition to original post: (Used for "PropertyValueInvalid") 
DefaultModelBinder.ResourceClassKey = "Resources"; 

// As described in original post: 
ClientDataTypeModelValidatorProvider.ResourceClassKey = "Resources"; 
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredAttribute), typeof(LocalizedRequiredAttributeAdapter)); 
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(StringLengthAttribute), typeof(LocalizedStringLengthAttributeAdapter));