Browse DevX
Sign up for e-mail newsletters from DevX


Internationalize Your ASP.NET Applications (Part 1 of 3)  : Page 6

Not only does ASP.NET simplify Web application development, it also provides comprehensive support for internationalization.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

In addition to handling the text in articles stored in the database, the example site also localizes smaller pieces of text on the page—such as the Copyright message—by using resource (resx) files. You can create resource files in Visual Studio .NET, or create resources in text files and convert them to resx files. You can also create resource files programmatically using the System.Resources.ResourceWriter class.

In the example the English-language resource file Resources.resx. ResX files are XML resource files and are very easy to hand edit. Because they're humanly readable you can translate them directly. They can contain serialized objects as well as textual information. The sample site contains resource strings for a welcome message, a copyright message and for the text above the culture drop-down list. Figure 5 shows how Resources.resx looks in the Visual Studio .NET XML editor:

Figure 5: The Resources.resx file in the Visual Studio .NET XML editor.

Note that there are fields containing the string value, a brief comment describing its purpose, and a name for the string resource. The code uses the name field value to retrieve individual strings from the resource files.

VS.NET stores default-language resources within the main assembly. To create a French equivalent you create another resource file called Resources.fr.resx. The naming is important as Visual Studio .NET uses the filename to maintain the logical relationship between these files and pass that information to the compiler. In this case it uses the "fr" in the filename Resources.fr.resx to determine that this is the French version of Resources.resx. Figure 6 shows the French version.

The French version contains only two strings so I can show you how the site handles a missing resource string.

You can repeat the resource file process for each language. If you now rebuild the project and look in the bin folder of the Web site, you'll see something very interesting. Although the primary English resource file (Resources.resx) is embedded in the main assembly, Visual Studio.NET creates the other three (French, Chinese and Japanese) as their own assemblies and places them in separate folders named using the culture identifier (see Figure 7).

Figure 7: VS.NET stores primary-language resource files in the main assembly, but creates subfolders to hold resource files for each additional language.

Author Note: The reason I set the Chinese-China culture as zh-CN rather than zh is that for political reasons there is no high-level culture zh.

If you are not using Visual Studio .NET, you can embed the main resource file in the main assembly by using the /res compiler flag. You can create satellite assemblies using the al.exe utility.

After compiling the resources into a useful form, you can access them from code and ensure that you select a string for the most suitable culture. To do that I created another server control called SiteTextControl that you can add to the page wherever you want to display localized text. The server control has a public property to set the name of the string value that you want to access, such as"Copyright"

public string Name { get { return textName; } set { textName = value; } }

The CreateChildControls() method renders the control. In this case it just writes the relevant resource text without any HTML tags. To retrieve the text, create a System.Resources.ResourceManager instance that provides access to the resource files. To instantiate the ResourceManager object, pass it the name of the resource file to use, and a reference to the currently executing assembly so it knows where to look for the resource strings.

ResourceManager res = new ResourceManager ("Multilingual.Resources", Assembly.GetExecutingAssembly());

To pull out the resource string value, call the GetString() method and supply the textName property that you set using the Name property of the server control.

string text = res.GetString(textName);

Finally create a literal control to hold the resource string and add it to the page control structure.

LiteralControl literal = new LiteralControl(text); Controls.Add(literal);

Notice that code doesn't have to manage the culture. The .NET framework does that for you, because the culture of the thread has already been set by the code in Application_BeginRequest();.

For example, to add the Copyright message to the article page, add a SiteTextControl control to the page and specify the name of the resource display using the Name property.

<NewsSite:SiteTextControl runat="Server" id="CopyrightMessage" Name="Copyright" />

After adding the control, and refreshing the article page the Copyright message appears (see Figure 8).

If you change the culture using the drop-down list, the control displays the Copyright message in the selected language:

Now suppose you wanted to add a caption above the drop-down list to prompt visitors to select their language. Unfortunately, you may remember that the ChooseLanguage string resource exists only in the UK English resource file (Resources.resx). To add the caption you need to make a change to the CreateChildControls() method inside the server control that displays the drop-down list. I added the following code just before the drop-down list was created:

SiteTextControl text = new SiteTextControl(); text.Name = "ChooseLanguage"; Controls.Add(text); literal = new LiteralControl("<br />"); Controls.Add(literal);

If you refresh the site it displays the new drop-down list caption. The site defaults to using the resources in the main assembly when it doesn't find relevant resources in the satellite assemblies; therefore, if you view a French page (see Figure 9), you'll see that because the French resource doesn't exist, the drop-down list caption defaults to UK English. If you add resource strings to translate the drop-down caption into other languages, the page would display those without any other code changes.

A framework like this is relatively simple to implement. After it's in place it's important to develop a process for inserting text strings into pages using resources rather than embedding strings directly into your code or ASP.NET page files.

In this article you have seen how to put together a simple Web site that uses some of the core localization features of .NET including the CultureInfo object and resource files. You have seen how to alter the culture setting at the page level and also how to insert localized content into the page from both a database, and from a resource file.

In the next part of this article I'll show you how to manage the localization of graphics, and also how to select the most suitable culture for Web site visitors automatically when they first arrive at the site.

Ollie Cornes has been working with the Internet and the Microsoft platform since the early 90's. His roles have included programming, technical authoring, writing, project management. He has co-written several .NET books for Wrox Press including "Beginning ASP.NET With C#" and "Professional C#" and has worked on Internet projects with Microsoft, Saab, Demon Internet (UK), Tesco (UK) and Vodafone. Ollie recently founded RichTextBox.com, which sells the formatted text editing control for ASP.NET. You can reach Ollie at ollie@cornes.org.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date