Multiple Language Website
Date posted: 16/05/2013
You haven't filled in compulsory values.
The email is not correct
More than 50% of the web's sites have English content. English can easily be regarded as the internet language since most users, no matter where they are from, can understand what an English page is about. There may also be websites that apart from English wish to have content in their own language or offer more optional languages to the user. A worldwide website is bound to fail unless every client feels comfortable enough to take a look at it.
Resource files
In order to create a website with more than one language, ASP.NET uses resource files (.resx). A resource file is an XML file that can store different values for a key depending on the language used. Each language supported requires an extra resource file. For example look at the way GoodMorning is stored in the following resource files.
These files will return the string "Good Morning" if we are using English . In case of French we would get "Bonjour" and in case of German, "Guten Morgen".
There are two types of resources, global and local. Global resources can be used anywhere. Local resources are restricted in the aspx page they are connected with.
What we are going to do is create three resource files. The default file is for English, and two more for French and German. There are two types of resources. All global resource files should be placed within a file called App_GlobalResources and the local ones within an App_LocalResources file.
Let's create the global resource files. Under the App_GlobalResources file we created, we will place the three resource files (one for each language).
-
Resource.resx will be the default language (English) resource file.
-
Resource.fr-FR.resx will be the French resource file.
-
Resource.de-DE will be the German resource file.
A resource file should be named after the convention resource_name.language_culture_code.resx
The language_culture_code in the previous line stands for a string that represents the language. For example "en" stands for English. However we can use extra info that stands for the country this language is spoken as well. "en-GB" stands for United Kingdom English while "en-US" stands for United States English. In the example, "fr-FR" refers to France French and "de-DE" to Germany German.
Global Resource Files
Let's create the App_GlobalResources file first. This file should be placed top-level in our project. Then we place the three resource files mentioned above, within it. These resource files contain all the values we would like to translate when changing languages.
We are ready to go. All we need is a simple aspx file containing a Label where we will place our text and a DropDownList which we will use to pick the desired language.
<asp:Label runat="server" ID="MultiLanguageLblID" /> Paul!
<br />
<asp:Label runat="server" ID="MultiLanguageDateLblID" />
<br />
<asp:DropDownList runat="server" ID="LanguagesListID" AutoPostBack="true">
<asp:ListItem Value="en-US" Text ="English" />
<asp:ListItem Value="fr-FR" Text="Francais" />
<asp:ListItem Value="de-DE" Text ="Deutsch" />
</asp:DropDownList>
Multi-Language.aspx
Now we need to use the resource files. It's quite easy. We can use the Resources library and simply ask for Resource.GoodMorning. Resource is the primary name of the resource files we crated. Another way would be to use GetGlobalResourceObject, using the name of the resource files and the name of the string to be translated.
This is what our Page_Load would look like.
protected void Page_Load(object sender, EventArgs e)
{
//Get the value from the global resource file
MultiLanguageLblID.Text = Resource.GoodMorning;
//We could have used GetGlobalResourceObject instead
//MultiLanguageLblID.Text = (String)GetGlobalResourceObject("Resource", "GoodMorning") ;
}
Building this code we would get
Good Morning Paul!
However no matter if we choose another language, nothing changes. That's because we still need something else to be done.
It is a good idea to set or change the language at a very early state of the page lifecycle. Even before Page_Init is called. We can override the InitializeCulture method. This is the default method for ASP.NET to do your language stuff. What we are going to do is get the DropDownList value and use to set the new language values. Here's the code:
protected override void InitializeCulture()
{
//We need to use Request.Form as the controls have not yet been initialized
if (Request.Form["LanguagesListID"] != null)
{
string selectedLanguageValue = Request.Form["LanguagesListID"];
//We can use Page properties to set the language
SetLanguageUsingPage(selectedLanguageValue);
//Alternative we could use thread properties
//SetLanguageUsingThread(selectedLanguageValue);
}
base.InitializeCulture();
}
private void SetLanguageUsingPage(string selectedLanguage)
{
//Use the page properties
//Get the resource file values
UICulture = selectedLanguage;
}
private void SetLanguageUsingThread(string selectedLanguage)
{
//Use the thread properties
//Get the resource file values
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
}
There are two ways to set the language in order to choose your value from the resource files (UICulture). Using the UICulture property of the Page class or creating a new CultureInfo and set it to the Thread.CurrentThread.CurrentUICulture value. They are about the same and the result actually is the same (setting UICulture to "en-US" will set the CurrentUICulture to "en-US" as well and vice versa) so you can choose whatever you like most.
Now, when we build our code we finally get the long-expected result.
Global resources can be used anywhere, so if we needed the string "Good Morning" in, let's say, five pages, still we would only need one instance in each language file
Local Resource Files
Global resources can be used anywhere. However local resources can be used in only one aspx page.
To create resource files we have to create an App_LocalResources file and place it the same level as the aspx page is. The resource files we create within it are like the ones we created earlier. However the files should be named after the convention referenced_file_name.language_culture_code.resx
For example Multi-Language.aspx.de-DE.resx
Now, let's create a new label.
<asp:Label runat="server" ID="LocalLblID" meta:resourcekey="lblGoodByeResource" /> Paul!
This time, in order to use our resource files we will need the GetLocalResourceObject method that gets only the name of the string to be translated.
protected void Page_Load(object sender, EventArgs e)
{
LocalLblID.Text = (String)GetLocalResourceObject("GoodBye");
}
In addition to the global resource files, local resources can contain translation of controls attributes such as text and color.
The Culture Property
So far, we have seen how we can use the resource files to get a website with more than one language. Yet, there is still one last thing to do. There are methods, such as ToString, that depend on the language in order to create their output. For example
DateTime.Now.ToString("dddd MMMM d, yyyy")
will return
Thursday May 16, 2013
in English, but in French that should be
jeudi mai 16, 2013
To accomplish this we need to set one more property. That will be the Culture property. Remember how we dealt with the UICulture property? The same way we will deal with Culture.
protected override void InitializeCulture()
{
//We need to use Request.Form as the controls have not yet been initialized
if (Request.Form["LanguagesListID"] != null)
{
string selectedLanguageValue = Request.Form["LanguagesListID"];
//We can use Page properties to set the language
SetLanguageUsingPage(selectedLanguageValue);
//Alternative we could use thread properties
//SetLanguageUsingThread(selectedLanguageValue);
}
base.InitializeCulture();
}
private void SetLanguageUsingPage(string selectedLanguage)
{
//Use the page properties
//Culture dependent functions.
Culture = selectedLanguage;
}
private void SetLanguageUsingThread(string selectedLanguage)
{
//Use the thread properties
//Culture dependent functions.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
}
Once again we can either choose the Culture Page property or the Thread.CurrentThread.CurrentCulture.
A local resource file value can also be set right through the aspx file like this
<asp:Label runat="server" ID="LocalLblID" meta:resourcekey="lblGoodByeResource" /> Paul!
Keep in mind that if you do not set your own UICulture and Culture properties, ASP.NET will automatically do it for you getting the values the client's browser uses. It is not a good idea to let the browser decide what the client will see, so make sure that you are always aware of that.
This is the complete code
<!--Global resources region -->
<asp:Label runat="server" ID="MultiLanguageLblID" /> Paul!
<br />
<asp:Label runat="server" ID="MultiLanguageDateLblID" />
<br />
<asp:DropDownList runat="server" ID="LanguagesListID" AutoPostBack="true">
<asp:ListItem Value="en-US" Text ="English" />
<asp:ListItem Value="fr-FR" Text="Francais" />
<asp:ListItem Value="de-DE" Text ="Deutsch" />
</asp:DropDownList>
<!--Local resources region -->
<asp:Label runat="server" ID="LocalLblID" meta:resourcekey="lblGoodByeResource" /> Paul!
and the code behind file
protected override void InitializeCulture()
{
//We need to use Request.Form as the controls have not yet been initialized
if (Request.Form["LanguagesListID"] != null)
{
string selectedLanguageValue = Request.Form["LanguagesListID"];
//We can use Page properties to set the language
SetLanguageUsingPage(selectedLanguageValue);
//Alternative we could use thread properties
//SetLanguageUsingThread(selectedLanguageValue);
}
base.InitializeCulture();
}
private void SetLanguageUsingPage(string selectedLanguage)
{
//Use the page properties
//Get the resource file values
UICulture = selectedLanguage;
//Culture dependent functions.
Culture = selectedLanguage;
}
private void SetLanguageUsingThread(string selectedLanguage)
{
//Use the thread properties
//Get the resource file values
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
//Culture dependent functions.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
}
protected void Page_Load(object sender, EventArgs e)
{
//Get the value from the global resource file
MultiLanguageLblID.Text = Resource.GoodMorning;
//We could have used GetGlobalResourceObject instead
//MultiLanguageLblID.Text = (String)GetGlobalResourceObject("Resource", "GoodMorning") ;
MultiLanguageDateLblID.Text = DateTime.Now.ToString("dddd MMMM d, yyyy");
//Get the value form the local resource file
LocalLblID.Text = (String)GetLocalResourceObject("GoodBye");
}
You may also like to check out my latest post on how to use tour routing system in order to place selected language within the URL.
Conclusion
If you want people from all around the world to visit your website you should use multiple languages. To get different string output depending on the language, you can use resource files. Each file contains the representation for that language. There are two kinds of resource files. Global can be accessed everywhere; local can be accessed by a single page. To use resource files we should set the proper value to the UICulture. To use different languages in functions that depend on such info we should set the proper value to the Culture.
Back to BlogPreviousNext