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

Hint

The message module isn't aware of CDI APIs. Therefore, it's possible to use it outside a CDI container in any Java based application. However, if you directly inject an artifact of the message-module in a CDI bean, a dependent scoped instance will be created by default. That means a custom producer is needed to change the configuration and scope it in the scope of your choice. Otherwise the configuration would just change the dependent instance -> configuration via any kind of startup-listeners won't work.

MessageResolver

A message-resolver is responsible for creating the message-text based on the message-descriptor (key or inline-text), the current locale (and in some cases the message-payload).
(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).

Configuration of a message-resolver
context.config().change().messageResolver(new CustomMessageResolver());

Hint

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.

Configuration of a message-interpolator
context.config().change().messageResolver(new CustomMessageInterpolator());

Hint

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 locale. 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).

Configuration of a locale-resolver
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.

Configuration of a message-handler
context.config().change().addMessageHandler(new CustomMessageHandler());

FormatterFactory

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.

Configuration of a formatter-factory
context.config().change().formatterFactory(new CustomFormatterFactory());

Formatter

A Formatter is responsible for 1-n types. A MessageInterpolator uses Formatter s for formatting arguments e.g. based on the current locale.

Configuration of a formatter
context.config().getFormatterFactory().add(new CustomFormatter());

Formatter-Config

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

Configuration of a formatter-factory
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 e.g. a new resolver which is configured via the fluent api.

  • No labels