Creating Multi-lingual Websites Easily in .NET

With the advent of .Net creating multi-lingual websites has become easy. Earlier you need to create pages for each language. Moreover editing such contents is also tedious. Creating multi-lingual applications is the first step towards globalization of an application.

An application can be considered truly global only if user of different cultures and languages can access the application. You need to separate the application’s resources that need to be translated from the main code of the application. Then you customize the application to meet the requirements of the different cultures and languages.

.Net provides the CultureInfo class and the namespaces like System.Globalization and the System.Resources.ResourceManager. When building an application that needs to be globalized you need to concentrate on globalization in the design phase itself. This would save you a lot of time and money. The CultureInfo class provided by .Net gives culture specific information. This information can be language, region, calendar and other parameters associated with a culture.

A unique name for each culture is specified based on the RFC 1766 standard. The CultureInfo class provides information required to process strings, casing, dates and numbers associated with a specific culture. The name of a culture is given as a lowercase two letter indicating the language followed by a uppercase two letter indicating the region or country. This subculture code in uppercase is separated from the lowercase culture code by a hyphen. For example “ja-JP” indicates the language Japanese as spoken in Japan.

Let us consider an webpage that wants to display a label in different languages based on what the user has selected. To achieve this you would be creating resource files for different languages. The resource file would have the equivalent of the text that is to be displayed in the label.

For example if you want to display the word “Username” in French, you would create a resource file that would have “Nom d'utilisateur” as an equivalent for “username”. You should know that resource files are just XML files that contain data mapping to different languages. For example the following data may be in a resource file, Res.fr-CA.resx which indicates the French language spoken in Canada.

<data name="Username">
<value>Nom d'utilisateur</value>
</data>

If you want to assign this word in the label, you need a ResourceManager object to perform this action. The ResourceManager is responsible for getting the right word from the correct resource file. The ResourceManager gets the information from the current thread’s CurrentCulture value. For example the code required to assign value to a label would be something like given below:

lblUserName.Text = rm.GetString("Username");

where rm is an instance of ResourceManager.

To get the correct information from the correct resource file, you need to write something like,

CultureInfo ci = new CultureInfo("fr-CA");
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
System.Threading.Thread.CurrentThread.CurrentUICulture = ci;

The last statement is where the ResourceManager locates the correct resource file for that particular culture.

Setting the culture info has to be done dynamically when the user clicks on a lick for a particular language version of the site. This is very easy. You can simple pass a query string to the .aspx page and retrieve the value of the querystring in the Application_BeginRequest of the Global.asax file and then set the CultureInfo instance. The code given below retrieves the querystring “language” and checks the value of the querystring.

protected void Application_BeginRequest(Object sender, EventArgs e) {
string slang = Request.QueryString["language"];
string cinfo = "en-CA";
if(lang != null){
switch (slang.ToLower()){
case "frc":
cinfo = "fr-CA";
break;
default:
cinfo = "en-CA";
break;

}
}
CultureInfo c = new CultureInfo(cinfo);

If the value of the querystring is “frc”, then the CultureInfo is set to “fr-CA”. then the current thread’s CurrentUICulture is set to this culture information. This would enable the ResourceManager to select the appropriate resource file and retrieve data that corresponds to that particular culture.

By using these concepts you can create applications that are truly global. The application that you create this way would have the resource files in the assembly itself and you would be required to recompile the application when you add some other resource files. It is possible to compile the resource file into a separate assembly and use that resource files for all the applications.


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.