MessageSource in Spring 3

The MessageSource interface supports the parameterization and internationalization of messages and provides 2 implementations:

  1. ResourceBundleMessageSource, built on top of the standard ResourceBundle
  2. ReloadableResourceBundleMessageSource, being able to reload message definitions without restarting the VM

The following is an example of ResourceBundleMessageSource to get messages in the specified language in Spring 3.

  1. create a java project in your preferred IDE (eclipse, netbeans, etc.)
  2. configure the project with Maven with file pom.xml

    otherwise you can use the following library:

    • spring-context-3.1.2.RELEASE.jar
    • spring-aop-3.1.2.RELEASE.jar
    • aopalliance-1.0.jar
    • spring-beans-3.1.2.RELEASE.jar
    • spring-core-3.1.2.RELEASE.jar
    • commons-logging-1.1.1.jar
    • spring-expression-3.1.2.RELEASE.jar
    • spring-asm-3.1.2.RELEASE.jar
    • log4j-1.2.17.jar
  3. create a folder “resources” and add to the classpath
  4. create the file resources/app-context.xml

    as explained below, the string “messages” is equal to the first part of the name of the files .properties, and you can specify a path from the classpath such as “locale/messages” for example
  5. create the file resources/log4j.xml
  6. create the 3 file .properties to manage the content of messages in english language (Great Britain), english language (United States) and italian language
    • resources/messages_en_GB.properties
    • resources/messages_en_US.properties
    • resources/messages_it.properties

      the names of the files .properties are important and must comply with one of the following syntaxes:

      • [base name]_[language]
      • [base name]_[language]_[country]
      • [base name]_[language]_[country]_[variant]

      where

      • [base name] is that specified in resources/app-context.xml (“messages” in this example)
      • [language] is the language code (“en”, “it”, etc.)
      • [country] is the country code (“GB”,”US”, “IT”, the same language changes depending on the country, the english spoken in the United States is different from that spoken in England)
      • [variant] is a code for the variant of the language (not used in this example)
  7. create the eu/lucazanini/messagesource/Main.java

    I use the method String getMessage(String code, Object[] args, Locale locale) where

    • code to look up in the files .properties
    • args is an object array to pass as arguments and they replace {0} {1} {2} … in the files .properties, use “null” if you don’t have arguments
    • Locale is an object of Locale class and has 3 constructors:
      • Locale(String language)
      • Locale(String language, String country)
      • Locale(String language, String country, String variant)

    you can also use the method String getMessage(String code, Object[] args, String defaultMessage, Locale locale) where you can specify a default value for the message

  8. launch the application

References:
MessageSource
Locale

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.