5,702,921 members and growing! (20,827 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Localization of static web files

By Anton Bar

Use HttpHandler for localization of static HTML, XML, Java Script, CSS files
Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 11 Nov 2006
Updated: 11 Nov 2006
Views: 11,004
Bookmarked: 14 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 3.15 Rating: 4.50 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
1 vote, 20.0%
4
3 votes, 60.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

LocalizationHandler

Introduction

This article shows a simple solution for localization of static files such as HTML, XML Java Script, or CSS, using the standard ASP.NET global resources. I was looking for a technique that will allow me using <%$ Resources:Resfile, KeyName %> syntax in all kinds of files. I also wanted all the resources to be concentrated into a single file in order to avoid duplicate entries and to simplify the translation process.

Required Knowledge 

I assume that the readers are familiar with:

  1. ASP.NET global resources
  2. ASP.NET caching
  3. .NET HTTP handlers
  4. Regular Expressions

How it works

The idea is simple - static files that need to be localized will be served by a special HTTP Handler. The handler will read the static files and replace all resource keys by their values as specified in your .resource file. The parsed files will be stored in ASP.NET cache and updated every time you change them.

Consider the following HTML code:

<html dir="<%$ Resources:Resource, direction %>">
<body>
<h1><%$ Resources:Resource, title %></h1>
</body>
</html> 

In this example, the HTTP handler will replace direction and title resources by their values from Resource.resources file.

At this point I suggest to download the enclosed sources and examine the code in LocalizationManager\LocalizationHandler.cs.

The code

All the important code is located into the ProcessRequest method, and the workflow is very simple:

  1. Check whether the requested file already exists in the cache.
  2. If not, read the file (HTML, JS, CSS, XML etc.)
  3. Create ResourceManager for the required resource file if it wasn't created yet
  4. Replace all resource keys by their values.
  5. Store the result in the cache depending on the input file
  6. Return the result.
public void ProcessRequest(HttpContext context)
{
	if (m_appPath == null)
		m_appPath = context.Server.MapPath(".");

	// Check whether the requested file was already parsed

	string filePath = context.Request.Url.LocalPath;
	string fileId = filePath + "." + Thread.CurrentThread.CurrentCulture.Name;
	string buffer = (string)context.Cache.Get(fileId);

	// Set the correct Content Type

	HttpResponse response = context.Response;
	response.ContentType = GetContentType(filePath);

	if (buffer == null)
	{
		// If not, read the required file into a string buffer

		string fullPath = context.Server.MapPath(filePath);
		buffer = ReadFile(fullPath);

		// Replace resource place holders by their values

		buffer = ParseBuffer(buffer, context);

		// Store the result in cache for future requests, the cache will 

		// expire when the processed Java Script file will be changed

		CacheDependency dependency = new CacheDependency(fullPath);
		context.Cache.Insert(fileId, buffer, dependency);
	}

	response.Write(buffer);
}

Configuration

You will need to configure your web application as following:

Step 1 - Configure IIS

In IIS console, open properties of your web application and click on the Configure button. Under Mappings, add a new one that maps your static files (e.g. ".html", ".js", ".css" etc.) to the ASP.NET ISAPI engine (usually c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll).  

Sample screenshot

Note: On Windows XP you will probably notice this very annoying bug - in order to enable the OK button, you will need to enter the file extension with preceding dot (like this: ".js") and click on the Executable edit box to expand the path (eliminating the ellipsis).

Step 2 - Configure HTTP Handler

In your web.config file add our HTTP handler:

<system.web>
  <httpHandlers>
    <add verb="*" path="*.js" type="LocalizationManager.LocalizationHandler, LocalizationManager"/>
    <add verb="*" path="*.htm" type="LocalizationManager.LocalizationHandler, LocalizationManager"/>
    <add verb="*" path="*.css" type="LocalizationManager.LocalizationHandler, LocalizationManager"/>
  </httpHandlers>
...

Step 3 - Configure Globalization

Add <globalization culture="auto" /> tag to your web.config file. This will ensure a correct value of Thread.CurrentThread.CurrentCulture in all requests, including requests to static files. Otherwise it always will be default (e.g. en-US).

Step 4 - Copy the Handler

Make sure that LocalizationHandler.dll is located under the Bin directory of your application.

That's it, now everything should work.

Further development

There are several improvements I planned to do in the near future including automatic invalidation of all cached files when the Resource file is changed. I will be more than happy to know if there are any other improvements that you would like to see in this tiny project.

 

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

About the Author

Anton Bar


Born in S.Petersburg, Russia. Live in Jerusalem, Israel since 1991. Married with 3 kids.

Received M.Sc. in Computer Science from the Hebrew University of Jerusalem.

Co-owner and CTO of C Systems (www.csystems.co.il).
Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Nov 2006
Editor:
Copyright 2006 by Anton Bar
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project