How to create multi-language websites using ASP.NET

By Mohammad Mahdi Ramezanpour at September 02, 2008 00:49
Filed Under: .NET General, ASP.NET

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:

Add App_GlobalResources Folder

When you created this folder, then right click on App_GlobalResources folder and then select Add New item:

Add A Resource File to Global Resource Folder

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:

Added Resources

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:

  1. Let your Web Browser decide.
  2. 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.aspx.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()
    {
        string langName;
        //Language short name for English (United States).
        langName = "en-US";
        System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Globalization.CultureInfo.GetCultureInfo(langName);
        System.Threading.Thread.CurrentThread.CurrentUICulture = 
            System.Globalization.CultureInfo.GetCultureInfo(langName);
        //Now the language is English (United States).
 
        //Language short name for Persian.
        langName = "fa-IR";
        System.Threading.Thread.CurrentThread.CurrentCulture =
            System.Globalization.CultureInfo.GetCultureInfo(langName);
        System.Threading.Thread.CurrentThread.CurrentUICulture =
            System.Globalization.CultureInfo.GetCultureInfo(langName);
        //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.

Comments

9/17/2008 11:17:01 AM #

mohamed

what happen when we use <%$ Resources: WebRes, ResLabel %> in not controls

mohamed

10/19/2008 9:00:10 PM #

admin

@Mohamed:
When you're using Resources, you must place your server-side tags in a server-side control. It means your controls must contain runat="server" attribute.

admin United States

5/13/2009 7:54:57 AM #

Amal

it is very nice , and i tried it and working v.good in english and in french but i need to apply it in Arabic and i have a problem in changing the alignment ,
Could you please help me to solve this problem becouse i want it urgently
also i have another problem in make CollapsiblePanelExtender Expanded text accept changing the language
thanks
Amal

Amal Egypt

5/13/2009 4:29:08 PM #

Admin

@Amal:
All you need is to control your DIV or TD alignments using your resources:

Here's an example:
<div runat="server" align="<%$ Resources: WebRes, PageAlign %>">>
[Your controls and contents]
</div>

Admin

5/14/2009 12:51:25 PM #

Amal

thanks for your reply , but it didn't work ( by the way : i'm using local Resources but global coz i want my site visitor chooses the language from the drop down list )
i put the code as below
<div runat="server" align="<%$ Resources:Home.aspx.ar.resx, PageAlign %>">>
and in my localresource i put a row
PageAlign  right  
please tell me where is my error in it
Thanks for your help
Amal

Amal Egypt

5/15/2009 8:04:57 PM #

Admin

@Amal:
It's not correct,
Correct it to something like this:

<div runat="server" align="<%$ Resources:Home, PageAlign %>">>
By the way, for your situation, it's common to use Global_Resource not Local_Resource.

Admin

5/17/2009 9:48:43 AM #

Amal


hi ,
thanks for your quick response , i tried what you told me last time and it also gives me error "The resource object with key 'PageAlign' was not found."
within i make  global resource instead of local as you told me ,
plz tell me where is the mistake i did
thanks for your help
Amal

Amal Egypt

7/13/2009 7:29:26 AM #

download free roulette

Extremely nice. It comes handy for developers. You've done a tremendous job at it. Keep up the great work.

download free roulette India

7/14/2009 11:58:30 PM #

Christian Audigier

Very interesting topic will bookmark your site to check if you write more about in the future.

Christian Audigier United States

7/19/2009 5:43:00 PM #

запознанства

10x for the code Smile Cheers!

запознанства

7/19/2009 5:43:04 PM #

запознанства

10x for the code Smile Cheers!

запознанства

7/19/2009 11:20:35 PM #

запознанства

Thank you, for this code Smile Cheers!

запознанства

7/22/2009 4:13:33 AM #

Hoodia Slankepiller

Thanks a lot for this post

Hoodia Slankepiller

7/26/2009 5:24:29 AM #

Duck Hunting Canada

Me and my friend were arguing about this the other day! Now I know that I was right. lol!

Duck Hunting Canada United States

7/26/2009 5:24:34 AM #

San Diego Web Design

I just couldn't leave your website before saying that I really enjoyed the quality information you offer. Will be back often to check up on new stuff you post!

San Diego Web Design United States

8/5/2009 10:09:50 AM #

Make Your Own Website

Admiring the time and effort you put into your blog and detailed information you offer! I didn't know that!

Make Your Own Website United States

Comments are closed

Currently Reading

Quote of the day

Send Persian SMS