Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Creating multilingual websites - Part 2

, 25 Aug 2004
Creating multilingual websites - Part 2
LocalizedSamplePart2.zip
LocalizedSample
bin
Localization.dll
LocalizedSample.dll
language
en-CA
fr-CA
Localization
bin
Localization.csproj.user
Localization.resharperoptions
LocalizedSample.csproj.webinfo
LocalizedSample.resharperoptions
LocalizedSamplePart2VB.zip
LocalizedSampleVB
bin
Localization.dll
LocalizedSampleVB.dll
language
en-CA
fr-CA
Localization
bin
Localization.dll
Localization.vbproj.user
LocalizedSampleVB.resharperoptions
LocalizedSampleVB.vbproj.webinfo
using System;
using System.Globalization;
using System.Threading;
using System.Web;

namespace Localization {
   /// <summary>
   /// Summary description for LocalizationHttpModule.
   /// </summary>
   public class LocalizationHttpModule : IHttpModule {
      public void Init(HttpApplication context) {
         context.BeginRequest += new EventHandler(context_BeginRequest);
      }
      public void Dispose() {}
      private void context_BeginRequest(object sender, EventArgs e) {
         HttpRequest request = ((HttpApplication) sender).Request;
         HttpContext context = ((HttpApplication)sender).Context;
         string applicationPath = request.ApplicationPath;
         if(applicationPath == "/"){
            applicationPath = string.Empty;
         }
         string requestPath = request.Url.AbsolutePath.Substring(applicationPath.Length);
         LoadCulture(ref requestPath);        
         context.RewritePath(applicationPath + requestPath);
      }
      private void LoadCulture(ref string path) {
         string[] pathParts = path.Trim('/').Split('/');
         string defaultCulture = LocalizationConfiguration.GetConfig().DefaultCultureName;
         if(pathParts.Length > 0 && pathParts[0].Length > 0) {
            try {
               Thread.CurrentThread.CurrentCulture = new CultureInfo(pathParts[0]);
               path = path.Remove(0, pathParts[0].Length + 1);
            }catch (Exception ex) {
               if(!(ex is ArgumentNullException) && !(ex is ArgumentException)) {
                  throw;
               }               
               Thread.CurrentThread.CurrentCulture = new CultureInfo(defaultCulture);
            }
         }else {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(defaultCulture);
         }
         Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Karl Seguin

Canada Canada
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 26 Aug 2004
Article Copyright 2004 by Karl Seguin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid