General

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.

Defining a Message

To define a message you must:

  • define the message in your messages.xml as a Spring bean
  • write the message text in the general resource bundle, or:
  • write the message text in the localized resource bundle

Defining a Message as a Spring Bean

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".

Note:
There is no mention of any property in the message:
  • The severity is determined according the parent. Possible parents are: fatalMessage, errorMessage, warningMessage and infoMesssage.
  • The message text is defined in a resource bundle - see below.
  • The message key is defined by the bean id.

Writing the Message Text

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}

Using Messages

To use message:

  • obtain the message
  • optionally set the runtime parameters
  • optionally use the formatted message
Obtaining a 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.

Setting Runtime Parameters

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.

Formatting a Message

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"});