Click here to Skip to main content
Click here to Skip to main content

Globalization in ASP.NET 2.0

, 8 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to create an ASP.NET Web application that can be adapted to different languages

Introduction

Globalization is the process of designing and developing applications which can be adapted to different languages. Globalization is the process of designing and developing a software product that functions for multiple cultures.

If you want your website to be accessible in several languages in different regions, you should globalize it.

Background

Globalization is a very efficient functionality that you can give to your website so that it can be adapted to different languages and regions.

A very good example you can see is the Google home page. There are different language options given below from which you can select one language and the whole site would adapt for that particular language.

How Would You Globalize Your Website in ASP.NET

Design your Page

Start Visual Studio 2005, go for a new website, name it ‘GloblizationEx’. First, design your website like a sample I have given below. Take 3 link buttons and a label. Change the text property of label to ‘Hello user let’s learn Globalization’ and change the text property of three link buttons to French, German, Russian.

Now make click event of LinkButton1 by double clicking on it. Go to LinkButton2 property select event tab, select ‘LinkButton1_Click’ for click event. Repeat this for LinkButton3.

Add Resources Files

Now, you have to add resources file to your website, but first add a New Folder to your website and named it ‘resources’. For this, just go to solution explorer, right click on root and select ‘New folder‘. Now right click to this resources folder and select ‘Add New Item..’ option, Add New Item dialog box will appears, select ‘Resource File’ and named it ‘strings.fr-FR.resx’. A message box will appear, click ‘No’ as given below:

Now a screen appears. You have to enter Name and value for your resource file.

Enter ‘msg’ for Name and ‘bonjour utilisateur apprenons globlization’ for value field. Don’t worry, this msg is only French conversion of your message that you had given at your home page. Repeat these steps for German and Russian, file name will be ‘strings.de-DE.resx’ and ‘strings.ru-RU.resx’ respectively. You can give value field for German and Russian as ‘hallo Benutzer lets lernen globlization’ and ‘@ hallo lernen globlization’ respectively. But Name field will be the same ‘msg’. Save all. A sample is given below:

Note: You can translate your messages using Google More>>Translate tab.

Generate ‘.resources’ files.

Now you have to generate ‘.resources’ file. For this you have to open visual studio command prompt, you can find it at ‘visual studio 2005>> Visual Studio Tools’ menu. As you start Visual Studio command prompt, cmd screen appears. You have to locate ‘resources’ folder, like if you are creating your website in C drive with name ‘GloblizationEx’, you have to locate like ‘c:\Globlization\resources’ now type command ‘resgen strings.fr-FR.resx’ for French resx file. Do it for German and Russian as ‘resgen strings.de-DE.resx’ and ‘resgen strings.ru-RU.resx’ respectively.

You can notice that .resources files are added to ‘resources’ folder.

Using the Code

You should use the following namespaces:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;
using System.IO;
using System.Globalization;

Now type the following code to click event (LinkButton1_Click) of LinkButton1.

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string lanName="";
        LinkButton lnkBtn=(LinkButton)sender;

        switch (lnkBtn.ID)
        {         
            case "LinkButton1":
                lanName = "fr-FR";
                break;
            case "LinkButton2":
                lanName = "de-DE";
                break;
            case "LinkButton3":
                lanName = "ru-RU";
                break;
        }
        System.Resources.ResourceManager rm = 
        ResourceManager.CreateFileBasedResourceManager
        ("strings", Server.MapPath("resources") + Path.DirectorySeparatorChar, null);
        CultureInfo cinfo = new CultureInfo(lanName);
        Label1.Text = rm.GetString("msg", cinfo);
    } 

Now just debug your website and click on ‘French’/’German’/’Russian’ link to change your welcome message.

History

  • 8th March, 2011: Initial version 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
GeneralThis is not globalisation. PinmvpPete O'Hanlon9-Mar-11 1:47 
GeneralRe: This is not globalisation. PinmemberOakman14-Mar-11 12:13 
GeneralMy vote of 5 PinmemberSona s9-Mar-11 1:20 
GeneralMy vote of 2 PinmemberMember 1319238-Mar-11 21:58 
GeneralMy vote of 5 Pinmemberr.s19878-Mar-11 18:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 9 Mar 2011
Article Copyright 2011 by Brijesh Kr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid