开发者

When accessing resources by property name is it possible to change which ResourceManager is used?

I want to use a custom ResourceProvider but still have the benefit of strongly typed resource names. It seems though that i开发者_StackOverflow社区n my view if I access the resource property directly like so:

@Html.Raw(UIText.Header)

Then the text is read directly from the resx file and doesn't go through my custom provider factory which is defined in the web.config.

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

Works fine but has the down-side of being stringly typed. I have created an extension method which wraps this but its still quite ugly:

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

So, is there a way to use the resource properties directly but have them route through a custom provider without having to create our own ResXFileCodeGenerator?


Would a static class that holds strings work for you? I've made a T4 template file that looks at my .resx and generates this static class automagically.

This is the result of the T4 template compilation:

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";
    ...
}

I use it my ASP.NET MVC attributes to classes like this:

    [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;

Hopefully I didn't miss it completely;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