Using messages
Loom is designed with i18n strongly in mind, which means that all messages must be stored in properties files.
To display messages, use a l:messages tag:
Messages will be serialized on a redirect, in which case they will be displayed on the next page request.
The messages files
Loom messages are stored in a MessagesRepository instance specific to each Locale and configured by MessagesRepositoryFactory. MessagesRepository is very similar to the ResourceBundle class included in the JDK.
If a ResourcesWatchdog class has been configured, any modification to these properties files will trigger a reload. Only changes to files that are not in the classpath will be detected, though.
By default MessagesRepository will use classpath:resources/loom-messages.properties (provided by loom.jar) and /WEB-INF/classes/resources/messages.properties. The latest is where the application is expected to store its own messages.
Note that if a specific Locale is not found the system will fallback to MessagesRepositoryFactory.defaultLocale (set by default to the OS locale).
Using parameters
Messages may include parameters:
When setting parameter values you must indicate if they should be translated or not:
MessagesRepository.guessString()
The guessString() method is invoked to translate messages: when asked for "customer.manager.name" it will first search the whole string, then "manager.name", then "name". If none is found it will return "[missing: customer.manager.name]" and mark it as not found to skip further searches.
Guess results are calculated just once to improve performance.
i18n and javascript
The browser will get a JSON copy of the messsages bundle for the current user locale. This copy will include:
- Any key that starts with "loom."
- Any key contained in MessagesRepositoryFactory.browserMessages.
- Any key contained in the @BrowserMessages annotation, used in any Action class
Thus, these two examples are equivalent:
spring.xml:
MyAction.java
Both will generate a JSON object that can be used at the browser with:
Using i18n from your JSP files
There are several ways to print i18n contents inside your JSP pages: