Search code examples
jakarta-eespring-mvclocalizationjava-7resourcebundle

What is a good way of introducing Resource bundle (to support different languages) on an existing system?


We have a legacy application system which has been built 15 years ago and we still use it. Our system only supports English. However, right now we are working on a new project which should use some part of the existing system but needs to have a feature to display other languages also.

For this project web project we are using Spring MVC, Java 7, and MySql. We have never used it but, heard of resource bundle. Should we define each thing to be displayed in a property file (1 file per language)and use this property files at run time based on the language. We have a lot of Enums which needs to be displayed on the user interface.

What do you recommend us to use to support a language other than English? It can be Resource bundle or any other methods.

I appreciate your help.

UPDATE We are going to support only 2 languages including English for now.


Solution

  • Existing System, meet Resource Bundle. Resource Bundle, meet Existing System...

    All jokes aside, the solution would be dependent on your application. Is it a web-based JSP app, Swing client, or something else?

    For simple non-web apps, here is a simple example using Spring's ResourceBundleMessageSource. Its a rather old example, but the basic idea is that you setup some MessageSource, then use the MessageSource to get the messages.

    If this is a web app using JSPs, then you would change all your JSPs to use the Spring JSTL tag spring:message (or the non-spring fmt:message, but I think this limits your options) where ever you need to use an internationalized string. Again, you need some MessageSource setup, and Spring will auto-magically find your MessageSource and use it. It still follows the same pattern: define the MessageSource, ask the MessageSource for a message...it's just using a JSTL tag to do the heavy lifting.

    There are several MessageSource options available. The easiest way is using .properties files as the example above does. I find this harder to maintain, so in our application we swapped out the MessageSource with our own implementation we call JdbcMessageSource, that extends AbstractMessageSource. Basically, it uses a table to read the messsages and provides caching of those messages.