Class LocalizableString


  • public class LocalizableString
    extends Object
    Represents a String which could be localizable. If it is localizable it needs to be in the following format, i18n:bundle#key which is a URI where:
    • i18n: is the scheme specifying that the string is localizable
    • bundle is the path of the bundle in the classpath (optional, if missing, a Class has to be provided and will be used as resource bundle name)
    • key, the fragment, is the key of the translated string
    This class attempts to make the i18n work for an OSGi environment, by encapsulating the name of a resource bundle, the key in the resource bundle to use, and the ClassLoader in which the resource bundle can be found, in the assumption that when it comes to serializing this object, the calling code (e.g. HttpFrameworkServlet and the Grizzly HandlerAdapter) is in a different classloader and so will not have direct access to the resource bundle.

    A default value LocalizableString can be provided so that if the key is not found in the bundle, another value can be specified, which could be either another bundle reference, or a plain value.

    • Field Detail

      • TRANSLATION_KEY_PREFIX

        public static final String TRANSLATION_KEY_PREFIX
        A constant used to indicate a string should be translated.
        See Also:
        Constant Field Values
    • Constructor Detail

      • LocalizableString

        public LocalizableString​(String value)
        String only constructor for non-localizable String values.
        Parameters:
        value - a string
      • LocalizableString

        public LocalizableString​(String value,
                                 Class<?> type)
        Constructor for potentially localizable String. If resource bundle name not provided in the value, then the provided type name will be used instead.
        Parameters:
        value - the String (i18n:#key.name is accepted here)
        type - class used to support relative resource bundle lookup (must not be null)
      • LocalizableString

        public LocalizableString​(String value,
                                 ClassLoader loader)
        Constructor for potentially localizable String.
        Parameters:
        value - the String
        loader - the ClassLoader where the string definition should be obtained
      • LocalizableString

        public LocalizableString​(String key,
                                 ClassLoader loader,
                                 LocalizableString defaultValue)
        Constructor for potentially localizable String. If a default value is not specified, if the key is a valid URI, its fragment will be used, and otherwise the whole key value will be used.
        Parameters:
        key - the localizable key
        loader - the ClassLoader where the string definition should be obtained
        defaultValue - the default value to use if not localizable.
    • Method Detail

      • toTranslatedString

        public String toTranslatedString​(PreferredLocales locales)
        Returns the contained string, translated if applicable.
        Parameters:
        locales - The preferred locales for the translation.
        Returns:
        the translated string
      • toString

        public String toString()
        The default toString method. No translation is applied.
        Overrides:
        toString in class Object
        Returns:
        the untranslated string value
      • equals

        public boolean equals​(Object o)
        The default equals operation.
        Overrides:
        equals in class Object
        Parameters:
        o - another object
        Returns:
        true if the other object is equal
      • hashCode

        public int hashCode()
        Default hashcode implementation.
        Overrides:
        hashCode in class Object
        Returns:
        the hashcode of the string