uk.ac.essex.common.lang
Class Translator

java.lang.Object
  extended by uk.ac.essex.common.lang.Translator

public class Translator
extends java.lang.Object

Class to provide translation facilities, using locales, resource bundles and properties files. This class is a variation on a singleton class, all references to it should be obtained by using the getInstance() method, which returns (the same) static instance. However a singleton will be created for each resource base name. This allows you to have multiple subsystems each with their own resource base name and corresponding file. Yet at the same time still treat this class as a singleton. This is simply impleemted as a HashMap mapping resource base name to Translator instances. If the hashmap doesn't contain the resource base name then a translator is instantiated otherwise you get the old one.-

Locales and resource bundles basically just provide a hash map that maps keys to values. For each available locale their is a different hashmap. These different maps have the same keys but differnt sets of values, there is one for each language available.

By default this will search for the resources on the classpath and will use the default resource base name of "Resources". The only default locale it will search for is Locale.ENGLISH

See Also:
PropertyResourceBundle, Locale

Field Summary
static java.util.Locale DEFAULT_LOCALE
          The default locale to use this is Locale.ENGLISH
static java.lang.String DEFAULT_RESOURCE_BASE_NAME
          The default resource base name to search for if none is given - this is "Resources"
 
Method Summary
 java.lang.String get(java.util.Locale locale, java.lang.String key)
          Given a locale and a STRING key this returns the string for that key using that locale.
 java.lang.String get(java.lang.String key)
          Given a locale and a STRING key this returns the string for that key using the locale obtained by calling DEFAULT_LOCALE.
static Translator getInstance()
          This uses the default locales
static Translator getInstance(java.lang.String resourceBaseName)
           
static Translator getInstance(java.lang.String resourceBaseName, java.io.File directoryToLoadFrom)
           
static Translator getInstance(java.lang.String resourceBaseName, java.io.File directoryToLoadFrom, java.util.Locale[] locales)
           
static void main(java.lang.String[] args)
          Used for testing
static void setDefaultLocale(java.util.Locale defaultLocale)
           
 boolean supportsLanguage(java.util.Locale locale)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_RESOURCE_BASE_NAME

public static final java.lang.String DEFAULT_RESOURCE_BASE_NAME
The default resource base name to search for if none is given - this is "Resources"

See Also:
Constant Field Values

DEFAULT_LOCALE

public static java.util.Locale DEFAULT_LOCALE
The default locale to use this is Locale.ENGLISH

Method Detail

get

public java.lang.String get(java.lang.String key)
Given a locale and a STRING key this returns the string for that key using the locale obtained by calling DEFAULT_LOCALE.

Parameters:
key -
Returns:

get

public java.lang.String get(java.util.Locale locale,
                            java.lang.String key)
Given a locale and a STRING key this returns the string for that key using that locale. The locale given should be available ie/ have it's own properties file. All available translations are searched (those in the database & from the properties file)

Parameters:
locale - - The locale to use to 'translate' the given key
key - - The key of the required translation

supportsLanguage

public boolean supportsLanguage(java.util.Locale locale)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
a string representation of the object.

main

public static void main(java.lang.String[] args)
Used for testing


getInstance

public static Translator getInstance()
This uses the default locales

Returns:
Translator - A new (static) instance of Translator

getInstance

public static Translator getInstance(java.lang.String resourceBaseName,
                                     java.io.File directoryToLoadFrom)
Parameters:
resourceBaseName - - The resource base name files are then called for eg/ %RESOURCE_BASE_NAME%_en_EN.properties
directoryToLoadFrom - - The root directory where the resources are located, if this is null then the classpath will be used
Returns:
Translator - A new (static) instance of Translator

getInstance

public static Translator getInstance(java.lang.String resourceBaseName)
Parameters:
resourceBaseName - - The resource base name files are then called for eg/ %RESOURCE_BASE_NAME%_en_EN.properties
Returns:
Translator - A new (static) instance of Translator

getInstance

public static Translator getInstance(java.lang.String resourceBaseName,
                                     java.io.File directoryToLoadFrom,
                                     java.util.Locale[] locales)
                              throws java.lang.Exception
Parameters:
resourceBaseName - - The resource base name files are then called for eg/ %RESOURCE_BASE_NAME%_en_EN.properties
directoryToLoadFrom - - The root directory where the resources are located, if this is null then the classpath will be used
Returns:
Translator - A new (static) instance of Translator
Throws:
java.lang.Exception - - If it cannot find or load the resource files

setDefaultLocale

public static void setDefaultLocale(java.util.Locale defaultLocale)
Parameters:
defaultLocale -