
Localization
Localization is often required on the web. For that reason, .NET framework includes localization support through the System.Globalization namespace.
However, while there is support, you need some extra work to use it. One of the most repeated tasks is actually making localization available to your web pages and making the system fit to user preferences.
The goal of this article is to provide one of the solutions for a clean and easy implementation of the localization system using the regular Culture settings provided by ASP.NET.
To do it, we will extend the basic System.Web.UI.Page class by overriding the IntilializeCulture method. This way, our web pages will be integrated with a persistent localization system that will, except for keeping track of user language selection while browsing the page, also keep track of user preferences for future visits.
This way, our only job on localization will be to provide users with a language selection menu.
Using the code
We will start by creating a new class library to create our own Page class. After creating the Page.cs file, we start creating our new, extended, and localized Page class.
First, we will need to include some additional namespaces and assembly references. The project is built using the 2008 version of Visual Studio and C# 3.0 (and .NET 3.5).
using System;
using System.Configuration;
using System.Web;
using System.Globalization;
using System.Threading;
Do not forget to reference System.Web and System.configuration.
Now, we override the InitializeCulture method:
public class Page : System.Web.UI.Page
{
protected override void InitializeCulture()
{
base.InitializeCulture();
CultureInfo selectedCulture = new CultureInfo(ConfigurationManager.
AppSettings["Localization_DefaultLanguage"]);
HttpCookie cookie = Request.Cookies.Get("lang");
DateTime cookieExpiration = DateTime.Now.AddDays(
Convert.ToInt32(
ConfigurationManager.
AppSettings["Localization_LanguageCookieExpirationInDays"]));
if (Request.QueryString["lang"] != null)
{
selectedCulture = new CultureInfo(Request.QueryString["lang"]);
cookie =
new HttpCookie("lang", selectedCulture.Name) { Expires = cookieExpiration };
Response.Cookies.Add(cookie);
}
else if (cookie != null)
{
selectedCulture = new CultureInfo(cookie.Value);
}
else
{
cookie =
new HttpCookie("lang", selectedCulture.Name) { Expires = cookieExpiration };
Response.Cookies.Add(cookie);
}
Thread.CurrentThread.CurrentUICulture = selectedCulture;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedCulture.Name);
}
}
That would be all that is required to implement internal localization support for the page. Now, we should actually use it inside a website.
Using the custom Page class to localize a website
Just create a default website and add the previously created class library as a reference. In the Default.aspx page, we will add a small Label for demonstration purposes. It will contain a meaningless text that will be translated.
To provide localization, we will use the built-in resource file based localization, thus we will create Default.aspx.resx files for all the languages we need.


Now, we will programmatically create a language selection menu using the previously prepared settings in the Web.config file:
<appSettings>
<add key="Localization_AvailableLanguages" value="en-gb,en-us,hr,it,fr,de,es,ru"/>
<add key="Localization_DefaultLanguage" value="fr"/>
<add key="Localization_LanguageCookieExpirationInDays" value="5"/>
</appSettings>
The menu will be created using a simple inline foreach loop:
foreach (string language in
ConfigurationManager.AppSettings["Localization_AvailableLanguages"].Split(','))
{
Response.Write(
string.Format(
"<li><a href='{0}?lang={1}'>{2}</a></li>",
Request.Path, language, new CultureInfo(language).NativeName));
}
And this would be pretty much all. Do not forget to change the Page class used by the default web form class:
public partial class _Default : WikiArticle_LocalizationLib.Page
Press F5 (build and run) and enjoy the show. If you did everything correctly, you should be seeing the page like this:

Tips
The point of this code is to give direct control over the settings for each specific user and also to enable Web Forms (pages) to automatically "understand" the URI language setup and combine it with the saved info.
This enables the pages for further processing like a storing system for more per-user settings and also for URL rewriting of the language settings control. One of the other features is also a system that specifically can enable or disable some of the already present translated resources and which can also be used for menu system generation in a very simple way.
Using this system, you are now able to easily extend supported languages and have user language preferences always stored in cookies.
This system can easily be extended according to your needs like many things here, like exception handling and settings storage. Some ideas have been removed here for the sake of simplicity.