Multilingual Applications in .NET

1. Introduction

 
This article explains the basic understanding of different methods of developing multilingual applications in . NET. Here the emphasis is given on handling multiple languages using resource files. This also explains the necessary settings to be made in the Windows operating system and SQL Server database to support the multilingual capabilities.
 

2. General

 
Different approaches to developing multilingual applications:
  • Developing the application or web pages for each language separately.
     
    Even though it is easy to host the application differently, the drawback is that keeping both sets in sync even after a minor change is very difficult.
     
  • Store all the content in different languages in database tables.
     
    This approach solves the problem to a large extent but still requires more coding and database storage. This can have a performance issue also as the content is fetched from the db; but has the advantage of easy maintainability and also the creation of the language on the fly.
     
  • Store all the content in different languages in resource files.
     
    Resource is non-executable data that is required by the application and is deployed along with the application. This will have a better performance compared to fetching from the db; but deployment and new language support can be a little cumbersome as it requires more technical knowledge.
Based on the requirement and priorities, a different approach can be selected for development. This article discus on using resource files for development.
 

3. Approach

 
Before going into details about how to create a resource, however, we need to understand what is meant by "globalization", "localization" and "culture" in this context.
 
3.1 Globalization
 
Globalization is a process of identifying all the parts of your application that need to be different for respective languages and separate them from the core application.
 
3.2 Localization
 
Localization is the process of creating and configuring your application for a specific language.
 
3.3 Culture
 
Culture is the combination of the language that you speak and the geographical location you belong to. It also includes the way you represent dates, times, and currencies. Culture is represented as shown in the following example:
 
en-US
en-GB
fr-BE
 
The first culture represents the English language being spoken in the US whereas the second culture represents the English culture being spoken in Great Briton.
 
3.4 Namespaces
 
To work with cultural information .NET provides a class called CultureInfo that resides in System.Globalization namespace. The culture is set at thread level so you also need to work with Thread class from System.Threading namespace. Finally, for working with resources you need to use classes from System.Resources.
 
3.5 Setting the Culture and UI Culture for Windows Forms
 
Set the CurrentCulture and CurrentUICulture properties if you wish to override the settings of the user or operating system. In general, you would want to specify a culture so that every part of the application's user interface is appropriate to that culture. So you must set it before the InitializeComponent method is called.
  1. // Put the using statements at the beginning of the code module  
  2. using System.Threading;  
  3. using System.Globalization;  
  4. // Put the following code before InitializeComponent()  
  5. // Sets the culture to French (France)  
  6. Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");  
  7. // Sets the UI culture to French (France)  
  8. Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR"); 
Note: The CurrentCulture property's default value is the system's User Locale, which is set in the Regional Options control panel. The CurrentUICulture property's default value is the system's UI Language, which is the language of your system UI. On Windows 2000 and Windows XP Multilanguage Edition, the CurrentUICulture defaults to the current user UI language settings.
 
3.6 Setting the Culture and UI Culture for Web Forms
 
A Web Forms page has two cultural values, Culture and UICulture. The Culture value determines or influences the results of culture-dependent functions, such as displaying the date. The UICulture value determines how the resources are loaded for the form and is used for culture-specific lookup of resource data. The only purpose of the UICulture value is to indicate the language the resources should load, that is, determine which language the UI strings should use. The Culture value determines everything else - date formatting, number formatting, and so on.
 
To set the Culture and UI Culture values
  • In the Globalization section of the Web.config file, add lines similar to the code below.
     
    culture="en-US"
     
    uiCulture="de"
     
    -Or-
     
  • In the HTML view, add the Culture and UICulture values to the page directive.
     
    <%@ Page UICulture="de" Culture="en-US"...%>
     
  • In the Code Editor, add code to set the CurrentCulture and CurrentUICulture properties.
The following example sets both the CurrentCulture and the CurrentUICulture properties to the culture specified in the browser's Accept-Language header. Note that the CurrentCulture property is set using the CreateSpecificCulture method since this property must be set to a specific culture and not a neutral culture.
  1. // Import these namespaces at the beginning of the code module.  
  2. using System.Globalization;  
  3. using System.Threading;  
  4. // Set the culture and UI culture to the browser's accept language  
  5. Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);  
  6. Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0]); 

3.7 Resource Files

 
A resource is non-executable data that is required by the application and is deployed along with the application. E.g. Bitmaps, icons, cursors, screen labels. In order to create a resource file, you need to first create a text/XML file containing the resource information and then compile it to get the actual resource file.
 
3.7.1 Types of Resource Files
 
There are two types of files in which you can store resource information.
  • Text files: they are plain ASCII text files with key-value pairs. They can not hold binary data like images or objects.
  • .resx files: They can contain text as well as binary data. They are actually XML files and can be used easily with VS.NET
3.7.2 Creating "text-only" Resource Files
 
Text resource files can be created with any text editor like Notepad. If you are using VS.NET you can add text file to a project and then add key-value pairs as shown below.
 
sample1.txt
 
label1=Hello World
 
3.7.3 Creating ".resx" Resource Files
 
To create .resx files VS.NET provides an easy way. Just Add > New Item > Resource File from the menu and enter keys and values in the resource editor.
 
A part of such file is shown below:
  1. <data name="label1">  
  2.      <value>Hello World</value>  
  3. </data> 
