Developing multilingual applications in ASP.NET (Part - II)
Introduction
In the last article of this series we saw how ASP.NET built-in controls such as calendar, provide multilingual support. The main part of your web forms, however, will consist of labels and similar text that also need to be in the respective language. This part of the article will tell you how to do that using resource files.
What are 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.
What are 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
How to create "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
How to create .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
How to create resource files for different locale?
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.
How to compile 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.
How to use .resources files in an application?
There are two ways of using resource files
- In which you explicitly specify a resource file irrespective of current locale-from which to extract the resources
- In which you embed the resource files in an assembly and then depending on
the locale specify the assembly from which to extract the resources. If you
recall from the previous article such "resource only" assemblies are called as
Satellite Assemblies.
How to explicitly read resource from resource files?
The first approach is illustrated in the following example.
Dim x As Resources.ResourceManager
x = ResourceManager.CreateFileBasedResourceManager
("mymessage.en-GB", ".",Nothing)
Label1.Text=x.GetString("message")
Here, we have created a variable of type ResourceManager class. This class
resides in System.Resources namespace. We then call
CreateFileBasedResourceManager shared(static) method that actually creates an
instance of it. We passed three parameters to the method. One - the resource
name for a specific locale. Two - the folder in which the .resources file
exists. Three - resource set which in our case is Nothing. This means that the
default resource set will be used.
Summary
In this article we saw how to create resource files and how to retrieve the
resources at run time. In the next article we will see how to create Satellite
Assemblies.