Using Resources can help you create flexible multi-language websites in ASP.NET. In this post I want to show you how you can do it.
As you know, ASP.NET has some specific folders like: App_Data, App_Code, … . One of these folders is App_GlobalResources that holds resources. you can add this folder by right click on your website in Solution Explorer and from Add New ASP.NET Folder, select App_GlobalResources as following:
When you created this folder, then right click on App_GlobalResources folder and then select Add New item:
As you can see, you can add a limited types of items to this folder. From the list, select Resource File and then select a name for your new file as always. I selected WebRes.resx as my file name. Resource files are XML based files that hold data in your application and will compile with your application. In order to create multi-language websites, you need to create a file for each language you want to implement in your website. So because I want to have Persian language too, I should create another resource file. The important section of creating another resource file is in it’s name. You must follow some rules in your naming in order to let ASP.NET know which languages you have in your application. The rules are listed below:
- You must start your resource file name with the name you specified to your first resource (In this post WebRes).
- The only difference between your first file name and new language file name is in the last section of your file name. For example if you have a first resource file named WebRes.resx, and you want to add Persian language to your application, you must name your Persian language resource file WebRes.fa.resx (fa is short for Farsi and it’s global); After that .NET Framework automatically understands that you have a Persian language resource file.
Now how to use resources in my application?
In order to use resources, you must make a use of embedded tags (<% %>) in your application. For example imagine that I have a label in my application and I want to use one of my resource keys in as the text property of my label. In order to do this, you must do something like this:
<asp:Label runat="server" ID="Label1" Text='<%$ Resources: WebRes, ResLabel %>'></asp:Label>
How it works?
The first part is Resources that tells ASP.NET that it’s going to be a Global Resource.
WebRes is the name of our Resource file (Without any suffix). Note that you don’t need to write any suffix in this section.
ResLabel is the name of our key we want to use in. I just added a key named ResLabel as shown below:
After you specified where want to show your resource , now you must let your application know which of those languages (In this sample, English (Unites States) and Persian) you want to use.
Your have two general options:
- Let your Web Browser decide.
- You enforce your application to use your specific language.
Let your Web Browser decide:
In order to set your application to get default language from your web browser, all you need is to set Culture and UICulture attributes to auto as shown below:
<%@ Page Culture="auto" UICulture="auto" Language="C#" AutoEventWireup="true" CodeFile="Default/.cs" Inherits="_Default" %>
Your Web Browser automatically fill your culture information from user’s Operating System.
Enforce your application to use your specific language:
Now it’s time to write some code. in order to enforce your application to use a specific language, you need to dynamically change the default Culture and UICulture of your application. ASP.NET holding culture information in the application main thread. So you have to change that as following:
protected void ChangeLanguage()
//Language short name for English (United States).
langName = "en-US";
//Now the language is English (United States).
//Language short name for Persian.
langName = "fa-IR";
//Now the language is Persian.
As you saw, .NET Framework will automatically understands which culture you want to use and then tells your application which resource file it must use.