Class I18NMessageProvider

  • All Implemented Interfaces:
    Serializable

    public class I18NMessageProvider
    extends Object
    implements Serializable
    An internationalized message provider, mapping instances of I18NMessage onto text. The locale used for the translation is either one supplied at the time of translation, or the active locale per ActiveLocale; if the chosen locale does not provide a message, the default JVM locale and root locale (in that order) are used as fallbacks.

    Message providers can be serialized. However, upon deserialization, they are not guaranteed to have the same classloader as during serialization, and hence may be unable to access the same message files as were available during serialization. If this happens, deserialization will fail.

    Since:
    0.5.0
    Version:
    $Id: I18NMessageProvider.java 16154 2012-07-14 16:34:05Z colin $
    Author:
    tlerios@marketcetera.com
    See Also:
    Serialized Form
    • Constructor Detail

      • I18NMessageProvider

        public I18NMessageProvider​(String providerId,
                                   ClassLoader classLoader)
        Creates a new message provider with the given ID. The provider ID is combined with the suffix MESSAGE_FILE_EXTENSION to form the name of a mapping file. The file should be retrievable as a resource via the given class loader, and its format should be that used by Apache Commons i18n.
        Parameters:
        providerId - The provider ID.
        classLoader - The class loader used to load the mapping file. It may be null to use the default classloader.
      • I18NMessageProvider

        public I18NMessageProvider​(String providerId)
        Creates a new message provider with the given ID. The provider ID is combined with the suffix MESSAGE_FILE_EXTENSION to form the name of a mapping file. The file should be retrievable as a resource via the default class loader, and its format should be that used by Apache Commons i18n.
        Parameters:
        providerId - The provider ID.
    • Method Detail

      • init

        private void init​(ClassLoader classLoader)
                   throws MessageNotFoundException
        Initializes the receiver.
        Parameters:
        classLoader - The class loader used to load the mapping file. It may be null to use the default classloader.
        Throws:
        MessageNotFoundException - Thrown if initialization fails.
      • getProviderId

        public String getProviderId()
        Returns the receiver's provider ID.
        Returns:
        The ID.
      • getBaseName

        private String getBaseName()
        Returns the receiver's base name.
        Returns:
        The base name.
      • getText

        public String getText​(Locale locale,
                              I18NMessage message,
                              Object... params)
        Returns the text of the given message in the given locale, using the receiver's map. The given parameters are used to instantiate parameterized messages.
        Parameters:
        locale - The locale.
        message - The message.
        params - The message parameters.
        Returns:
        Returns the text. If the message cannot be found, an error is logged, and a simple form of the message comprising its IDs and parameters is returned.
      • getText

        public String getText​(I18NMessage message,
                              Object... params)
        Returns the text of the given message in the active locale per ActiveLocale. The given parameters are used to instantiate parameterized messages.
        Parameters:
        message - The message.
        params - The message parameters.
        Returns:
        Returns the text. If the message cannot be found, an error is logged, and a simple form of the message comprising its IDs and parameters is returned.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object