Localize text using Properties Resources on Asp.Net WebForms in aspx page
This is how I do it on Visual Studio 2013, 2015 and 2017:
- For global resources right click on Project and select
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:
- Select the local resource file
- press F4 or right click and select 'Properties'
- On
Custom Tool
type 'PublicResXFileCodeGenerator' - On
Build Action
selectEmbedded Resource
- 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>
Create resources files in "App_GlobalResources" folder as below:
then add your button text in resources files as follows:
Create resources files for each and every languages you want to transfer the button text to
And in the source code change the button text as below:
Hope this helps