Package wsh.util

Class Localize


  • public class Localize
    extends java.lang.Object
    Localize messages for end users, using a Formatter pattern and a localized resource bundle. In a class like MyPanel.java you might have lines like this:
     double value = 3.14;
     String msg = "The value "+value+" is too large";
     
    Instead, in the same package as MyPanel.java, create a file MyPanel.properties, with this line:
     too_large = The value %g is too large.
     
    Then you can replace the original lines by this.
     private static Localize local = new Localize(MyPanel.class);
     ...
    
     double value = 3.14;
     String msg = local.format("too_large", value);
     
    If you create an additional file called MyPanel_es.properties, then that file will be used automatically in Spanish-speaking locales.
    • Constructor Summary

      Constructors 
      Constructor Description
      Localize​(java.lang.Class<?> clazz)
      Construct for localized messages.
      Localize​(java.lang.Class<?> clazz, java.lang.String resourceBundleName)
      Construct for localized messages.
      Localize​(java.lang.Class<?> clazz, java.lang.String resourceBundleName, java.util.Locale locale)
      Construct for localized messages.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String filter​(java.lang.String message, java.lang.Class<?> resourceClass)
      Deprecated.
      Prefer other methods for standard localization.
      static java.lang.String filter​(java.lang.String message, java.util.ResourceBundle catalog)
      Filter the specified string with the specified resource bundle.
      java.lang.String format​(java.lang.String key, java.lang.Object... args)
      Format a localized message, with java.util.Formatter and the appropriate resource.
      static java.lang.String getMessage​(java.lang.Throwable throwable)
      Get the best localized message from a Throwable that may contain other Throwables as a cause.
      static java.util.ResourceBundle getResourceBundle​(java.lang.Class<?> clazz, java.lang.String resourceBundleName, java.util.Locale locale)
      Get a resource bundle associated with a class.
      static java.lang.String timeWords​(long seconds)
      Convert a number of seconds into words
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Localize

        public Localize​(java.lang.Class<?> clazz)
        Construct for localized messages.
        Parameters:
        clazz - Name of client class that requires localization Class<Foo> is assumed to have property files Foo.properties, Foo_es.properties, etc. Uses default Locale.
      • Localize

        public Localize​(java.lang.Class<?> clazz,
                        java.lang.String resourceBundleName)
        Construct for localized messages.
        Parameters:
        clazz - Name of client class that requires localization
        resourceBundleName - Name of ResourceBundle to be used for localizing messages. If null, then Class<Foo> is assumed to have property files Foo.properties, Foo_es.properties, etc. Uses default Locale.
      • Localize

        public Localize​(java.lang.Class<?> clazz,
                        java.lang.String resourceBundleName,
                        java.util.Locale locale)
        Construct for localized messages.
        Parameters:
        clazz - Name of client class that requires localization
        resourceBundleName - Name of ResourceBundle to be used for localizing messages. If null, then Class<Foo> is assumed to have property files Foo.properties, Foo_es.properties, etc.
        locale - Locale to use for localization. If null, then will use default Locale.
    • Method Detail

      • format

        public java.lang.String format​(java.lang.String key,
                                       java.lang.Object... args)
        Format a localized message, with java.util.Formatter and the appropriate resource.
        Parameters:
        key - used to specify format string in properties file. If not found, then will be used as the format.
        args - Optional arguments to be passed to Formatter.format method.
        Returns:
        formatted localized String
      • getResourceBundle

        public static java.util.ResourceBundle getResourceBundle​(java.lang.Class<?> clazz,
                                                                 java.lang.String resourceBundleName,
                                                                 java.util.Locale locale)
        Get a resource bundle associated with a class.
        Parameters:
        clazz - Name of client class that requires resource bundle.
        resourceBundleName - Name of ResourceBundle to be used for localizing messages. If null, then Class<Foo> is assumed to have property files Foo.properties, Foo_es.properties, etc.
        locale - Locale to use for localization. If null, then will use default Locale.
        Returns:
        ResourceBundle for this locale.
      • getMessage

        public static java.lang.String getMessage​(java.lang.Throwable throwable)
        Get the best localized message from a Throwable that may contain other Throwables as a cause.
        Parameters:
        throwable - a Throwable that may contain other Throwables as a cause.
        Returns:
        best localized message, unwrapping as necessary.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • filter

        public static java.lang.String filter​(java.lang.String message,
                                              java.util.ResourceBundle catalog)
        Filter the specified string with the specified resource bundle. First the entire string is filtered, then substrings delimited as ${key} are filtered.
        Parameters:
        message - Filter this string
        catalog - Get keys and values from this.
        Returns:
        Filtered string.
      • filter

        @Deprecated
        public static java.lang.String filter​(java.lang.String message,
                                              java.lang.Class<?> resourceClass)
        Deprecated.
        Prefer other methods for standard localization.
        Filter the specified string with a ResourceBundle for the specified class. First the entire string is filtered, then substrings delimited as ${key} are filtered.
        Parameters:
        message - Filter this string
        resourceClass - Use the class loader for this class and load a localized PropertyResourceBundle with the same name as the class. For example, a class Foo has a Foo.properties and a Foo_ch.properties file in the same directory as the compiled Foo.class. Test with java -Duser.language=ch
        Returns:
        Filtered string.
      • timeWords

        public static java.lang.String timeWords​(long seconds)
        Convert a number of seconds into words
        Parameters:
        seconds - Number of seconds
        Returns:
        Localized words describing the number of seconds.