in .NET

How to create multi-language websites using 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/.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.

  • mohamed

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

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

  • 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:
    All you need is to control your DIV or TD alignments using your resources:

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

  • 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/.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:
    It’s not correct,
    Correct it to something like this:

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

  • Amal

    hi[quote][/quote] ,
    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

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

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

  • 10x for the code 🙂 Cheers!

  • 10x for the code 🙂 Cheers!

  • Thank you, for this code 🙂 Cheers!

  • Thanks a lot for this post

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

  • 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!

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

  • We're a bunch of volunteers and opening a new scheme in our community. Your web site provided us with helpful info to work on. You have performed a formidable process and our entire neighborhood will be thankful to you.

    My web site :: asp web hosting ( Sung – http://exetersnuggroup.org/sample-page/ )

  • May I just say what a relief to uncover someone who genuinely understands what they are discussing on the net. You actually know how to bring a problem to light and make it important. More and more people really need to look at this and understand this side of your story. I can't believe you aren't more popular since you certainly have the gift.|

    Feel free to visit my web-site :: pit 36 a pit 36l – http://Alumniaffairs.Ait.asia/node/11549

  • Wow, marvelous weblog structure! How lengthy have you been blogging for? you make running a blog glance easy. The total look of your site is wonderful, as neatly as the content!

    My weblog :: ministerstwo finansów pit – http://www.ricored.net/blogs/37544/48901/formularze-pit

  • Thank you for every other informative blog. Where else may just I am getting that kind of information written in such an ideal way? I have a mission that I'm just now running on, and I have been at the look out for such info.|

    Here is my blog post: epity 2013 – http://maroc4u.com/index.php?do=/blog/22689/pit-2014-ulgi/