<February 2023>

On this page...



Member of...

ASP Insiders

MVP Visual Developer ASP/ASP.NET

Enter CodeZone

Blog Categories



Deutsche Resourcen


Sign In

#  Friday, July 29, 2005

One thing I didn't get around to demo at last week's CBC05 is the usage of profiles to remember and subsequently set the culture on ASP.NET pages. That's why I am documenting it now.

To get started, we need to have a profile that stores the user's preferred culture. So your profile in web.config looks could like this:

    <profile enabled="true" defaultProvider="myProfile">
        <add name="myProfile"
        <add name="Culture" type="String" defaultValue="en-US" />
        <add name="Theme" type="String" />

The Culture profile setting is changed via a simple (hard-coded) dropdown list in default.aspx, which also displays a greeting pulled from a localized global resource, the culture code for verification purposes, as well as the current time formatted according to the respective locale:

The code for the button in default.aspx.cs is rather basic as you would have expected:

protected void Button1_Click(object sender, EventArgs e)
   Profile.Culture = DropDownList1.SelectedValue;

   // really nasty redirect to go back to this back and see the effect
   Response.Redirect(Request.Path + "?" + Server.UrlEncode(DateTime.Now.ToLongTimeString()));

The real magic for setting the culture so that formatting is done correctly, as well as the right resources being displayed, is done in a custom HttpModule, which needs to be registered in web.config:

      <add name="myProfileModule" type="ICSharpCode.Web.ApplyProfileToPageModule" />

This guy lives in App_Code\ApplyProfileToPageModule.cs, and looks like this:

using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Globalization;

namespace ICSharpCode.Web
 public class ApplyProfileToPageModule : IHttpModule
  public void Dispose()
   // currently, does nothing

  public void Init(HttpApplication context)
    context.PreRequestHandlerExecute += AppPreRequestHandlerExecute;

  private void AppPreRequestHandlerExecute(object sender, EventArgs e)
   Page p = HttpContext.Current.Handler as Page;

   if (null != p)
      ProfileCommon pc = (ProfileCommon)HttpContext.Current.Profile;

      string cultureName = pc.Culture;

      CultureInfo culture = new CultureInfo(cultureName);

      Thread.CurrentThread.CurrentUICulture = culture;
      Thread.CurrentThread.CurrentCulture = culture;

      // You can set the Theme exactly the same way, shown below:
      // p.Theme = pc.Theme;

In PreRequestHandlerExecute, I can access the profile, and go ahead and read the culture that is defined. Then all you need to do is set the CurrentUICulture as well as CurrentCulture of the CurrentThread. Done.

By the way, this event is also your ticket to set the Theme for a Page - if you have defined the user's preferred theme in her profile. Neat way of doing this.

LocalizationModule.zip (6.09 KB)

Categories: 2 Ohhhh | ASP.NET
Friday, July 29, 2005 11:00:03 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [2]


Friday, July 29, 2005 1:17:43 PM (W. Europe Daylight Time, UTC+02:00)
i think this is the easier way
not exactly the same
Friday, July 29, 2005 1:22:39 PM (W. Europe Daylight Time, UTC+02:00)
I prefer this approach because that way I do not need to tamper with the Page class - this module will work with *any* Page-derived page class. So it is more universal.
Comments are closed.

© Copyright 2023 Christoph Wille

newtelligence dasBlog 2.3.9074.18820
Subscribe to this weblog's RSS feed with SharpReader, Radio Userland, NewsGator or any other aggregator listening on port 5335 by clicking this button.   RSS 2.0|Atom 1.0  Send mail to the author(s)

Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.