Implementing Multilingual Applications in Microsoft .NET
page 10 of 12
by Joydip Kanjilal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 53115/ 107

Configuring Localization in our Application

In order to ensure that we do not require changing the application source code each time we require to have support for a newer culture in the application, we can ensure even loose coupling by setting the culture name in the web.config file.  This ensures that the application's culture settings are configurable.  This information can be read by the application at run time and the appropriate culture settings set accordingly.

The <appSettings> element of the web.config file can be used to specify the culture name as shown below.

Listing 13

<appSettings>
  <add key = "Culture" value = "fr-FR">
  </add>
</appSettings>
 
Or

Listing 14

<appSettings>
  <add key = "Culture" value = "en-GB">
  </add>
</appSettings>

Note that fr-FR refers to French language that is spoken in France while en-GB refers to English language that is spoken in Great Britain.

The culture type that is specified in the web.config file can be read by using System.ConfigurationSettings.AppSettings in the source code.  Note that System.Configuration.ConfigurationSettings.AppSettings is a class in the System.Configuration namespace in the system.dll assembly.  The source code is provided below.

Listing 15

string culture =
 System.Configuration.ConfigurationSettings.AppSettings["Culture"].ToString();
ResourceManager resourceManager = new
 ResourceManager(typeof(TestForm).Namespace.ToString()+"."+culture,
 Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new CultureInfo(culture);
string message = resourceManager.GetString("ID",cultureInfo);
Response.Write(message);

In the code listing shown above, an instance of the ResourceManager class is created by passing the resource and the assembly in which the resource is embedded as parameters.  Here the culture and a reference of the current executing assembly are passed to it as parameters.  Then an object of the CultureInfo class is created and the culture name passed to the parameterized constructor of the class.  The ResourceManager class enables access to the resources for a particular culture using the GetObject and GetString methods.  Now we have to call the GetString method on the ResourceManager instance and pass the ID string as key and the instance of the CultureInfo class as parameters.  The resource value is returned as string and the same can now be used as needed in the application.  For the sake of simplicity, I have displayed the message using Response.Write method.  This concept can be used to set the text of a particular control in a specific language by reading the locale specific text from the resource.  The current locale is specified in the web.config file.

As an example, to set the copyright on a label control in the web from use the code provided below.

Listing 16

lblCopyright.Text = resourceManager.GetString("Copyright",culture);

Here the copyright message would be displayed in the label control based on the current culture that is set using the web.config file as explained earlier.

Note that either the satellite assembly or the application's assembly should have the resources for all the cultures to be supported by the application.


View Entire Article

User Comments

Title: What about localized Databases?   
Name: Rojalin
Date: 2006-09-28 6:54:50 AM
Comment:
many many tx for this article
Title: Implementing Multilingual Applications in Microsoft .NET   
Name: rambabu
Date: 2006-09-12 7:22:14 AM
Comment:
The article is very nice and the author explained in simple terms to understand the various aspects of Globailization and Localization.I liked this article very much.
Title: What about localized Databases?   
Name: Max
Date: 2006-09-12 4:58:25 AM
Comment:
Hi,
thank you very much for your article!
I have always been looking around for a standardized way of localizing db-entries. For instance if I run a web-shop which must be available to german and english customers my descriptions of my articles must be in german and english.
Of course this has to be stored in a DB and not in embedded resources.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-23 3:01:31 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search