Internationalization and JavaScript

Lately the amount of JavaScript in web applications has grown a lot. JavaScript has always been there but with the need of AJAX and more and more dynamical features modern web developers can not avoid it.

Luckily there are a lot of good concepts arising and libraries available (e.g. prototype, yui, …) that show how to control the JavaScript monster. Something that always bothered me was that I was not able to find a good editor for JavaScript. Since about six months I am using JSEclipse from Adobe which is a free Eclipse plugin that provides a lot support for writing JavaScript (code-completion, syntac check, …).

The JSEclipse editor allows me to edit standalone JS files and script code included in JSPs but many times I ran into the problem that I needed to mix JavaScript and Java code. For example to define an URL for an AJAX request or to get a message from a resource bundle that has to be displayed in a JavaScript dialog. The JSEclipse editor does not understand this “mixed” code parts. Additionally it is not possible to externalize the JavaScript code into external files and the code looks ugly.

Yesterday I found this article about how to do I18N in JavaScript. I like the approach with the externalized constants very much because

  • it allows me to create a central include containing all the information from Java needed for the JavaScript code -> only one place to change
  • the main JavaScript code is “JavaScript only” -> editable by the JSEclipse editor
  • the JavaScript only code may be externalized into separate files
  • the appearance of the constants instantly makes their purpose clear and the JS code looks clean