Class LocalizableSupport

  • All Implemented Interfaces:
    Localizable

    public class LocalizableSupport
    extends java.lang.Object
    implements Localizable
    This class provides a default implementation of the Localizable interface. You can use it as a base class or as a member field and delegates various work to it.

    For example, to implement Localizable, the following code can be used:

      package mypackage;
      ...
      public class MyClass implements Localizable {
          // This code fragment requires a file named
          // 'mypackage/resources/Messages.properties', or a
          // 'mypackage.resources.Messages' class which extends
          // java.util.ResourceBundle, accessible using the current
          // classpath.
          LocalizableSupport localizableSupport =
              new LocalizableSupport("mypackage.resources.Messages");
    
          public void setLocale(Locale l) {
              localizableSupport.setLocale(l);
          }
          public Local getLocale() {
              return localizableSupport.getLocale();
          }
          public String formatMessage(String key, Object[] args) {
              return localizableSupport.formatMessage(key, args);
          }
      }
     
    The algorithm for the Locale lookup in a LocalizableSupport object is:
    • if a Locale has been set by a call to setLocale(), use this Locale, else,
    • if a Locale has been set by a call to the setDefaultLocale() method of a LocalizableSupport object in the current LocaleGroup, use this Locale, else,
    • use the object returned by Locale.getDefault() (and set by Locale.setDefault()).
    This offers the possibility to have a different Locale for each object, a Locale for a group of object and/or a Locale for the JVM instance.

    Note: if no group is specified a LocalizableSupport object belongs to a default group common to each instance of LocalizableSupport.

    Version:
    $Id: LocalizableSupport.java 1598621 2014-05-30 14:55:00Z ssteiner $ Originally authored by Stephane Hillion.
    • Field Detail

      • localeGroup

        protected LocaleGroup localeGroup
        The locale group to which this object belongs.
      • bundleName

        protected java.lang.String bundleName
        The resource bundle classname.
      • classLoader

        protected java.lang.ClassLoader classLoader
        The classloader to use to create the resource bundle.
      • locale

        protected java.util.Locale locale
        The current locale.
      • usedLocale

        protected java.util.Locale usedLocale
        The locale in use.
      • resourceBundle

        protected java.util.ResourceBundle resourceBundle
        The resources
    • Constructor Detail

      • LocalizableSupport

        public LocalizableSupport​(java.lang.String s)
        Same as LocalizableSupport(s, null).
      • LocalizableSupport

        public LocalizableSupport​(java.lang.String s,
                                  java.lang.ClassLoader cl)
        Creates a new Localizable object. The resource bundle class name is required allows the use of custom classes of resource bundles.
        Parameters:
        s - must be the name of the class to use to get the appropriate resource bundle given the current locale.
        cl - is the classloader used to create the resource bundle, or null.
        See Also:
        ResourceBundle