« Matthew and Santa | Main | JetSpeed - WebLogic »

I18N in Struts/JSTL

This is a new category - Programming. It probably isn't useful to anyone else, but it is a good place for me to summarize my experiences (and it will help me remember how things work).

Today's challenge was how to create a user interface where the user could dynamically change the language, e.g. by choosing from a select list or clicking on a neat little flag. And of course, the changes should take effect immediately without having to restart the server.

General background/constraints:
* J2EE - WebLogic 8.1, JDK 142
* Struts 1.1 framework
* Tiles
* JSTL 1.0
(as you can see I am trying to keep current)

The general approach seems to be well summarized at http://www.anassina.com/struts/i18n/i18n.html. In particular the section on Dynamic Language Selection.

However, that approach did not work for me. As best I could determine it was related to inconsistencies between how JSTL and the browser and the container were determining the local. An interesting thread along these lines (but way above my head) can be found here.

So, I found a hacky way to do this - basically using the on each page in my applicaton and setting "foo" to the right value in the Struts action when the user picks their language. But even I realized that was a hack. So I kept searching.

And found the answer here. in particular on page 2. The key appears to be to set the locale in the JSTL Configuration so that the JSTL tags will use it - not just in the session. By using their code snippet I was able to remove all my lines and everything works as I would want.

In particular:
* When the user starts the app, everything appears in the preferred language of the browser (at least in IE).
* The user can then change the language through the application interface and everything appears in the new language.

Well, that is my first programming article. If anyone ever finds this useful, please let me know.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)