Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localize text using Properties Resources on Asp.Net WebForms in aspx page

I'm trying to translate the text of a button in my aspx page. I'm using asp.net webforms and I did not achieve it yet. In MVC I can do exactly what I want but in webforms it's being a pain to me.

Here is my resources

My reources

I'm trying to use it in aspx but without success. I can do it in code behind with this code

 protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Text = Properties.Resources.BUTTON_SEND;
    }

But I really want to do it directly in page

 <asp:Button ID="Button1" runat="server" Text="HERE_COMES_THE_LOCALIZABLE_TEXT" />

Can someone help me please?

like image 775
Leandro De Mello Fagundes Avatar asked Feb 21 '17 11:02

Leandro De Mello Fagundes


1 Answers

This is how I do it on Visual Studio 2013, 2015 and 2017:

  1. For global resources right click on Project and select Add > Add ASP.NET Folder > App_GlobalResources.
  2. For local resources right click on the folder where is located the file where you want to consume the resources are and select Add > Add ASP.NET Folder > App_LocalResources.

    For labels and error messages I prefer to use LocalResources. This practice makes the project more organized and easy to modify. Here's a link for more details.

  3. Create resource files and names them as show below:

    Creation

    It's possible to create as many App_LocalResources folders as you need, but again, the App_LocalResources folder, where you will place the resource files (.resx), must be in the same folder as the .aspx,.Master or .ascx file.

    Frontend.Master.resx for labels and messages with the default language
    Frontend.Master.pt-br.resx for Brazilian Portuguese labels and messages.

    If user change the language of it browser to pt-BR then the page use the pt-br.resx.

  4. Create resource items. Name = Key, Value = display text

    Editing

  5. Using local or global resource file:

    <head>
         <title><%= GetGlobalResourceObject("Labels", "HelloWorld") %></title>
    </head>
    <body>
        <button type="button">
            <span><%= GetLocalResourceObject("Header_NavButton_Sr") %></span>
            <asp:Literal runat="server" Text="<%$ resources:Header_NavButton_Sr %>"></asp:Literal>
        </button>
        <a href="index.html"><%= GetLocalResourceObject("Header_TextLogo") %></a>
        <asp:TextBox ID="tb1" runat="server" Text="<%$ resources:Navbar_Home %>"></asp:TextBox>
    </body>
    

GlobalResources files generate a .designer.cs file. This file generate a static class named 'Labels', if the name of resource file is 'Labels.pt-br.resx', in a global namespace called Resources. The responsible for this is the 'Custom Tool' GlobalResourceProxyGenerator that is defined in resource file properties, and you can access global resources writing Resources.Labels.ResourceKey.

To make LocalResources files static access like as GlobalResources, you can do the following:

  1. Select the local resource file
  2. press F4 or right click and select 'Properties'
  3. On Custom Tool type 'PublicResXFileCodeGenerator'
  4. On Build Action select Embedded Resource
  5. After this, rebuild your application or web site. Now you can see that VisualStudio generate a .designer.cs file nested with resource file.

How to use it?

Following the structure that I create in this answer, we have a LocalResource in the MasterPages folder generating the name space WebFormsProject2.MasterPages.App_LocalResources. If you open the '.designer.cs', in this case Frontend.Master.designer.cs, on another text editor, you will see that it generate a class named Frontend_Master on the namespace WebFormsProject2.MasterPages.App_LocalResources and some static properties with the same name as the resources keys you created in the resource file. Now you just need to create a reference to this namespace and access properties like Frontend_Master.Header_TextLogo.

Example:

<%@ Import Namespace="WebFormsProject2.MasterPages.App_LocalResources" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title><%= Frontend_Master.Header_TextLogo %></title>

</head>
<body>...</body>
like image 124
Henryk Budzinski Avatar answered Sep 28 '22 03:09

Henryk Budzinski