vmm.core
Class I18n

java.lang.Object
  extended by vmm.core.I18n

public class I18n
extends java.lang.Object

This class implements internationalization by providing facilites for getting strings from properties files. A single default property file is used, but other files can be added. All files that have been added are searched in the reverse of the order in which they were added. The default file is searched last. This class does not generate errors; if a property file cannot be found for a specified resource name, it is ignored. If no value can be found for a specified key, then either null or the key itself is returned rather than generating an error, depending on which method was called.

The default properties file name is "strings.properties" in the directory vmm/resources. Property files for other locales should be given names such as, for example, strings_fr.properties, ' and stored in the same location.


Constructor Summary
I18n()
           
 
Method Summary
static boolean addFile(java.lang.String fileName)
          Adds a file to the list of property files that are searched when looking for a string.
static java.util.Locale getLocale()
          Returns the locale that is currently being used by this class.
static void setLocale(java.util.Locale locale)
          Sets a locale to use when searching for property files.
static void setLocale(java.util.Locale locale, boolean reload)
          Sets a locale to use when searching for property files.
static java.lang.String tr(java.lang.String key)
          Finds the value associated with a specified key.
static java.lang.String tr(java.lang.String key, java.lang.Object... arg)
          A convenience method that calls MessageFormat.format(tr(key), arg...).
static java.lang.String trIfFound(java.lang.String key)
          Does the same thing as I18n.tr(key), except that if no value is found for the key, the return value is null.
static java.lang.String trIfFound(java.lang.String key, java.lang.Object... arg)
          Does the same thing as I18n.tr(key,arg...), except that if no value is found for the key, the return value is null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

I18n

public I18n()
Method Detail

setLocale

public static void setLocale(java.util.Locale locale)
Sets a locale to use when searching for property files. This does not affect any property files that have already been loaded. If the locale is null (which is the default), then the default locale will be used -- which is almost always what you want to do! Property files that have already been loaded are reloaded. (This method just calls "setLocale(locale,true)".) It is not usually necessary to call this method. It could be used, for example, to give the user a choice of languages.


setLocale

public static void setLocale(java.util.Locale locale,
                             boolean reload)
Sets a locale to use when searching for property files. If the reload argument is false, then this will not affect property files that have already been loaded; if reload is true, then all property files that have already been loaded are discarded and new ones are loaded using the new locale. If the locale is null (which is the default), then the default locale will be used -- which is almost always what you want to do! It is not usually necessary to call this method. It could be used, for example, to give the user a choice of languages.


getLocale

public static java.util.Locale getLocale()
Returns the locale that is currently being used by this class.

Returns:
the current locale for internationalization. The return value is non-null. If no non-null locale has been set by setLocale(Locale), then the default locale for the Java Virtual Machine is returned.

addFile

public static boolean addFile(java.lang.String fileName)
Adds a file to the list of property files that are searched when looking for a string. Property files are searched in the reverse of the order in which they are added. That is, if a file contains a key that also occurred in a previously added file, the value in the new file will hide the value in the old file.

Parameters:
fileName - The resource name for the property file to be added. The bundle is obtained using the getBundle method in the ResourceBundle class. If the file name is null, nothing is done. If the file name is a duplicate of a name that was used previously, the bundle is NOT added to the search path for a second time. Note that the file name should be given as a resource name, such as "vmm.resources.strings", that can be used to locate the resource, rather than as an actual file name.
Returns:
The return value indicates whether a resource bundle was found. If the return value is false, it means that no property file with the specified resource name was found.

tr

public static java.lang.String tr(java.lang.String key)
Finds the value associated with a specified key. All loaded reseource bundles are checked, in the reverse of the order in which they were added. (The resource bundle with the default name is automatically added first, so is the last one checked.) The name of this method, "tr", is short for "translate".

Returns:
The value associated with the key. If the key does not occur in any of the loaded resource bundles (or if key is null), then the key itself is returned.

tr

public static java.lang.String tr(java.lang.String key,
                                  java.lang.Object... arg)
A convenience method that calls MessageFormat.format(tr(key), arg...). That is, the translation of key can contain substrings of the form {0}, {1}, {2}, ..., and the arg parameters is substituted for those substrings wherever thye occur.

Parameters:
key - String to be translated, by calling tr(key). The translation can contain substrings of the form {0}, {1}, {2}...
arg - The strings (or other objects) that are to be substituted for {0}, {1}, {2}, ...
Returns:
The translated string, with any occurance of {0}, {1}, {2}... replaced by the arg's. If no translated string is found, then the substitution is done on the key string.

trIfFound

public static java.lang.String trIfFound(java.lang.String key)
Does the same thing as I18n.tr(key), except that if no value is found for the key, the return value is null.

Returns:
The value associated with the key. If the key does not occur in any of the loaded resource bundles (or if key is null), then null is returned.
See Also:
tr(String)

trIfFound

public static java.lang.String trIfFound(java.lang.String key,
                                         java.lang.Object... arg)
Does the same thing as I18n.tr(key,arg...), except that if no value is found for the key, the return value is null.

See Also:
tr(String, Object[])