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:
- Create the resource files for various languages.
- Access the resource file data from the web pages.
- Unicode (multilingual) data into the database.
- 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
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:
System.Globalization used for the
System.Threading contains the
Thread class, which allows us to change the
Culture for the current
System.Resources used to access the Resource Manager, to access culture-specific resources at runtime.
Application_Start, the following code is written. Fires when the application is started and all values are stored in application [“RM”].
Server.MapPath("resources") + Path.DirectorySeparatorChar,null);
Application_BeginRequest, code is written as:
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:
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
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:
- While crating tables, data types should be
ntext because they support multilingual data (data in Unicode format).
- 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"] + "')";
The Unicode value is required to be preceded by
SELECT * FROM MultiLang WHERE userfname =N’The Unicode Data’