|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Table of Contents
I'd like to thank Jean-Claude Manoli for developing his C# Code format, which Ii used in writing this tutorial. IntroductionDeveloping websites to support multiple languages can be a challenging and time-consuming process. With standard HTML pages, this involves creating and maintaining duplicate versions of each page for each supported language as well as having the language content embedded into the HTML, where content can’t easily be edited. While the process improved slightly with the introduction of scripting technologies such as ASP and PHP, no significant development or maintenance time was saved. For those of you who have to develop multi-lingual interfaces and applications, you’ll be glad to know that ASP.NET makes things considerably easier. ASP.NET and the .NET framework ship with support for multilingual
applications, namely in the form of Resource Files, the In this first part, we'll develop a custom resource manager which avoids the limitation of .NET Assembly Resource Files as well as extend a number of classes to easily support localization. In the second part, we’ll spend more time talking about creating multilingual applications, specifically looking at database implementations and techniques. By the end of this tutorial, you should be able to create multilingual applications with a minimum of work and maintenance, and be able to easily add new languages to it later on. Before we get startedIf you aren't familiar with localization in .NET, don't worry. This tutorial mostly skips what's available in .NET and talks about alternatives to make the job easier. There are a couple of core principals you should know though. (Even if you don't know the basics, you can skip this section and download the simple application I've created to showcase the core functionality, playing with it will probably be the best way to understand). BasicsThe way localization works in .NET is fairly straightforward. Content is
stored in pretty simple XML files called Resource Files. You create a Resource
File for each supported language (more can be added later on). When the
application is compiled, the resource files are embedded into assemblies - the
default resource file is embedded in the main assembly (.dll file);
language-specific resource files are embedded into their own assemblies called
satellite assemblies. Resource files are pretty simple and look a lot like a
hashtable, they have a name and a value - the name is the same for all resource
files, and the value is a language specific translation of some content. In
essence, this allows you to use the
1: UserNameLabel.Text = myResourceManager.GetString("Username");
2: UserNameValidator.ErrorMessage =
myResourceManager.GetString("RequiredUsername");
The resource manager will automatically load the right resource file based on
the current thread's
CulturesIt's important to have a good understanding of Cultures since our new code
will make use of them - specifically the
1: CultureInfo c = new CultureInfo("en-US");
//creates a CultureInfo instance for American English
2: CultureInfo c = new CultureInfo("en-AU");
//creates a CultureInfo instance for Australian English
3: Cultureinfo c = new CultureInfo("he-IL");
//creates a CultureInfo instance for Israel Hebrew
Once you have a 1: CultureInfo c = new CultureInfo("en-US");
//creates a CultureInfo instance for American English
2: System.Threading.Thread.CurrentThread.CurrentCulture = c;
//Will automatically format dates and such
3: System.Threading.Thread.CurrentThread.CurrentUICulture = c;
//Used by the ResourceManager to get the correct XML File
In part 2, we'll discuss ways to figure out which culture to load, but for
now, it can be as simple as passing a code in the QueryString. For example, when
lang=f is present, the French Canadian culture should be used. The other
key factor is where to do all of this. The simplest and most logical place is in
the Global.Asax's
Download dummy applicationThe best way to understand the basics is to play with some code. I've created an extremely basic VB.NET web application to demonstrate the basic principles. Download it and play with it. Look at the structure of the 3 resource files, the codebehind for index.aspx, and the code in global.asax. Download sample - 16.6 Kb.
Why not use what's available as-is?While it's certainly possible to develop a multilingual application with the tools provided with ASP.NET, there are a number of limitations which make the task less than streamlined. Some of the key problems are:
While the list might seem small, the above three issues can be quite serious - with the first being the worst. For example, since resource files are embedded into assemblies, it's very difficult to ship a product which provides the client with the flexibility to change the content - a feature offered by many products. At my previous job, every time the translation department wanted to change some text, we'd need to recompile the entire application, stop 20 web servers, and copy the .dll into the bin folder - a frustrating process.
Building a better Resource ManagerOur first task is to build a better Resource Manager which won't cause our Resource Files to be embedded into assemblies. This will allow Resource Files to be easily edited in a production or client environment. Our core functionality will be located in three functions:
GetString() 1: public static string GetString( string key) {
2: Hashtable messages = GetResource();
3: if (messages[key] == null){
4: messages[key] = string.Empty;
5: #if DEBUG
6: throw new ApplicationException("Resource" +
" value not found for key: " + key);
7: #endif
8: }
9: return (string)messages[key];
10: }
The method accepts a single argument, the key of the resource we want to get.
It then retrieves a GetResource() 1: private static Hashtable GetResource() {
2: string currentCulture = CurrentCultureName;
3: string defaultCulture =
LocalizationConfiguration.GetConfig().DefaultCultureName;
4: string cacheKey = "Localization:" +
defaultCulture + ':' + currentCulture;
5: if (HttpRuntime.Cache[cacheKey] == null){
6: Hashtable resource = new Hashtable();
7:
8: LoadResource(resource, defaultCulture, cacheKey);
9: if (defaultCulture != currentCulture){
10: try{
11: LoadResource(resource, currentCulture, cacheKey);
12: }catch (FileNotFoundException){}
13: }
14: }
15: return (Hashtable)HttpRuntime.Cache[cacheKey];
16: }
The First, the default culture is loaded [line: 8], and then the current culture is loaded [line: 11]. This means if a key is defined in both XML files (which most should be), the default value will be overridden by the culture-specific value. But if it doesn’t exist in the culture-specific value, the default value will be used. LoadResource() 1: private static void LoadResource(Hashtable resource,
string culture, string cacheKey) {
2: string file =
LocalizationConfiguration.GetConfig().LanguageFilePath +
'\\' + culture + "\\Resource.xml";
3: XmlDocument xml = new XmlDocument();
4: xml.Load(file);
5: foreach (XmlNode n in xml.SelectSingleNode("Resource")) {
6: if (n.NodeType != XmlNodeType.Comment){
7: resource[n.Attributes["name"].Value] = n.InnerText;
8: }
9: }
10: HttpRuntime.Cache.Insert(cacheKey, resource,
new CacheDependency(file), DateTime.MaxValue, TimeSpan.Zero);
11: }
Other enhancementsWrappersThere are a number of minor enhancements which can be done to our Resource Manager class. For example, I build bilingual webpages in English and French. Annoyingly, in English, a colon is always glued to the word it follows, but in French there has to be a space. For example: Username: //English
Nom d'utilisateur : //French
This means, the colon needs to be localized. Instead of using the
1: public static string Colon {
2: get { return GetString("colon"); }
3: }
In our English resource file, the colon would simply be ':', while in the French one, it would have a space ' :'. Strongly-typed resourcesThe reason we use Localized ControlsOur next goal is to make our life easier when developing a website by expanding existing server controls (literals, labels, buttons) to be localization-aware. We begin by creating a very simple interface our new controls will implement. ILocalized1: public interface ILocalized{
2: string Key {get; set; }
3: bool Colon {get; set; }
4: }
LocalizedLiteral 1: public class LocalizedLiteral : Literal, ILocalized {
2: #region fields and properties
3: private string key;
4: private bool colon = false;
5:
6: public bool Colon {
7: get { return colon; }
8: set { colon = value; }
9: }
10:
11: public string Key {
12: get { return key; }
13: set { key = value; }
14: }
15: #endregion
16:
17:
18: protected override void Render(HtmlTextWriter writer) {
19: base.Text = ResourceManager.GetString(key);
20: if (colon){
21: base.Text += ResourceManager.Colon;
22: }
23: base.Render(writer);
24: }
25: }
The first web control that we'll look at making localization-aware is the
oft-used Rinse, wash and repeaterYou can copy and paste the same code over and over again and simply change the name of the class and what it inherits from; for example, let's do a localized button: 1: public class LocalizedButton : Button, ILocalized {
2:
3: #region Fields and Properties
4: private string key;
5: private bool colon = false;
6:
7: public string Key {
8: get { return key; }
9: set { key = value; }
10: }
11:
12: public bool Colon {
13: get { return colon; }
14: set { colon = value; }
15: }
16:
17: #endregion
18:
19: protected override void Render(HtmlTextWriter writer) {
20: base.Text = ResourceManager.GetString(key);
21: if (colon){
22: base.Text += ResourceManager.Colon;
23: }
24: base.Render(writer);
25: }
26: }
Notice that only the two bolded words have changed. When desired, you can expand the functionality. For example, it isn't
uncommon to have a 1: using System.Web.UI;
2: using System.Web.UI.WebControls;
3:
4: namespace Localization {
5: public class LocalizedLinkButton : LinkButton, ILocalized {
6: #region Fields and Properties
7: private string key;
8: private bool colon;
9: private string confirmKey;
10:
11: public string ConfirmKey {
12: get { return confirmKey; }
13: set { confirmKey = value; }
14: }
15: public string Key {
16: get { return key; }
17: set { key = value; }
18: }
19: public bool Colon {
20: get { return colon; }
21: set { colon = value; }
22: }
23: #endregion
24:
25: protected override void Render(HtmlTextWriter writer) {
26: if(key != null){
27: Text = ResourceManager.GetString(key);
28: if (colon) {
29: Text += ResourceManager.Colon;
30: }
31: }
32: if (confirmKey != null) {
33: Attributes.Add("onClick", "return confirm('" +
ResourceManager.GetString(confirmKey).Replace("'",
"\'") + "');");
34: }
35:
36: base.Render(writer);
37: }
38:
39: }
40: }
Using Localized ControlsYou use the localized controls like any other server control. First, register the control on the page: 1: <%@ Register TagPrefix="Localized"
Namespace="Localization" Assembly="Localization" %>
Then, without having to write any code, you can simply add the control either by drag and dropping it in the designer, or in the HTML mode by typing: 1: <Localized:LocalizedLiteral id="passwordLabel"
runat="server" Key="password" Colon="True" />
2: <Localized:LocalizedButton id="login"
runat="server" colon="false" Key="login" />
DownloadThe best thing to do now is to play a bit with some code. I've again created
a sample site (similar to the previous one), but this time using our new
Resource Manager class and Localized controls. You might need to change the
web.config's ContactKarl Seguin – karlseguin@hotmail.com - 8/14/2004. | ||||||||||||||||||||