Visit the Java Pro News Directory
Blogs
Blogosphere, Web 2.0...
Education
Help, Learn...
Forums
Search, Learn...
Newsletters
eBusiness, Tech...
News
Tech, Sales...
Software
Animation, Technique...
Tools and Technologies
Extra's,plugins...
Web Site Design
Monitoring, Hosting...


Recent Articles

SDK Preview for JavaFX
After the advent of SDKs for Adobe AIR & Microsoft Silverlight and the considerable amount of market share they already have, Sun Microsystems has finally come up with their SDK preview for JavaFX. This was a result...

Use JavaScript and Google Analytics to Optimize...
Forms are pretty much a necessity on websites therefore it becomes important to track how your visitors interact with the forms on your website, understand which fields are most often left empty, and determine how...

JavaScript Custom LightBox
A few people have asked me how to implement the div popup with a LightBox effect, like the one used when you click the Filter by APML link in the top right corner...

Morph AppSpace Adding Support for Java
I just read about Morph, another one of Winston Damarillo's startups. Winston's past startup successes include Gluecode Software, acquired by IBM...

New Atlanta's New ColdFusion to .NET/Java Migration...
Vince Bonfanti just announced New Atlanta's "new ColdFusion-to-.NET and ColdFusion-to-Java migration services." and goes on to say "As experts in ColdFusion, ASP.NET, and Java technologies, New Atlanta is...

Rich Internet Application Misconceptions
Some-a people out there in our nation don't have that, and I believe RIA, like, such as in Flash and Silverlight everywhere, should help the US, or should help...

Enterprise Class MVC with ColdFusion and Java
*Enterprise Class MVC with ColdFusion and Java is one of the sessions at the cf.Objective() 2008 Conference. We hear a lot of talk about using individual Java objects within ColdFusion but the reality of enterprise...



09.02.08

Using JavaScript to Localize Text

By Mads Kristensen

Just about every web project I've been involved in have, at one time or the other, needed to present some text to the visitor through JavaScript. It could be in an alert box or some other way and the problem has always been to localize that text into different languages using resource files or satellite assemblies.

There are many ways of localizing the keys, but most of them involve writing out variables on a page with the localized text and then let the .js include files read from those variables. That's not a good solution. It would be much better if the .js files could be rendered with the localized text directly.

At work, I've written exactly such a mechanism and here is a cleaned up, plug 'n play version you can use in your own web project. It's an HttpHandler that intercepts the requests to the .js files and performs the localization based on regular expressions. Here's how it works.

Resource files

It doesn't matter whether you use .resx files or satellite assemblies for localizing your text, because both methods work with the System.Resources.ResourceManager class.

A resource file contains keys and a values. The key is always the same, but the value varies for each language. Each language is represented in its own .resx file and the name of the file decides which language it contains like below.



If no language is added in the file name, it automatically becomes the default localizations – normally English. The three .resx files each contain one line with the key nameOfPage. The value in each of the files is localized text that can be referenced by the key.

text.resx: Name of page
text.da.resx: Sidens navn
text.es.resx: Namo de la pago (Sorry, my Spanish is slightly rusty)

The script method

In the .js script files you need a way to specify a certain text as localizable. I've chosen to pick a syntax that looks like this:



Notice that the Translate method above takes the localizable key as parameter. This is the syntax the regular expression in the HttpHandler is looking for.


The HttpHandler

The HttpHandler does a couple of things. It reads the content of the script file, localizes the text, caches the response and compresses it using HTTP compression. It does all this in about 150 lines of easy readable code.



This will trigger the correct .resx file to be used based on the visiting browser's language. If your .resx files aren't called text.resx or you use satellite assemblies, then you need to update the instantiation of the ResourceManager in the TranslateScript method in the handler.

Download code and sample

Download the code below and place the ScriptTranslator.cs in your App_Code folder. Then update your web.config with the web.config values found in the zip file. If you unzip the zip file, the entire contents can be opened directly in Visual Studio and you will be able to try it out easily.

Localization.zip (8,37 kb)

Comments


About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.
http://www.madskristensen.dk/

About JavaProNews
JavaProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Java environments. News and Advice for Java Professionals





JavaProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagmentNews.com





-- JavaProNews is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2008 iEntry, Inc. All Rights Reserved Privacy Policy Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article