This article explains the basic understanding on different methods of developing multilingual applications in .NET. Here 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.
Different approaches of 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 the 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 creation of the language on the fly.
- Store all the content in different languages in resource files.
Resource is a 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 little cumbersome as it requires more technical knowledge.
Based on the requirement and priorities, different approach can be selected for development. This article discus on using resource files for development.
Before going in details of how to create resource, however, we need to understand what is meant by "globalization", "localization" and "culture" in this context.
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.
Localization is process of creating and configuring your application for a specific language.
A 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. A culture is represented as shown in the following example:
The first culture represents English language being spoken in US where as the second culture represents English culture being spoken in Great Briton.
To work with culture 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.
// Put the using statements at the beginning of the code module
// Put the following code before InitializeComponent()
// Sets the culture to French (France)
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
// Sets the UI culture to French (France)
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 culture values, Culture and UICulture. The Culture value determines or influences 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.
- In 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.
// Import these namespaces at the beginning of the code module.
// Set the culture and UI culture to the browser's accept language
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages);
3.7 Resource Files
A resource is a 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.
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:
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 same as mentioned above. However, you need to follow certain naming convention. The resource source files for different locales should have names matching following syntax.
<base file name>.<locale>.txt
<base file name>.<locale>.resx
Note that a file without 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 above mentioned resource 'source' files using ResGen utility that ships with .NET SDK
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 later stage after main application has been deployed. They are ideal for product based development where in 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 in to 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:
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 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 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 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
Dim ci As New CultureInfo("en-GB")
Thread.CurrentThread.CurrentCulture = ci
x = New ResourceManager("mymessage.en-GB",[Assembly].GetExecutingAssembly.GetSatelliteAssembly(ci))
Here, we have created an instance of CultureInfo class by passing culture code "en-GB" to its constructor. Then we set this culture as the current culture of running thread. We then create an instance of ResourceManager class by specifying resource base name. The second parameter of ResourceManager constructor is of our interest. We are passing the Satellite Assembly that contains resources for specified culture. We get this Satellite Assembly by GetSatelliteAssembly() method of the executing assembly. Finally, we get 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.