Internationalization basics in C#

Introduction

The computer is now in use to give support to different languages other than English. Many major languages of the world like Arabic, Japanese and German are provided for dealing with applications in these languages. This article will tell how to implement multilingual applications using C#.

This code explains the basic understanding and development of multilingual applications in .NET. Please find the zipped file for the same.

Steps for creation of multilingual application:

  1. Create the resource files for various languages.
  2. Access the resource file data from the web pages.
  3. Unicode (multilingual) data into the database.
  4. Multilingual data from the database.

The Resource Files:

Creating Resource Files:

The resource files are one type of base for multilingual support. The text for different languages is written in Notepad. The text files are compiled into "resources" using the resource compiler resgen.exe.

The text "=" is in English in each file. But the text on the right of the "=" is the text matter in the particular language. E.g.:

  • mytext.ja-jp.txt for Japanese language resources,
  • mytext.en-us.txt for US English,
  • mytext.de-de.txt for German (Germany),
  • mytext.ar-sa.txt for Arabic (Saudi Arabia).

Standard identification of different languages is done as:

  • "ja-jp" is the ISO string for Japanese - Standard.
  • "en-us" is the ISO string for English - US.
  • "de-de" is the ISO string for German - Standard.
  • "ar-sa" is the ISO string for Arabic - Saudi Arabia.

After creation of text file for different languages, compile them using resgen.exe. The exe is provided with the .NET framework.

The command is as follows:

Resgen C:\Inetpub\wwwroot\Multilingual\resources resgen resources\mytext.txt ResgenC:\Inetpub\wwwroot\Multilingual\\mytext.de-de.txt Resgen C:\Inetpub\wwwroot\Multilingual\resources\mytext.de-de.resources resgen resources\mytext.en-us.txt  resources\mytext.en-us.resources resgen resources\mytext.ar-sa.txt  resources\mytext.ar-sa.resources resgen resources\mytext.ja-jp.txt   resources\mytext.ja-jp.resources

The above commands will generate the resource files.

Identifying The Client Default Language:

In global.asax.cs, namespaces used are:

using System.Globalization; using System.Threading; using System.Resources;
  • System.Globalization used for the CultureInfo.
  • System.Threading contains the Thread class, which allows us to change the Culture for the current Thread.
  • System.Resources used to access the Resource Manager, to access culture-specific resources at runtime.

In Application_Start, the following code is written. Fires when the application is started and all values are stored in application ["RM"].

Application["RM"] =   System.Resources.ResourceManager.CreateFileBasedResourceManager("mytext",   Server.MapPath("resources") + Path.DirectorySeparatorChar,null);

In Application_BeginRequest, code is written as:

try {   Thread.CurrentThread.CurrentCulture =                         new CultureInfo(Request.UserLanguages[1]); } catch (Exception ex) {   Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); } Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

The above code gets the client default language in ISO string format. Code creates a CultureInfo and assigns the CultureInfo to the current thread. All the resources have to be picked up from the resource file matching the ISO string. The current Culture will be based on the client language.

Code in defalt.aspx.cs:

Thread.CurrentThread.CurrentCulture =             new CultureInfo(Request.UserLanguages[0]); Thread.CurrentThread.CurrentUICulture =             new CultureInfo(Request.UserLanguages[0]);

The Request object's UserLanguages property is an array holding all the ISO strings representing the languages which are set within the system, and the first element is the default language.

So, in the Page_Load event, identify the user's language and display the first page in the  language which was selected by the user.

Accessing Data from Resource Files:

Text are displayed on the browser as per language basis, the page gets data from the required resource file.

Once the thread Culture is set to the new language culture, the Resource Manager will get data from the specific resource file which contains the Language ISO String in the file name. If it cannot find it, it picks up data from the default resource file. The default resource file is mytext.txt.

To access the data from resource files, ResourceManager object is used. The object accesses data from the specific resource file. The object has to be defined as public.

public ResourceManager rm

The object's GetString() function is used to get data from the resource File. The above specified code will read the resource file for the language specified in the thread culture. All the data comes from the resource file.

The Database (MS SQL Server)

Creating the Database for Multilingual:

  1. While crating tables, data types should be nvarchar, nchar and ntext because they support multilingual data (data in Unicode format).
  2. Use the following INSERT query format:
    INSERT INTO MultiLang (userfname, userlname,     userlangid, useraddress) VALUES(N'" + Request.Form["txtFName"] +     "', N'" + Request.Form["txtLName"] + "','" +     Request.QueryString["lang"] + "', N'" +     Request.Form["txtAddress"] + "')";
    • N' : the Unicode data

    The Unicode value is required to be preceded by N.

  3. For SELECT query.
    SELECT * FROM MultiLang WHERE userfname =N'The Unicode Data'

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mishra Krishna


--
Alain Lompo
Excelta - Conseils et services informatiques
MCT
MCSD For Microsoft .Net
MVP Windows Systems Server / Biztalk Server
Certifié ITIL et Microsoft Biztalk Server

Commentaires

Posts les plus consultés de ce blog

Printing and Graphics

Solution Items

Printing in C#