5.8.2. Getting Localized Messages
This section covers ways of getting localized messages in different application components.
-
In screen XML-descriptors, component attributes for displaying static text (such as caption) can address localized messages using the rules of MessageTools.loadString() method. For example:
-
caption="msg://roleName"
– gets a message defined by theroleName
key in the message pack of the current screen. Screen message pack is defined by themessagesPack
attribute of the rootwindow
element. -
caption="msg://com.company.sample.entity/Role.name"
– gets a message defined by theRole.name
key in thecom.company.sample.entity
message pack.
-
-
In screen controllers, localized strings can be retrieved in the following ways:
-
From the current screen message pack:
-
Using
getMessage()
method inherited from the AbstractFrame base class. For example:String msg = getMessage("warningMessage");
-
Using
formatMessage()
method inherited from theAbstractFrame
base class. In this case, the extracted message is used to format submitted parameters according to the rules ofString.format()
method. For example:messages.properties:
warningMessage = Invalid email address: '%s'
Java controller:
String msg = formatMessage("warningMessage", email);
-
-
From an arbitrary messages pack via an injection of Messages infrastructure interface. For example:
@Inject private Messages messages; @Override public void init(Map<String, Object> params) { String msg = messages.getMessage(getClass(), "warningMessage"); ... }
-
-
For components managed by a Spring container (managed beans, services, JMX-beans, Spring MVC controllers of the portal module), localized messages can be retrieved with the help of the Messages infrastructure interface injection:
@Inject protected Messages messages; ... String msg = messages.getMessage(getClass(), "warningMessage");
-
In application code where injection is not possible, the
Messages
interface can be obtained using the staticget()
method of theAppBeans
class:protected Messages messages = AppBeans.get(Messages.class); ... String msg = messages.getMessage(getClass(), "warningMessage");