Search code examples
springinternationalizationresourcebundle

Spring Internationalization Support


What are the different ways to resolve UI messages and provide internalization support in an web application using spring framework?

We use property files and ResourceBundleMessageSource to resolve messages in Spring. Spring's implementation causes an high cpu usage in our application. There are two problems with ResourceBundleMessageSource implementation.

  1. Locking contention - getResourceBundle() and getMessageFormat() are both synchronized.
  2. MissingResourceException - Resolving a message involves looping through all the resource bundles defined in the application and calling bundle.getString(key). bundle.getString(key) method call throws an MissingResourceException if the key is not found. Searching for the key until we find the message for the given key. Since exception construction is a slow process and could eat up the CPU (which is what I observed in our load tests) this looks like a bottleneck.

Though there are workarounds for both of the above problems (by extending the class and overridding the behavior) I wanted to know if there are other ways in spring framework to provide internationalization support to a web application.

Thanks in advance


Solution

  • You can use the ReloadableResourceBundleMessageSource instead. It provides some internal caching.

    If that does not work, I would advise implementing your own MessageSource (It's fairly straight forward). Spring provides AbstractMessageSource which may be helpful to start with.

    From there, you can implement some caching. More than likely, your localizations are not being updated frequently.

    You can read here on using the new Cacheable annotations in Spring 3.1

    spring 3.1 @Cacheable example

    I have done this already with success in applications that allow admins to override locales in the database. Your particular implementation however would obviously be very different.