Note that for all resource files the Build Action should be set to Embedded Resource.
 
3.7.4 Creating Resource Files for different locales
 
The process of creating resource files for different locales is exactly the same as mentioned above. However, you need to follow a certain naming conventions. The resource source files for different locales should have names matching the following syntax.
 
<base file name>.<locale>.txt
<base file name>.<locale>.resx
 
e.g
 
mymessage.en-US.txt
mymessage.en-GB.txt
mymessage.fr.txt
 
Note that a file without a locale indicator forms the default resource file. E.g. mymessage.resources will form the default resource file for the application.
 
3.7.5 Compiling Resource Files
 
You can create a resource file by compiling the above-mentioned resource 'source' files using ResGen utility that ships with .NET SDK
 
e.g.
 
resgen mymessage.txt
 
resgen mymessage.resx
 
The above utility will output mymessage.resources file after successful execution.
 

3.8 Satellite Assemblies

 
Satellite Assemblies are compiled DLLs that contain only resource data and no other code. Satellite assemblies can be created with the help of AL utility provided with .NET SDK or you can use VS.NET for creating them. They can be deployed at a later stage after the main application has been deployed. They are ideal for product-based development where companies can come up with additional language support at later date.
 
3.8.1 Reading resource from an assembly
 
In order to compile your resources in a satellite assembly, you need to follow these steps:
  • Create a resource file (.txt or .resx)
  • Compile resource files (.resource)
  • Create satellite assembly (.dll) using AL command-line tool
  • Create folders for storing the satellite assembly
  • Place corresponding satellite assembly into the folders
  • Access the resources inside the application
3.8.2 Creating Satellite Assemblies using AL utility
 
Using AL command-line utility you can embed your resources in an assembly. The assembly thus formed is nothing but a Satellite Assembly. Following code shows how this is done:
 
AL
/t:lib
/culture:en-GB
/embed:mymessage.en-GB.resources
/out:ResourcesDemo.resources.dll
 
Here, /t switch tells that the output is of type library (DLL). The culture switch tells that the resources being embedded in the assembly are for en-GB culture. /embed switch specifies the .resources file that is to be embedded in the Satellite Assembly. Finally, /out switch specifies the name of the output assembly.
 
3.8.3 Creating Satellite Assemblies using VS.NET utility
 
If you are using VS.NET, you can simply add the resource file to your project (library project) and make sure to set its action property in the property window to Embedded Resource. At compile time VS.NET automatically embeds the resources inside the assembly being generated.
 
3.8.4 Creating folders for storing the Satellite Assembly
 
Satellite Assemblies can be dynamically loaded based on the current culture of your application. In order that this works as expected, you need to follow a specific folder structure to place Satellite Assemblies. You need to create a sub-folder inside the main folder of the application (i.e. where EXE of your application resides or BIN folder for ASP.NET application) with the same name as the culture. For example, if your Satellite Assembly is for culture en-GB then you need to create a folder called en-GB and place the Satellite Assembly there.
 
3.8.5 Access the resources inside the application
 
In order to access a resource inside Satellite Assembly, we will again use ResourceManager class. Following code shows how this works:
 
Dim x As ResourceManager
  1. Dim ci As New CultureInfo("en-GB")  
  2. Thread.CurrentThread.CurrentCulture = ci  
  3. x = New ResourceManager("mymessage.en-GB",[Assembly].GetExecutingAssembly.GetSatelliteAssembly(ci))  
  4. Label1.Text=x.GetString("label1"
Here, we have created an instance of CultureInfo class by passing the culture code "en-GB" to its constructor. Then we set this culture as the current culture of the running thread. We then create an instance of the ResourceManager class by specifying the resource base name. The second parameter of the ResourceManager constructor is of our interest. We are passing the Satellite Assembly that contains resources for the specified culture. We get this Satellite Assembly by GetSatelliteAssembly() method of the executing assembly. Finally, we get a culture-specific string by calling GetString() method on the resource manager.
 
4. Windows Requirements
 
Windows 2000, XP, and above are the best operating systems to support multilingual capabilities as they are completely UNICODE enabled. Even though the older versions of windows do support but you will require the specific language edition of the operating system to support.
 
5. Setting up Windows 2000
 
To support multiple languages it is required to setup Language settings for the operating system.
 
"Language settings for the system" is available in "Start => Settings => Control Panel => Regional Options" and "General" tab. Select the languages which are to be supported.
 
6. SQL Server Settings
 
To store the UNICODE data in the SQL Server database tables it is required to use the nvarchar and nchar datatypes.
 
7. Input Method Editors
 
IMEs are components that allow the user to enter the thousands of different characters used in East Asian languages using a standard 101-key keyboard. The user composes each character in one of several ways: by radical, by phonetic representation, or by typing in the character's numeric code-page index. IMEs are widely available; Windows 2000 and Windows XP ship with standard IMEs that are based on the most popular input methods used in each target country, and a number of third-party vendors sell IME packages.
 
Different IMEs can be added by using "Start => Settings => Control Panel => Regional Options" and "Input Locales" tab.
 
8. References
  1. http://www.microsoft.com/globaldev/
  2. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconinternationalizingapplications.asp
  3. http://www.devx.com/dotnet/Article/6997/0/page/1