MessageContextConfig
The message module is very extensible. Via the fluent API of the MessageContext
it's possible to change the configuration ( MessageContextConfig
) of the context or to create a new context.
It's possible to provide:
- MessageResolver
- MessageInterpolator
- LocaleResolver
- MessageHandler
- FormatterFactory
- Formatter
- Formatter-Config
MessageResolver
A message-resolver is responsible for using the message-descriptor (key or inline-text) to creating the equivalent text for it.
(The supported format e.g. if it's required to escape a key, if inline-text is supported,... depends on the concrete implementation.)
In case of a message-key, the message-resolver has to transform it to the message-text by looking it up in a message source like a resource-bundle. The JSF module of MyFaces CODI will provide a default implementation which uses the configured application message bundle as message source. However, usually it's required to provide an implementation in every project (because the impl. is aware of the message-source).
Code Block |
---|
title | Configuration of a message-resolver |
---|
borderStyle | solid |
---|
|
context.config().change().messageResolver(new CustomMessageResolver());
|
Note |
---|
|
In case of a key as message-descriptor you might see the key and some missing-message-markers like ??? or the key itself. In such a case the key isn't defined in the message-source or there is no or the wrong message-resolver configured for the current context. |
The result of a MessageResolver
is the message-text. The text might contain placeholders which are processed by a MessageInterpolator
MessageInterpolator
A MessageInterpolator
replaces the placeholders in a message-text with the arguments of the message. MyFaces CODI supports numbered- and named-arguments. However, it's possible to provide a custom MessageInterpolator
.
Code Block |
---|
title | Configuration of a message-interpolator |
---|
borderStyle | solid |
---|
|
context.config().change().messageResolver(new CustomMessageInterpolator());
|
Note |
---|
|
If the message still contains placeholders after the interpolation, there might be a missing argument or the message-text uses a wrong syntax for the placeholders or the current context has no or the wrong MessageInterpolator . |
LocaleResolver
A locale resolver provides the current local. The locale is e.g. used to by a MessageResolver
to choose the correct language for the message-text. The JSF module of MyFaces CODI provides a LocaleResolver
which uses the locale of the current view(-root) (if a FacesContext
is available).
Code Block |
---|
title | Configuration of a locale-resolver |
---|
borderStyle | solid |
---|
|
context.config().change().localeResolver(new CustomLocaleResolver());
|
MessageHandler
As soon as you add a message to the context, the message gets forwarded to all configured MessageHandler
s.
The JSF module of MyFaces CODI provides a MessageHandler
which converts the message to a FacesMessage
and adds it to the current view(-root). The mentioned MessageHandler
also supports message-severity.
Code Block |
---|
title | Configuration of a message-handler |
---|
borderStyle | solid |
---|
|
context.config().change().addMessageHandler(new CustomMessageHandler());
|
A FormatterFactory
creates a Formatter
for a given type. Such a factory is also responsible for managing Formatter
s and optional locale specific configs for the formatters.
Code Block |
---|
title | Configuration of a formatter-factory |
---|
borderStyle | solid |
---|
|
context.config().change().formatterFactory(new CustomFormatterFactory());
|
A Formatter
is responsible for 1-n types. A MessageInterpolator
uses Formatter
s for formatting arguments e.g. based on the current locale.
Code Block |
---|
title | Configuration of a formatter |
---|
borderStyle | solid |
---|
|
context.config().getFormatterFactory().add(new CustomFormatter());
|
A config for formatters is a generic config (key/value-pair). It's possible to register a config per argument-type (and also for a specific locale).
Code Block |
---|
title | Configuration of a formatter-factory |
---|
borderStyle | solid |
---|
|
context.config().getFormatterFactory().addFormatterConfig(MyCustomType.class, configForMyCustomType, currentLocale);
|
Info |
---|
|
Instead of context.config().change() it's also possible to use context.config().use() . Instead of changing the current context, the current context gets cloned and the new context uses the new resolver. |