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

1

4

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)??

Lion answered 7/4, 2011 at 15:23 Comment(1)
Hmmm... I have already written a custom ResourceProvider/ResourceManager and other supporting infrastructure to achieve what I needed...Lion
C
3

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

Cedell answered 4/5, 2011 at 21:30 Comment(4)
Thanks for replying Leniel. As I mentioned in my comment to the original post, I have already written a custom ResourceProvider for myself that can parse out resources in an extremely deep and complex hierarchy based on my needs. Further, it can work on .resx files (in a WSP environment) as well as .resources (in a completely pre-compiled or WAP environment). Thanks for your comment none-the-less.Lion
@r_honey: I didn't see that the comment was yours... :)Cedell
I have accepted this post as the answer as it correctly provides a solution to the question asked (although I should have framed it better as my needs were different :))Lion
Thanks to Leniel for answering the question, with some extra reading material, and r_honey for leaving the question up. This helps others coming along later with the same questions, like me :).Flowerer

© 2022 - 2024 — McMap. All rights reserved.