Whenever I start an ASP.NET project I always put the localizable strings in a separate resource file. For starters it is quite common nowadays that a website needs to support different languages. Even when that is not the case, keeping the strings in a separate file is more orderly that having them mixed with the code, and it makes it easier,in the future, to alter the texts, or translate them. In other words, the cost of preparing your site as if it were to be multi-language is negligible, while the benefits are quite obvious.

Once we have prepared our site to use resource files for our localizable strings we need to set up a way to select the appropriate language to use in the UI.

We can either let the user select the language he wants to use, or we can rely on the browser configuration to decide which language to display.

In this post we will focus on how to choose the language based on the browser preferences: notice that if we do not do configure this correctly, the site will keep stubbornly displaying the default language.

Before we move to that part, though, let’s remember how to add resource files to an ASP.NET project.

How to add resource files to ASP.NET webs

In ASP.NET applications your resource files live in a folder called App_GlobalResources: right click on the project, and select Add >> Add ASP:NET Folder >> App_GlobalResources to add it.

Adding App_GlobalResources folder
Add App_GlobalResources folder

Right click on the created App_GlobalResources folder to add a resource file:

App_GlobalResources
Add a .resx file

Naming the file …
lang_03

To add extra resource files for different languages/cultures simply copy/paste the existing resource file, and rename it following this syntax:

name.language-culture.resx

For example if the English resource file is our default file, and we want to add resource files for French and German we would have the following files: Resources.resx, Resources.de.resx and Resources.fr.resx.

We can use the localized strings in our views like this:

@Resources.Resources.Greeting

ASP.NET ignore resources files other than the default

To make the ASP.NET site to select the resource file to use depending on the user’s browser preferences we have to add this line inside the system.web tag in our web.config:

<globalization culture="auto" uiCulture="auto" />

Differences between Culture and uiCulture

The culture attribute refers to such things as format of numbers, dates, etc. Meanwhile the uiCulture attribute controls the resource language that is going to be loaded.

Therefore setting the uiCulture to auto results in the automatic selection of the resource file to use based on the user settings preferences.

Notice that we may be interested in keeping the uiCulture on auto, but set the culture to a fixed value. For example, our site may need to display the UI in different languages, while needing to interact (feeding/reading data to/from) with a third party system that expects all numeric data to be in a specific format.

Advertisements