Incorporate Localization in ASP.NET Using Resource Files

By | February 13, 2015

The primary reason behind website creation is to expand your business reach to audience located far and wide. But, what if your website cannot meet the needs of the global audience? Well, there is a strong possibility that some global users may be searching for things that you’re dealing into. But you may not be able to connect to users from across the globe as they cannot speak your language. With localization, you can make your website more user-friendly for visitors who speak other language than yours.

In order to add localization support, you can choose an approach where you need to re-create your website pages in different language. But, this approach is time-consuming and usually prone to errors, as it involves having numerous if statements and switch condition. Luckily, ASP.NET provides an effective way to build localized pages without the need to writing long lines of code, using resource files. This help users to obtain content or other data (also referred to as resources) according to their choice of language.

Assuming that you’re familiar with ASP.NET concepts, through this post, I would like to help you learn more about the resource files and how you can create one to render localization support.

An Overview of Resource Files

A resource file is an XML document that includes strings – that you need to translate into varied types of languages or image paths. The resource file contains content or other data (also referred to as resources). The web pages can access the resource files to obtain labels populated according to user’s choice of language. And, the best part is that you can create a separate resource file for each language (e.g. English, French and others) and cultures. At run time, the ASP.NET engine will choose the resource file that match up users locale and culture.

The resource files in ASP.NET are saved with an .resx extension. So, when you have to create a resource file, you’ll first have to begin with creating a base .resx file. Remember, when creating resource files for different languages (ideally the ones you want to support), make sure to create a new file having same name as that of its related resource file. However, you also need to include either the language or both the language and culture in the name.

For example, let’s create a few resource files:

  • WebResources.resx – This is default resource (or fallback) file.
  • WebResources.es.resx – This is a resource file for Spanish language
  • WebResources.es-mx.resx – This is a resource file that is created for the language and culture Spanish (Mexico) i.e. culture name.

In our example, the UI culture is Spanish. Now, to render a web page in Spanish language ASP.NET engine will utilize ‘WebResources.es.resx’ file compiled version. If the engine cannot find the correct match for the current UI culture, it will use resource fallback and will once again start looking for resources for any particular culture. If the specific culture cannot be found, ASP.NET will load the default resource file (i.e. WebResource.resx in our case).

Understanding How to Create Resource Files

In this section, I’ll take a working example to help you understand how you can create resource files. But before that, let’s look at some prerequisites that you’ll require for creating resource files:

  • Microsoft Visual Studio 2010 (not Microsoft Visual Web Developer Express).
  • .NET Framework

To understand the process of creating a resource file, let’s create a sample ASP.NET web page. Following is a test page displaying a welcome message via Label control. Additionally, there are two more labels: one label specifies the current date while the other one displays today’s date.ASP.NET Web Page

As you can see in the above screenshot, the labels are populated at design time. However, to support multiple language and/or cultures, I’ll need to obtain them from the local resource file. For this purpose, I’ll be using Visual Studio that offers a mechanism to create local resource files. For this, open your web page in the design view and then go to Tools>>Generate Local Resources. From there you can create a folder for your application, let’s say, App_LocalResources.
Next, create a local resource file for the same page and fetch the text values for your web page labels from the newly created resource file.ASP.NET Resource File

Here’s a screenshot of the resource file that contains text for all web page controls:Localized Resource File

The controls within that page will use the resource file to obtain the required contents:

<asp:Label ID="lblWelcome" runat="server" meta:resourcekey="lblWelcomeResource1">
</asp:Label>

Next, we’ll have to create local resource files for our web page so that it can obtain text from respective files on the basis of users culture. The format of the resource file for the page will look something like:

PageName.aspx.languageId-cultureId.resx.

In case, the resource file for any specific combination of language and culture is not found, then the default resource file i.e. Default.aspx.resx will be taken.
Let us assume, you want to display the page in Hindi-Indian combination, in that case you’ll first need to create a resource file named Default.aspx.hi-in.resx.Resource File for Hindi-Indian

Until now you won’t possibly see any changes when you’ll run that page, making you unable to test the changes. In order to test the changes, we’ll have to set our browser’s default language to Hindi-Indian.Change browser language preference

Now when we’ll run the page, the ASP.NET engine will analyze if user setting determines language and region (or culture) as Hindi and Indian respectively. And then, it will begin fetching the text from the newly created resource file.Culture in Resource File

Note: In case I’ll use the same process to provide localization support for French language, in that case the default file will be taken if the French resource file won’t exist. In that case we’ll need to create a separate resource file for French.

A word of caution, you cannot always trust your user’s browser and system settings to obtain information regarding a specific culture and/or language. And so, it is recommended that you should provide an option to your web page visitors, allowing them to change the language and culture for that page. For instance, you can provide users with a dropdown to pick the language of his choice.

To do so, we’ll need to override the function InitializeCulture() and then change the UICulture based on the user selected language, as shown in the following code:

protected override void InitializeCulture()
{
if (Request.Form["DropDownList1"] != null)
{
UICulture = Request.Form["DropDownList1"];
}
base.InitializeCulture();
}

Output:

The above code will help you provide the users with an option to change the language dynamically and quickly. However, you might encounter a problem here: the one I have highlighted in a red square in the above image. Although the UI appears in Hindi but the date is not displayed in the Indian format. To resolve the problem, what we need to do is to set the Culture to the selected culture option. This will ensure that the date, and any other format settings will be displayed based on specific region.Displaying Culture with Specific Region

protected override void InitializeCulture()
{
if (Request.Form["DropDownList1"] != null)
{
//for UI elements
UICulture = Request.Form["DropDownList1"];
//for region specific formatting
Culture = Request.Form["DropDownList1"];
}
base.InitializeCulture();
}

So, that’s it! Here’s hoping that reading this post will help you become familiar with resource files and how you can create one for adding localizing your ASP.NET website pages.

About the Author – Mike Swan
Mike Swan is an experienced WordPress developer associated with Markupcloud Ltd, a PSD to WordPress service provider. He is keen on sharing his innovative ideas related to web design technologies and development.

Top 10 Interview Questions and Answers Series: