2011-04-29 20 views
4

我想使用自定義的ResourceProvider,但仍然具有強類型資源名稱的好處。看來,雖然在我看來,如果我直接訪問資源屬性,像這樣:通過屬性名稱訪問資源時,是否可以更改使用哪個ResourceManager?

@Html.Raw(UIText.Header) 

然後將文本直接從RESX文件中讀取,並通過其在網絡中定義我的自定義商工廠不走的.config。

@Html.Raw(HttpContext.GetLocalResourceObject("UIText.resx", "Header").ToString()) 

工作正常,但存在被串行輸入的問題。我創建了包裝此,但它仍然相當難看的擴展方法:所以

@Html.ResourceText(() => UIText.Header) 

,有沒有直接使用資源屬性的方式,但有他們的路線,通過自定義提供,而無需創建我們自己的ResXFileCodeGenerator

回答

0

保存字符串的靜態類是否適合您?我製作了一個T4模板文件,它可以查看我的.resx文件,並自動生成這個靜態類

這是T4模板編譯的結果:

public static class ResourceStrings { 
    public const string AddAttachments = @"AddAttachments"; 
    public const string button_create = @"button_create"; 
    public const string button_delete = @"button_delete"; 
    public const string button_details = @"button_details"; 
    public const string button_display = @"button_display"; 
    ... 
} 

我用它在我的ASP.NET MVC屬性類是這樣的:

[StringLength(8, ErrorMessageResourceType = typeof(i18n), 
    ErrorMessageResourceName = ResourceStrings.Client_VatNo_length_validation_msg)] 
    public string VatNo; 

    [StringLength(10, ErrorMessageResourceType = typeof (i18n), 
    ErrorMessageResourceName = ResourceStrings.Client_BaseIdentNr_length_validation_msg)] 
    public string BaseIdentNr; 

希望我沒有完全錯過;)

相關問題