fi.hu.cs.titokone
Class Translator

java.lang.Object
  extended byfi.hu.cs.titokone.Translator

public class Translator
extends java.lang.Object

This class deals with translating strings. It also remembers which language is currently set, but does not know specifically what languages are currently available.


Field Summary
private static java.util.Locale currentLocale
          This field stores the current locale.
static java.util.Locale defaultLocale
          This field contains the default locale.
private static java.util.ResourceBundle defaultTranslations
          This field stores the default ResourceBundle.
static java.lang.String resourceFamilyName
          This name identifies the resource files containing translations for this software.
private static boolean TESTING
          This field should be false during normal operation.
private static java.util.ResourceBundle translations
          This field stores the current ResourceBundle in use.
 
Constructor Summary
Translator()
           
 
Method Summary
static java.util.ResourceBundle getResourceBundle()
          This method returns the resource bundle in use.
static void setLocale(java.util.Locale newLocale)
          This method sets the current locale in use and fetches a corresponding ResourceBundle that contains the translations most suitable for this locale.
static void setLocale(java.util.Locale newLocale, java.util.ResourceBundle newTranslations)
          This method sets the current locale in use and tries to fetch the translation from translationPath.
static java.lang.String translate(java.lang.String keyString)
          This function translates a fixed string to the currently used language.
static java.lang.String translate(java.lang.String keyString, java.lang.String[] parameters)
          This function translates a template string to the currently used language and replaces any {i} markers in it with strings from the parameters array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TESTING

private static final boolean TESTING
This field should be false during normal operation. It can be set to true to enable some testability quirks.

See Also:
Constant Field Values

resourceFamilyName

public static final java.lang.String resourceFamilyName
This name identifies the resource files containing translations for this software.

See Also:
Constant Field Values

defaultLocale

public static final java.util.Locale defaultLocale
This field contains the default locale.


currentLocale

private static java.util.Locale currentLocale
This field stores the current locale. It defaults to defaultLocale.


defaultTranslations

private static java.util.ResourceBundle defaultTranslations
This field stores the default ResourceBundle.


translations

private static java.util.ResourceBundle translations
This field stores the current ResourceBundle in use.

Constructor Detail

Translator

public Translator()
Method Detail

translate

public static java.lang.String translate(java.lang.String keyString)
This function translates a fixed string to the currently used language. If the current language has no string corresponding to the key, a default translation is used from Translations.class. If that class does not have the translation either, the keystring itself is returned. The translation therefore fails silently.

Parameters:
keyString - A string key that identifies the translation in a Translations*class file.
Returns:
The translated string, if available, or something otherwise usable.

translate

public static java.lang.String translate(java.lang.String keyString,
                                         java.lang.String[] parameters)
This function translates a template string to the currently used language and replaces any {i} markers in it with strings from the parameters array. If the current language has no string corresponding to the key, a default translation is used from Translations.class. If that class does not have the translation either, the keystring itself is returned. The translation therefore fails silently. The translation string is fetched from the current ResourceBundle in use, and the replacement is done with the help of java.text.MessageFormat.

Parameters:
keyString - A string key that identifies the translation in a Translations*class file.
parameters - A string array containing strings to replace {i} markers in the string in order - that is, parameters[0] replaces {0}, parameters[1] replaces {1} etc.
Returns:
The translated string, if available, or something otherwise usable, with {i} markers replaced from the parameters array as far as there are available replacements.

setLocale

public static void setLocale(java.util.Locale newLocale)
This method sets the current locale in use and fetches a corresponding ResourceBundle that contains the translations most suitable for this locale. This may mean the default English translation if there is nothing better available.

Parameters:
newLocale - The locale to switch to, eg. new Locale("fi", "FI").

setLocale

public static void setLocale(java.util.Locale newLocale,
                             java.util.ResourceBundle newTranslations)
This method sets the current locale in use and tries to fetch the translation from translationPath. Use of setLocale(Locale) is recommended, but this method enables the user to have very exact control over where the translations are found.

Parameters:
newLocale - The locale to switch to, eg. new Locale("fi", "FI").
newTranslations - A class containing the translations for this locale. If the translations are located in the standard place, setLocale(Locale) can be used.

getResourceBundle

public static java.util.ResourceBundle getResourceBundle()
This method returns the resource bundle in use.

Returns:
The resource bundle set to correspond to the current locale.