Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » Internet & Web » Multilingual Applications in .NET - Part I

Multilingual Applications in .NET - Part I

This article explains the basic understanding on different methods of developing multilingual applications in .NET.

Page Views : 9995
Downloads : 0
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

1. Introduction

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.

2. General

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.

3. Approach

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.

3.1 Globalization

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.

3.2 Localization

Localization is process of creating and configuring your application for a specific language.

3.3 Culture

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:

en-US
en-GB
fr-BE

The first culture represents English language being spoken in US where as the second culture represents English culture being spoken in Great Briton.

3.4 Namespaces

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
using System.Threading;
using System.Globalization;
// 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.
    culture="en-US"
    uiCulture="de"

    -Or-

  • 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.
using System.Globalization;
using System.Threading;
// Set the culture and UI culture to the browser's accept language
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
Thread.CurrentThread.CurrentUICulture =
new CultureInfo(Request.UserLanguages[0]);

3.6 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.6.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.6.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.

sample1.txt
label1=Hello World

3.6.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:
<data name="label1">
<value>Hello World</value>
</data>

Note that for all resource files the Build Action should be set to Embedded Resource.

3.6.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

e.g
mymessage.en-US.txt
mymessage.en-GB.txt
mymessage.fr.txt

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.6.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

e.g.
resgen mymessage.txt
resgen mymessage.resx

Above utility will output mymessage.resources file after successful execution.

3.7 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.7.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.7.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:

AL
/t:lib
/culture:en-GB
/embed:mymessage.en-GB.resources
/out:ResourcesDemo.resources.dll

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.7.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.7.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.7.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))
Label1.Text=x.GetString("label1")

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.

8. References

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
pradeep.kellangere
Pradeep K. V. is working with Wipro Technologies.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.