Search code examples
asp.netlocalizationresourcemanagerresource-file

Specifying custom resource file for an ASP.NET page/usercontrol


If I have a page called Default.aspx, ASP.NET automatically uses the resource file named Default.aspx.resx in App_LocalResources for localizing server controls in the page.

But for some reason, I need to choose another file, let's say Default-Custom.aspx.resx. To provide some background, I already have Default.aspx.resx but some users need to have different content shown to them, which I am going to put in Default-Custom.aspx.resx.

Is is possible to choose the Resource file used for a TemplateControl in ASP.NET (short of writing a custom ResourceProvider)??


Solution

  • Take a look here:

    ResourceManager.GetResourceFileName Method

    This method uses CultureInfo 's Name property as part of the file name for all cultures other than the invariant culture. This method does not look in an assembly's manifest or touch the disk, and is used only to construct what a resource file name (suitable for passing to the ResourceReader constructor) or a manifest resource blob name should be.

    A derived class can override this method to look for a different extension, such as ".ResX", or a completely different scheme for naming files.

    You can try something like this:

    public class ResxResourceManager : ResourceManager
    {  
        protected override string GetResourceFileName(
             System.Globalization.CultureInfo culture)
        {
            return base.GetResourceFileName(culture);       
        }
    
        public string GetResxFileName(System.Globalization.CultureInfo culture)
        {
            return GetResourceFileName(culture).Replace(".resources", ".resx");
        }
    }
    

    For more on this:

    Creating a custom Resource Provider

    Under the Hood of BuildManager and Resource Handling