Search code examples
asp.netinternationalizationascx

How to provide translations for items in a ASP.Net custom web control (.ascx)?


How do I go about providing localised text for items in a custom web control? I had thought that I just need to add meta.resourcekey tags to the control items and then define and fill some resource files called mycontrol.ascx.[lang].resx but that doesn't seem to work.

e.g.

MyControl.ascx

<asp:Label ID="Label1" meta:resourcekey="Label1" runat="server" Text="Oops!"></asp:Label>

MyControl.ascx.de.resx

Label1.Text    Donner und Blitzen!

Solution

  • You dont need to add meta tags.

    Have this in your resource file (MyControl.ascx.de.resx) which will be located in App_LocalResources:

    Name Value


    SomeName Oops!

    Then in your User Control:

    <asp:Label ID="Label1" meta:resourcekey="Label1" runat="server">
        <%=GetLocalResourceObject("SomeName") %>
    </asp:Label>
    

    That helper method is part of the System.Web.UI.TemplateControl namespace.