Locate text using Resources properties on Asp.Net Web pages on an aspx page

I am trying to translate button text on my aspx page. I am using asp.net webforms and I have not achieved this yet. In MVC, I can do exactly what I want, but in webforms, this is a pain for me.

Here are my resources

My resources

I am trying to use it in aspx, but to no avail. I can do it in code with this code

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

But I really want to do it right on the page

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

Can anybody help me?

+5
source share
3 answers

Here's how I do it in Visual Studio 2013:

  • For global resources, right-click Project and select Add > Add ASP.NET Folder > App_GlobalResources .
  • For local resources, right-click on the folder where the file you want to use the resources is located 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 is a link for more details.

  • Create resource files and name them as shown below:

    Creation

    It is possible to create as many App_LocalResources folders as needed, but again, the App_LocalResources folder where you put the resource files ( .resx ) should be in the same folder as the .aspx , .Master or .ascx .

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

    If the user changes the language of their browser to pt-BR, use pt-br.resx on the page.

  • Creating resource items. Name = Key, Value = Display Text

    Editing

  • Using a 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 generates a static class called 'Labels' if the resource file name is 'Labels.pt-br.resx', in a global namespace called Resources . The "Custom Tool" GlobalResourceProxyGenerator , which is defined in the properties of the resource file, is responsible for this, and you can access the global resources writing Resources.Labels.ResourceKey .

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

  • Select a local resource file
  • press F4 or right click and select "Properties"
  • In the Custom Tool enter 'PublicResXFileCodeGenerator'
  • In Build Action select Embedded Resource
  • Then rebuild the application or website. Now you can see that VisualStudio generates the .designer.cs file attached to the resource file.

How to use it?

Following the structure I create in this answer, we have a LocalResource in the MasterPages folder creating the WebFormsProject2.MasterPages.App_LocalResources namespace. If you open '.designer.cs', in this case Frontend.Master.designer.cs , in another text editor you will see that it will generate a class called Frontend_Master in the WebFormsProject2.MasterPages.App_LocalResources namespace and some static properties with the same by the name as the resource keys created in the resource file. Now you just need to create a link to this namespace and access properties such as 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> 
+3
source

Create resource files in the App_GlobalResources folder, as shown below:

enter image description here

then add the button text to the resource files as follows: enter image description here

Create resource files for each language into which you want to pass the button text to

And in the source code, change the button text as shown below: enter image description here

Hope this helps

+2
source

You should have done some more research ...
Look this

1) Create the folder "App_GlobalResources"
2) Inside the specified folder, create a resource file "ResourceStrings" and an entry named "Key1"

Then:

 <asp:Button ID="Button1" runat="server" Text="<%$ Resources:ResourceStrings, Key1 %>" /> 
+1
source

Source: https://habr.com/ru/post/1264511/


All Articles