org.xnap.commons.i18n

Class I18n


public class I18n
extends java.lang.Object

Provides methods for internationalization.

All text messages wrapped with one of the methods tr(String), trc(String,String), marktr(String), trn(String,String,long) etc. can be extracted into a pot file using xgettext from GNU gettext:

   xgettext -ktrc -ktr -kmarktr -ktrn:1,2 -o keys.pot *.java 
 
Authors:
Steffen Pingel
Felix Berger
Tammo van Lessen

Constructor Summary

I18n(ResourceBundle bundle)
Constructs an I18n object for a resource bundle.
I18n(String baseName, Locale locale, ClassLoader loader)
Constructs an I18n object by calling setResources(String,Locale,ClassLoader).

Method Summary

ResourceBundle
getResources()
Returns the current resource bundle.
static String
marktr(String text)
Marks text to be translated, but doesn't return the translation but text itself.
boolean
setLocale(Locale locale)
Tries to load a resource bundle for the locale.
void
setResources(ResourceBundle newBundle)
Sets a resource bundle to be used for message translations.
void
setResources(String baseName, Locale locale, ClassLoader loader)
Tries to load a resource bundle using ResourceBundle.getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader).
void
setSourceCodeLocale(Locale locale)
Sets the locale of the text in the source code.
String
tr(String text)
Returns text translated into the currently selected language.
String
tr(String text, Object o1)
Convenience method that invokes tr(String,Object[]).
String
tr(String text, Object o1, Object o2)
Convenience method that invokes tr(String,Object[]).
String
tr(String text, Object o1, Object o2, Object o3)
Convenience method that invokes tr(String,Object[]).
String
tr(String text, Object o1, Object o2, Object o3, Object o4)
Convenience method that invokes tr(String,Object[]).
String
tr(String text, Object[] objects)
Returns text translated into the currently selected language.
String
trc(String comment, String text)
Disambiguates translation keys.
String
trn(String text, String pluralText, long n)
Returns the plural form for n of the translation of text.
String
trn(String text, String pluralText, long n, Object o1)
Overloaded method that invokes trn(String,String,long,Object[]) passing Object arguments as an array.
String
trn(String text, String pluralText, long n, Object o1, Object o2)
Overloaded method that invokes trn(String,String,long,Object[]) passing Object arguments as an array.
String
trn(String text, String pluralText, long n, Object o1, Object o2, Object o3)
Overloaded method that invokes trn(String,String,long,Object[]) passing Object arguments as an array.
String
trn(String text, String pluralText, long n, Object o1, Object o2, Object o3, Object o4)
Overloaded method that invokes trn(String,String,long,Object[]) passing Object arguments as an array.
String
trn(String text, String pluralText, long n, Object[] objects)
Returns the plural form for n of the translation of text.

Constructor Details

I18n

public I18n(ResourceBundle bundle)
Constructs an I18n object for a resource bundle.
Parameters:
bundle - must not be null

I18n

public I18n(String baseName,
            Locale locale,
            ClassLoader loader)

Method Details

getResources

public ResourceBundle getResources()
Returns the current resource bundle.

marktr

public static final String marktr(String text)
Marks text to be translated, but doesn't return the translation but text itself.

setLocale

public boolean setLocale(Locale locale)
Tries to load a resource bundle for the locale.

The resource bundle is then used for message translations. Note, you have to retrieve all messages anew after a locale change in order for them to be translated to the language specified by the new locale.


setResources

public void setResources(ResourceBundle newBundle)

setResources

public void setResources(String baseName,
                         Locale locale,
                         ClassLoader loader)
Tries to load a resource bundle using ResourceBundle.getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader).

setSourceCodeLocale

public void setSourceCodeLocale(Locale locale)
Parameters:
locale - the locale

tr

public final String tr(String text)
Returns text translated into the currently selected language. Every user-visible string in the program must be wrapped into this function.
Parameters:
text - text to translate
Returns:
the translation

tr

public final String tr(String text,
                       Object o1)

tr

public final String tr(String text,
                       Object o1,
                       Object o2)

tr

public final String tr(String text,
                       Object o1,
                       Object o2,
                       Object o3)

tr

public final String tr(String text,
                       Object o1,
                       Object o2,
                       Object o3,
                       Object o4)

tr

public final String tr(String text,
                       Object[] objects)
Returns text translated into the currently selected language.

Occurrences of {number} placeholders in text are replaced by objects.

Invokes MessageFormat.format(java.lang.String, java.lang.Object[]).

Parameters:
text - text to translate
objects - arguments to MessageFormat.format()
Returns:
the translated text

trc

public final String trc(String comment,
                        String text)
Disambiguates translation keys.
Parameters:
comment - the text translated + a disambiguation hint in brackets.
text - the ambiguous key string

trn

public final String trn(String text,
                        String pluralText,
                        long n)
Returns the plural form for n of the translation of text.
Parameters:
text - the key string to be translated.
pluralText - the plural form of text.
n - value that determines the plural form
Returns:
the translated text

trn

public final String trn(String text,
                        String pluralText,
                        long n,
                        Object o1)

trn

public final String trn(String text,
                        String pluralText,
                        long n,
                        Object o1,
                        Object o2)

trn

public final String trn(String text,
                        String pluralText,
                        long n,
                        Object o1,
                        Object o2,
                        Object o3)

trn

public final String trn(String text,
                        String pluralText,
                        long n,
                        Object o1,
                        Object o2,
                        Object o3,
                        Object o4)

trn

public final String trn(String text,
                        String pluralText,
                        long n,
                        Object[] objects)
Returns the plural form for n of the translation of text.
Parameters:
text - the key string to be translated.
pluralText - the plural form of text.
n - value that determines the plural form
objects - object args to be formatted and substituted.
Returns:
the translated text