|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object uic.pjava.Translate
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.propertiesfrom 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
Nested Class Summary | |
static class |
Translate.Translator
Class that holds the actual translation strings |
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 to 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 |
public Translate()
public Translate(String section)
section
- the section of your application that this translate belongs to.public Translate(String section, Object o)
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 classloaded to load the properties file from.Method Detail |
public void setSection(String newSection)
public String i18n(String in)
i18n
in interface TranslationInterface
in
- the input string you want translated
public String getButtonText(String in)
getButtonText
in interface TranslationInterface
public char getMnemonic(String in)
getMnemonic
in interface TranslationInterface
public static void setLocale(String language, String country, String variant)
For the countr 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
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)public static void setLocale(String language, String country)
public static void setLocale(Locale locale)
public static Locale getLocale()
public static Translate createTranslator(String section)
public static Translate createTranslator(String section, Object o)
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.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |