![]() |
Languages »
C# »
How To
Intermediate
Multilingual applications using C#By Mishra KrishnaThis code explains the basic understanding and development of multilingual applications in .NET using C#. |
C#.NET 1.0, Win2K, ASP.NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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:
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.:
Standard identification of different languages is done as:
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.
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.
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.
nvarchar, nchar and ntext because they support multilingual data (data in Unicode format).
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.
SELECT query. SELECT * FROM MultiLang WHERE userfname =N�The Unicode Data�| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Apr 2004 Editor: Smitha Vijayan |
Copyright 2004 by Mishra Krishna Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |