The messages infrastructure is designed to support i18n. Every message has a key, severity and text. A message may optionally include parameters. The key is looked up in resource bundles which may be localized (l10n). A general (default) resource bundle that includes, for example, English messages may also be used.
To define a message you must:
If you want the infrastructure to automatically load all your messages, then the messages.xml must be placed in the MEAT-INF/spring directory. It is possible, like any other bean, to define the messages in an xml file with another name, see Writing an Application for further details on the application startup sequence.
Each message has a severity. A severity may be one of: FATAL, ERROR, WARN or INFO. The severity of the message is defined in its bean definition, and cannot change at runtime.
Example of defining a message as a Spring bean:
<bean id="testError" parent="errorMessage" /> <bean id="testWarnWithParam" parent="warningMessage" />
The first example defines an error message with the key "testError" and the second example defines a warning message with the key "testWarnWithParam".
A message text should be written in a resource bundle. There can be as many resource bundles as there are locales supported. The resource bundles are loaded according to the current locale, or from the general resource bundle. Resource bundles should be named: basename_lang_country_variation.properties. For example for the base name "messages" a French resource bundle for France should be named: messages_fr_FR.properties, and an English resource bundle for the US should be named: messages_en_US.properties.
The general resource bundle is the "fall-back" bundle. If a key is not located in the localized resource bundle it is searched in the general bundle, and only then a default message is returned. The default message is: "@@@key@@@". Continuing the example above, the name of the general resource is messages.properties.
As mentioned before, a message may include parameters. Parameters are placeholders for runtime values in the message. The placeholders are defined in the message text as {x} whereas x is an incrementing index starting with 0.
For example, in the shinefw_messages_en_US.properties file in the common test:
testError=american english test error testWarnWithParam=warning test with param: {0}
The first line defines the text for a message with the key "testError", and the second line defines a text with a single parameter for a message with the key "testWarnWithParam".
The same messages are may also be defined in other resource bundles - in the general, and, as in the following French example, in a localized resource bundle. The following example is from the shinefw_messages_fr_FR.properties resource bundle:
testError=erreur française d''essai testWarnWithParam=essai d''avertissement avec le param: {0}
To use message:
Unlike previous versions of the framework, it is now not encouraged to obtain messages directly. The most common use of a message would be to hang it on a user context. Do that directly with the user context API.
If your message has runtime parameters, then you need to set them before you can use the message. If you do not they will appear as {x} in the formatted message. To set the parameters use the available API within the user context.
The parameters should be set in the order they appear in the message text.
In some cases a message may be usefull outside a user context. In these cases you can format the message directly using the Localizer. For example:
String formattedMessage=Localizer.getMessage("msgKey",new String[] {"param1","param2"});