Package wsh.util
Class Localize
- java.lang.Object
-
- wsh.util.Localize
-
public class Localize extends java.lang.ObjectLocalize 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.Stringfilter(java.lang.String message, java.lang.Class<?> resourceClass)Deprecated.Prefer other methods for standard localization.static java.lang.Stringfilter(java.lang.String message, java.util.ResourceBundle catalog)Filter the specified string with the specified resource bundle.java.lang.Stringformat(java.lang.String key, java.lang.Object... args)Format a localized message, with java.util.Formatter and the appropriate resource.static java.lang.StringgetMessage(java.lang.Throwable throwable)Get the best localized message from a Throwable that may contain other Throwables as a cause.static java.util.ResourceBundlegetResourceBundle(java.lang.Class<?> clazz, java.lang.String resourceBundleName, java.util.Locale locale)Get a resource bundle associated with a class.static java.lang.StringtimeWords(long seconds)Convert a number of seconds into wordsjava.lang.StringtoString()
-
-
-
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 localizationresourceBundleName- 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 localizationresourceBundleName- 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:
toStringin classjava.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 stringcatalog- 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 stringresourceClass- 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.
-
-