Click here to Skip to main content
15,868,420 members
Articles / Web Development / ASP.NET
Article

Multilingual applications using C#

Rate me:
Please Sign up or sign in to vote.
1.81/5 (14 votes)
18 Apr 20043 min read 152.7K   5.3K   56   9
This code explains the basic understanding and development of multilingual applications in .NET using 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:

C#
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”].

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

In Application_BeginRequest, code is written as:

C#
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:

C#
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.

C#
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:
    SQL
    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.
    SQL
    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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralBe sure not to use the SQL in this article in your own projects. Pin
rlamoni16-Jul-08 10:16
rlamoni16-Jul-08 10:16 
QuestionMultilingual Web page Pin
reachmeharry17-Jun-08 8:36
reachmeharry17-Jun-08 8:36 
Generalhi Pin
prototype_nsx26-Jun-07 8:13
prototype_nsx26-Jun-07 8:13 
QuestionI want to use multiple files per culture how can i...? Pin
Naveen Kumar M14-Jun-07 19:37
Naveen Kumar M14-Jun-07 19:37 
AnswerRe: I want to use multiple files per culture how can i...? Pin
jeyma10-Jul-07 3:54
jeyma10-Jul-07 3:54 
GeneralRe: I want to use multiple files per culture how can i...? Pin
jeyma16-Jul-07 19:12
jeyma16-Jul-07 19:12 
GeneralHelp Pin
sreejith ss nair18-Sep-05 21:29
sreejith ss nair18-Sep-05 21:29 
Generalæ = æ does not work in dropdown Pin
willowwillowwillow16-Aug-04 0:32
willowwillowwillow16-Aug-04 0:32 
GeneralLocalizing Enums Pin
RayGT7-Jun-04 23:58
RayGT7-Jun-04 23:58 
OK, so far so good.
But how do i localize enums?
Regards
Ray

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.