Implementing Multilingual Applications in Microsoft .NET
page 5 of 12
by Joydip Kanjilal
Feedback
Average Rating: 
Views (Total / Last 10 Days): 53234/ 85

Resource Files

A resource file consists of non-executable data that is required by the application and is deployed along with it.  Bitmaps, icons, cursors, etc. are typical examples of resource files.  Microsoft .NET provides support for multilingual applications using resource files.  The resource files used to create a multilingual application contain the culture dependent resources for the application.  A resource file can be used to specify the locale-specific settings.  Note that to support each locale in the application, there should be one resource file created.  These resource files are then compiled into satellite assemblies and accessed by the application.  Resource files can be either text files or resx files in .NET.

The basic advantages of using resource files are given below.

·         Support for Globalization with isolation of the resource content from the application

·         Reusability and the provision for change of the resource content without the need to change the application's code

It should be noted that each resource file has support for a specific culture.  Hence, if we need to have support for "n" cultures, we need to have "n" different resource files.

Creating Resource Files

First create a resource file using the VS.NET IDE and save using either a .txt or a .resx extension.  The resource file should be created using the intended culture if it is to be used for Globalization or Localization purposes.

When creating resource files for specific locales, the following naming convention should be followed.

<base file name>.<locale>.txt

or

<base file name>.<locale>.resx

Therefore the resource file targetted at en-GB locale should be named as TestResource.en-GB.txt or TestResource.en-GB.resx.  Note that the resource file name TestResource.en-GB.resx contains the name of the resource that it is intended at.

The ResourceWriter class in the System.Resources namespace is used to create a resource programmatically.  The sample code below creates a resource file called Test.Resources in the root directory of the C drive.

Listing 3

using System;
using System.Resources;
class CreateResources
{
  public static void Main(string[]args)
  {
    ResourceWriter rw = new ResourceWriter("C:\\Test.resources");
    rw.AddResource("CopyRight", "CopyRight Message in English");
    rw.Close();
  }
}

The ResourceWriter class can also be used to store any other serializable object in the resource file.

Reading Resource Files

The content of the resource files can be read in the application using the ResourceManager class defined in the System.Resources namespace.  The ResourceManager class looks up culture-specific resources and provides convenient access to culture-specific resources at runtime.  According to MSDN, "The Resource Manager class looks up culture-specific resources, provides resource fallback when a localized resource does not exist, and supports resource serialization."

Listing 4

ResourceManager resourceManager = new
 ResourceManager("Internationalization.en-GB"+culture, Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new CultureInfo(culture);
string message = resourceManager.GetString("ID",cultureInfo);

Compiling Resource Files

Refer to the section above.  Note that we had created a resource file named TestResource.en-GB.resx.  Now let us compile the resource using the ResGen utility shipped with the Microsoft .NET Framework to create a compiled resource that would have a .resources extension.

Listing 5

resgen Internationalization.en-GB.resx  Internationalization.en-GB.resources

When used, the above command line tool would compile the .resx file to a compiled binary .resources file for the en-GB locale.


View Entire Article

User Comments

Title: What about localized Databases?   
Name: Rojalin
Date: 2006-09-28 6:54:50 AM
Comment:
many many tx for this article
Title: Implementing Multilingual Applications in Microsoft .NET   
Name: rambabu
Date: 2006-09-12 7:22:14 AM
Comment:
The article is very nice and the author explained in simple terms to understand the various aspects of Globailization and Localization.I liked this article very much.
Title: What about localized Databases?   
Name: Max
Date: 2006-09-12 4:58:25 AM
Comment:
Hi,
thank you very much for your article!
I have always been looking around for a standardized way of localizing db-entries. For instance if I run a web-shop which must be available to german and english customers my descriptions of my articles must be in german and english.
Of course this has to be stored in a DB and not in embedded resources.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 8:50:58 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search