Search code examples
asp.net-mvc-3localizationglobalization

resourceProviderFactoryType is ignored in web.config file


I have been trying to set a custom resourceProviderFactory at the web.config file but its always ignored, my web.config file looks like the following

<globalization culture="auto" resourceProviderFactoryType="MyProject.CustomResourceFactory, MyProject"></globalization>

My CustomResourceFactory class is never fired, setting a breakpoint at the constructor is never reached.

Misspelling the class name in the globalization section does not raise any exceptions, is that normal ?

I've tried this in more than one project (thinking that the project I'm working on is misconfigured or something) but the same results.

I'm using C# asp.net MVC3,

Does anyone have a clue why this happens, and is there a way to change the ResourceProviderFactory at runtime ?

Thanks.


Solution

  • I ran into the same issue, and didn't quite understand your comment on the first answer. Now that I've solved it I do, but I thought I'd elaborate for anyone else.

    http://publicityson.blogspot.ca/2010/11/aspnet-mvc-razor-view-engine-and.html

    He created helpers to call his custom resources as the razor requirement of calling ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey) in the view is really long, which resulted in something like:

    public static class CommonHtmlExtensions
    {
        public static object GetGlobalResource(this HtmlHelper htmlHelper, string classKey, string resourceKey)
        {
            return htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(classKey, resourceKey);
        }
        //several other methods.
    }
    

    Allowing him to use @Html.GetGlobalResource("CommonText", "Some_ResourceKey") in a razor view.

    Also very useful to keep things strongly typed and usable in dataannotations, the link below shows how to use T4 templates to create constants for every resource item in your database. http://carrarini.blogspot.ca/2010/08/localize-aspnet-mvc-2-dataannotations.html