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

若如初见. 提交于 2019-12-19 09:09:22

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/5583529/specifying-custom-resource-file-for-an-asp-net-page-usercontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!