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
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?
This is how I do it on Visual Studio 2013, 2015 and 2017:
Add > Add ASP.NET Folder > App_GlobalResources
.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.
Create resource files and names them as show below:
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
.
Create resource items. Name = Key, Value = display text
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 globalnamespace
calledResources
. The responsible for this is the 'Custom Tool'GlobalResourceProxyGenerator
that is defined in resource file properties, and you can access global resources writingResources.Labels.ResourceKey
.
To make LocalResources files static access like as GlobalResources, you can do the following:
Custom Tool
type 'PublicResXFileCodeGenerator'Build Action
select Embedded Resource
.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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With