uic
Class Translate

java.lang.Object
  extended by uic.Translate
All Implemented Interfaces:
TranslationInterface

public class Translate
extends Object
implements TranslationInterface

Translate is a class that you can use to translate the texts on a GUI. To make a gui change for a different language all you need to do is change the texts on widgets like labels and buttons. If you use UICompiler to build your GUIs the strings will automatically use this class to find out if the current locale needs different strings on screen.

So; how does that work; First, find out what translating means by checking the introduction to internationalisation here[java.sun.com]

Your GUI has strings that are hardcoded in the java file that shows them; this is a standard approuch that most application developers use:

      myPanel.add(new JLabel("Your Name Please:")); 
Using Translate you can ask for the string that should be displayed on screen based in the input string, this means you will get this:
      Translate translate = new Translate("entryform");
      myPanel.add(new JLabel( translate.i18n("Your Name Please:") )); 
the call to i18n() means that Translate will try to open one of
      entryform_en_US.properties
      entryform_en.properties
      entryform.properties
from the current classpath and find if the string was translated in that file. If it finds the string it will return the value it is translated into, otherwise it will assume no translation is available and just return the original string.

The format of the properties file is just that; a properties file. The file is based on 2 rules;
1) seperation of key and value is done using a '=' (equals sign) or a ' ' (space). If the key should contain that value it should be escaped using a backslash '\'.
2) no characters with unicode values above 255 are allowed; they should be escaped using the ሴ escape sequence (in hexedecimal)

The above string could be translated into Dutch by providing a file named entryform_nl_NL.properties with at least one line which has the format:

  Your\ Name\ Please: = Uw naam alstublieft:

After providing file to jave (in your classpath) you should tell Translate that it should Dutch as its language. As Dutch is nl_NL according to the ISO norms your code becomes:

      Translate.setLocale("nl","NL");
      Translate translate = new Translate("entryform");
      myPanel.add(new JLabel( translate.i18n("Your Name Please:") ));

There are a number of ways to use the Translator, the above method is just one of them, the class was written to be flexible with many defaults to make it hard to get errors during operation.
Just read the API docs for the methods to find out more.

Another possibly interresting link is: www.javai18n.com

Since:
1.1

Nested Class Summary
static class Translate.Translator
           
 
Constructor Summary
Translate()
          Initialize a new Translate object.
Translate(String section)
          Create a new Translate that will get translations from the properties file named 'section_[lan]_[country].properties
Translate(String section, Object o)
          Create a new Translate that will get translations from the properties file named 'section_[lan]_[country].properties
 
Method Summary
static Translate createTranslator(String section)
          Convinience method
static Translate createTranslator(String section, Object o)
          Create and initialize a translator.
 String getButtonText(String in)
          In Qt the '&' in front of a character represents a mnemonic; but should not be visible on buttons; this method returns the string sans the '&'.
static Locale getLocale()
           
 char getMnemonic(String in)
          In Qt the '&' in front of a character represents a mnemonic; but should not be visible on buttons; this method returns the first character that follows the '& char.
 String i18n(String in)
          To do internationalisation you call this method.
static void setLocale(Locale locale)
          convienience method
static void setLocale(String language, String country)
          convienience method
static void setLocale(String language, String country, String variant)
          Set one time per application, all following calls to createTranslator will look for translations in the given Locale.
 void setSection(String newSection)
          To change the key that this Translate object can be fetched with set a new one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Translate

public Translate()
Initialize a new Translate object. This constructor will assume the DEFAULT section should be used and all calls to this instance will be directed to that.


Translate

public Translate(String section)
Create a new Translate that will get translations from the properties file named 'section_[lan]_[country].properties

Parameters:
section - the section of your application that this translate belongs to.

Translate

public Translate(String section,
                 Object o)
Create a new Translate that will get translations from the properties file named 'section_[lan]_[country].properties

Parameters:
section - the section of your application that this translate belongs to.
o - when the translations have not been loaded by this application before, Translate will use the Object o to find a classloader to load the properties file from.
Method Detail

setSection

public void setSection(String newSection)
To change the key that this Translate object can be fetched with set a new one. Subsequent Translate objects that are instanciated with the section key will find the resources this object carries with it.


i18n

public String i18n(String in)
To do internationalisation you call this method. Note that internationalisation is too long and all the industry has now conformed to the i18n naming (where i18n is a count of characters).

Specified by:
i18n in interface TranslationInterface
Parameters:
in - the input string you want translated
Returns:
the translated string, or the input when no translation is available.

getButtonText

public String getButtonText(String in)
In Qt the '&' in front of a character represents a mnemonic; but should not be visible on buttons; this method returns the string sans the '&'.

Specified by:
getButtonText in interface TranslationInterface

getMnemonic

public char getMnemonic(String in)
In Qt the '&' in front of a character represents a mnemonic; but should not be visible on buttons; this method returns the first character that follows the '& char.

Specified by:
getMnemonic in interface TranslationInterface

setLocale

public static void setLocale(String language,
                             String country,
                             String variant)
Set one time per application, all following calls to createTranslator will look for translations in the given Locale. For the language code you have to use ISO 639; You can find a full list of these codes at a number of sites, such as: http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt

For the country code you have to use ISO 3166; You can find a full list of these codes at a number of sites, such as: http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html

Parameters:
language - These codes are the lower-case two-letter codes as defined by ISO-639.
country - These codes are the upper-case two-letter codes as defined by ISO-3166.
variant - the variant of the locale (e.d. EURO)

setLocale

public static void setLocale(String language,
                             String country)
convienience method


setLocale

public static void setLocale(Locale locale)
convienience method


getLocale

public static Locale getLocale()
Returns:
the set locale; or Locale.getDefault() when none set

createTranslator

public static Translate createTranslator(String section)
Convinience method


createTranslator

public static Translate createTranslator(String section,
                                         Object o)
Create and initialize a translator.

Parameters:
section - the module you are loading; this should correspond to the filename of your bundle.
o - object from the same classloader that is to be used to find the resourcebundle.
Returns:
an initialized Translate object.


Copyright © 2002-2004 Thomas Zander Available under the Free Apache licence