Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

i18n - synching up Struts and JSTL

As he did with Roller, Jaap van der Molen made some enhancements to AppFuse to fully support internationalization. He also translated all the existing keys to Dutch. What a guy, eh?!

After I installed this enhanced version of AppFuse, I noticed a few quirks. Namely that my default language was Dutch. To me, this meant that my browser's local must be Dutch. However, since it wasn't, I knew there had to be an issue. After a bit of Googling, I found my answer. It turns out that if you don't specify the language on your default properties file, JSTL won't pick it up - and it defaults to the first one with a language specified. This means that you must have ApplicationResources_en.properties instead of ApplicationResources.properties. Also, another quirk is that you need to synch up Struts locale-setting and JSTL's locale-setting. To do this, I added the following method to my ActionFilter.java class:

    // keep JSTL and Struts Locale's in synch
    Locale locale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
    if (locale == null) {
        locale = request.getLocale();
    }
    if (request.getParameter("locale") != null) {
        locale = new Locale(request.getParameter("locale"));
    }
    session.setAttribute(Globals.LOCALE_KEY, locale);
    Config.set(session, Config.FMT_LOCALE, locale);

Even with all these "hacks" - Mozilla and Safari handle this stuff differently on the Mac. Jaap says that everything works fine on Windows w/o these hacks, so maybe it's just an OS X thing. Regardless, to make things easier, I've added the ability for users to switch between languages by clicking on their language of choice.

There's nothing like adding a new feature to something you're about to demo! I'm on in 45 minutes - the butterflies are fluttering like mad...

Posted in Java at Apr 15 2004, 12:42:00 PM MDT 9 Comments
Comments:

That's just wrong... That's not how the PropertiesResourceBundle does it... Jeez... silly JSTL.

Posted by Jason Carreira on April 15, 2004 at 06:46 PM MDT #

I think you are seeing the effect of the ResourceBundle using the JVM's default locale before the base name. Let's assume your default bundle file, i.e., the one without a suffix, is English. In addition you have the Dutch translation in a file with the appropriate suffix. Run your JVM with Dutch as the default.

Now when you request a ResourceBundle for English ( _en suffix) the ResourceBundle code doesn't find a match and so next tries the JVM default, Dutch. There is a match for Dutch so that's what you get.

To get around this, add an empty file with the _en suffix. Now when the ResourceBundle searches for _en it gets a match. However, for each property that is not in the _en file, i.e., all of them, it falls through to the base file and you get English.

To summarize, if your JVM will ever run with the default locale not the same as the base file, and there is a translated file for that default, add an empty file with the suffix of the language of the base file. Note that for a ListResourceBundle the file is java and isn't actually empty but contains some minimal information described in the javadoc for ResourceBundle.

Posted by Bob Hanner on April 30, 2004 at 06:35 PM MDT #

It actually only happens on the first page of my app. On that page, before you log in, the text is from the first file with an extension (if I don't have an _en file). After logging in, everything works fine. It's just plain wierd...

Posted by Matt Raible on April 30, 2004 at 07:00 PM MDT #

This certainly is an annoying little thing. I am trying to put together an English/Danish site and I have a default (english), _en (english) and _da_DK (danish). If I enter the site with my browser set to Danish/English, I get the Danish text. If I enter with English/Danish I get the English text. But, if I enter with Bulgarian/English/Danish, I still get the Danish text. I would have expected the text from the default file, which is in English. So, I add a Bulgarian file, leave it empty, and tadaaa - I now get the text from the default file, which is in English. I guess that results the conclusion that I need to add an empty file for each (known) language in the universe. Bummer... :-)

Posted by Bjørn Stenfeldt on May 07, 2004 at 06:03 PM MDT #

I started to convert my struts tags named 'bean:message' to the JSTL 'fmt:message' but when I switchd locales my application no longer works like it used to with the 'bean:message' tags from struts. Is it worth converting over? Does the JSTL 'fmt:message' tag anything more then breaking a correctly working app. Joe

Posted by 216.189.172.8 on December 05, 2004 at 11:57 PM MST #

I was hoping to glean some information from your "I found my answer" link, but it appears to go somewhere very different. Something to do with hardware optimization. Does the original references still exist? (*Chris*)

Posted by Chris Pratt on September 13, 2006 at 08:18 PM MDT #

n

Posted by 220.225.70.2 on November 01, 2007 at 05:56 AM MDT #

still JSTL doesn't pick up new locale without setting Config... great tip, thanks!

Posted by mars on July 20, 2009 at 08:23 AM MDT #

What simple set the default locale?

Locale.setDefault(Locale.ENGLISH);

I work with Jetty8 + Struts2. I start the Jetty8 as an embedded web server in my program and set the locale before I start Jetty. It works perfectly for me.

Posted by Gerrit on September 22, 2011 at 10:30 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed