views:

34

answers:

2

Hello,

I am trying to implement internationalization in Tomcat. There are going to be different resource text files. My idea is to load all the resources in to the memory while tomcat loads. Below is the sample code to load multiple resource in to the memory.

public class ResourceBundleLoader {


    private static ResourceBundle enResourceBundle;
    private static ResourceBundle frResourceBundle;

    public static void loadBundle(){
        Locale enLocale = new Locale("en", "US");
        enResourceBundle = ResourceBundle.getBundle("MessagesBundle",enLocale);
        enLocale = new Locale("fr", "FR");
        frResourceBundle = ResourceBundle.getBundle("MessagesBundle",enLocale);
    }

    public static ResourceBundle getEnResourceBundle(){
        return enResourceBundle;
    }

    public static ResourceBundle getFrResourceBundle(){
        return frResourceBundle;
    }
}

The method loadBundle is called once thru startup servlet. And getEnResourceBundle() and getFrResourceBundle() is called accordingly. Is this right way to implement/maintain internationalization in tomcat? or is there any better way?

Thanks in advance.

+4  A: 

You dont need to make this helper class, as per the java documentation the bundles are already cached for you in memory. This will just make your code more complicated to maintain. ie You would have to alter your code every time you add a new "bundle".

Just add code like this to your servlets and/or JSP's:

//request.getLocale() returns the web browsers locale
bundle = ResourceBundle.getBundle("MessagesBundle",request.getLocale())

Just make sure you have a default message bundle file with all your text. Then you can just add extra locales at will as things get translated.

UTF-8 support

I also strongly suggest you create a servlet filter that applies to all requests that ensures that UTF-8 is turned on for both the html that is output, and the parsing of the form responses that are posted back to your application:

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
corydoras
i agree. refer to the Cache Management section from http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html
Pangea
I can initializa local in this way right?request.setLocale(new Locale("fr"));
Soft
How to i set different Locale?
Soft
The purpose of request.getLocale() is to read the locale of the users web browser, so you can display the web page using the locale of the end user. If you wont want to respect the locale set in the users web browser, you simply dont use request.getLocale(). See http://download.oracle.com/javase/tutorial/i18n/locale/create.html
corydoras
A: 

I wouldn't optimize until I knew the i18n was too slow.

But if I proceeded down your path, instead of using scalar ResourceBundles, I'd put the ResouceBundles into a Map. Now your code can use any bundle knowing the locale - which you have to select the appropriate ResourceBundle anyway.

Your code won't have any if locale is this, use English. Instead, it will be myResourceBundle = bundleMap.get(myLocale);

Tony Ennis
All this "optimisaiton" will do is slow things down and make the code hard to maintain. If you do it like this then adding locales will require code changes.
corydoras
I wouldn't do it this way, but I could do it so it wouldn't require code changes. That being said, I'd probably only be mimicking the code that already exists. I'm offering an alternative to the idea of scalar ResourceBundles, really.
Tony Ennis
But yes +1 for recommending that someone should not try to optimise things they have not first tested and found is slow. Too many programmers spend time trying to fix their code to make it run faster, when they dont even know which bits of the code will slow down the application!
corydoras
How to i set different locale?
Soft
http://download.oracle.com/javase/tutorial/i18n/index.html
Tony Ennis