JBoss.orgCommunity Documentation

Chapter 56. Internationalization and Localization

With the help of the JBoss Logging project, all log and exception messages in RESTEasy are internationalized. That is, they have a default value in English which can be overridden in any given locale by a file which gives translated values. For more information about internationalization and localization in Java, see, for example, http://docs.oracle.com/javase/tutorial/i18n. For more about JBoss Logging Tooling, see https://jboss-logging.github.io/jboss-logging-tools/, Chapters 4 and 5.

56.1. Internationalization

Each module in RESTEasy that produces any text in the form of logging messages or exception messages has an interface named org.jboss.resteasy...i18n.Messages which contains the default messages. Those modules which do any logging also have an interface named org.jboss.resteasy...i18n.LogMessages which gives access to an underlying logger. With the exception of the resteasy-core-spi module, all messages are in the Messages class. resteasy-core-spi has exception messages in the Messages class and log messages in the LogMessages class.

Each message is prefixed by the project code "RESTEASY" followed by an ID which is unique to RESTEasy. These IDs belong to the following ranges:

Table 56.1. 
RangeModule
2000-2999resteasy-core-spi log messages
3000-4499resteasy-core-spi exception messages
4500-4999resteasy-client
5000-5499providers/resteasy-atom
5500-5999providers/fastinfoset
6000-6499providers/resteasy-html
6500-6999providers/jaxb
7500-7999providers/multipart
8000-8499providers/resteasy-hibernatevalidator-provider
8500-8999providers/resteasy-validator-provider
9500-9999async-http-servlet-3.0
10000-10499cache-core
10500-10999resteasy-cdi
11500-11999resteasy-jsapi
12000-12499resteasy-links
12500-12999resteasy-servlet-initializer
13000-13499resteasy-spring
13500-13999security/resteasy-crypto
14000-14499security/jose-jwt
14500-14999security/keystone/keystone-as7
15000-15499security/keystone/keystone-core
15500-15999security/resteasy-oauth
16000-16499security/skeleton-key-idm/skeleton-key-as7
16500-16999security/skeleton-key-idm/skeleton-key-core
17000-17499security/skeleton-key-idm/skeleton-key-idp
17500-17999server-adapters/resteasy-jdk-http
18500-18999server-adapters/resteasy-netty4

For example, the Jakarta XML Binding provider contains the interface

org.jboss.resteasy.plugins.providers.jaxb.i18.Messages

which looks like


@MessageBundle(projectCode = "RESTEASY")
public interface Messages
{
   Messages MESSAGES = org.jboss.logging.Messages.getBundle(Messages.class);
   int BASE = 6500;

   @Message(id = BASE + 00, value = "Collection wrapping failed, expected root element name of {0} got {1}", format=Format.MESSAGE_FORMAT)
   String collectionWrappingFailedLocalPart(String element, String localPart);

   @Message(id = BASE + 05, value = "Collection wrapping failed, expect namespace of {0} got {1}", format=Format.MESSAGE_FORMAT)
   String collectionWrappingFailedNamespace(String namespace, String uri);
   ...
   

The value of a message is retrieved by referencing a method and passing the appropriate parameters. For example,


throw new JAXBUnmarshalException(Messages.MESSAGES.collectionWrappingFailedLocalPart(wrapped.element(), ele.getName().getLocalPart()));
   

56.2. Localization

When RESTEasy is built with the "i18n" profile, a template properties file containing the default messages is created in a subdirectory of target/generated-translation-files. In the Jakarta XML Binding provider, for example, the

Messages.i18n_locale_COUNTRY_VARIANT.properties

goes in the

org/jboss/resteasy/plugins/providers/jaxb/i18n

directory, and the first few lines are


# Id: 6500
# Message: Collection wrapping failed, expected root element name of {0} got {1}
# @param 1: element -
# @param 2: localPart -
collectionWrappingFailedLocalPart=Collection wrapping failed, expected root element name of {0} got {1}
# Id: 6505
# Message: Collection wrapping failed, expect namespace of {0} got {1}
# @param 1: namespace -
# @param 2: uri -
collectionWrappingFailedNamespace=Collection wrapping failed, expect namespace of {0} got {1}
   

To provide the translation of the messages for a particular locale, the file should be renamed, replacing "locale", "COUNTRY", and "VARIANT" as appropriate (possibly omitting the latter two), and copied to the src/main/resources directory. In the Jakarta XML Binding provider, it would go in

src/main/resources/org/jboss/resteasy/plugins/providers/jaxb/i18n

For testing purposes, each module containing a Messages interface has two sample properties files, for the locale "en" and the imaginary locale "xx", in the src/test/resources directory. They are copied to src/main/resources when the module is built and deleted when it is cleaned.

The Messages.i18n_xx.properties file in the Jakarta XML Binding provider, for example, looks like


# Id: 6500
# Message: Collection wrapping failed, expected root element name of {0} got {1}
# @param 1: element -
# @param 2: localPart -
collectionWrappingFailedLocalPart=Collection wrapping failed, expected root element name of {0} got {1}
# Id: 6505
# Message: Collection wrapping failed, expect namespace of {0} got {1}
# @param 1: namespace -
# @param 2: uri -
collectionWrappingFailedNamespace=aaa {0} bbb {1} ccc
...
   

Note that the value of collectionWrappingFailedNamespace is modified.