This example illustrates how to setup a project to use the internalization (I18N) classes in Pineapple, i.e. resolve message from a resource bundle defined in property file.
To setup a project with I18N support follow these steps:
The I18N classes are defined in the pineapple-api project, so add a Maven dependency to the project POM:
<dependency> <groupId>com.alpha.pineapple</groupId> <artifactId>pineapple-api</artifactId> </dependency>
Create a text file in the Maven project which contains the messages for the project.
If the project contain a Spring configuration file for definition of the beans used by the project then add an additional bean definition to the configuration file which defines the message provider:
Example:
<!-- define message provider for internationalization --> <bean id="messageProvider" class="com.alpha.pineapple.i18n.PropertyFileMessageProviderImpl"> <property name="basename" value="com.alpha.pineapple.report.basichtml-messages"/> </bean>
The id of the bean should be messageProvider.
Pineapple only support a single implementation of the MessageProvider interface which is named com.alpha.pineapple.i18n.PropertyFileMessageProviderImpl.
The PropertyFileMessageProviderImpl class supports usage of dots in the name of message file. This enables usage of the naming scheme which uses the package name for the message file: ${package-name}-messages.properties.
To get access to the message provider bean from a class in the project, declare a field in a class and let Spring inject the message provider when the class is initialized.
Example:
/** * Message provider I18N support. */ @Resource MessageProvider messageProvider;
To use the message provider, first define a key-value pair in the message file:
Example: com.alpha.pineapple.report.basichtml-messages.properties contains:
good_message=This a good message from [{0}] and [{1}].
And resolve the message in the class:
// log debug message if ( logger.isDebugEnabled() ) { Object[] args = { "Titus", "Ed Chianese" }; String message = messageProvider.getMessage("good_message", args ); logger.debug(message); }