org.xnap.commons.i18n
Class I18n

java.lang.Object
  extended by org.xnap.commons.i18n.I18n

public class I18n
extends Object

Provides methods for internationalization.

To learn how message strings wrapped in one of the tr*() methods can be extracted and localized, see this tutorial.

Since:
0.9
Author:
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
 Locale getLocale()
          Returns the locale this instance was created with.
 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 bundle)
          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[] objects)
          Returns text translated into the currently selected language.
 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 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[] objects)
          Returns the plural form for n of the translation of text.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

I18n

public I18n(ResourceBundle bundle)
Constructs an I18n object for a resource bundle.

Parameters:
bundle - must not be null
Throws:
NullPointerException - if bundle is null
Since:
0.9

I18n

public I18n(String baseName,
            Locale locale,
            ClassLoader loader)
Constructs an I18n object by calling setResources(String, Locale, ClassLoader).

Throws:
MissingResourceException - if the resource bundle could not be loaded
NullPointerException - if one of the arguments is null
Since:
0.9
Method Detail

getResources

public ResourceBundle getResources()
Returns the current resource bundle.

Since:
0.9

getLocale

public Locale getLocale()
Returns the locale this instance was created with. This can be different from the locale of the resource bundle returned by getResources().

Returns:
the locale or null, if this instance was directly created from a resource bundle
Since:
0.9

setResources

public void setResources(ResourceBundle bundle)
Sets a resource bundle to be used for message translations.

If this is called, the possibly previously specified class loader and baseName are invalidated, since the bundle might be from a different context. Subsequent calls to setLocale(Locale) won't have any effect.

Since:
0.9

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).

Throws:
MissingResourceException - if the bundle could not be loaded
NullPointerException - if one of the arguments is null
Since:
0.9

marktr

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

Since:
0.9

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.

Returns:
false if there is not enough information for loading a new resource bundle, see setResources(ResourceBundle).
Throws:
MissingResourceException - if the resource bundle for locale could not be found
NullPointerException - if locale is null
Since:
0.9

setSourceCodeLocale

public void setSourceCodeLocale(Locale locale)
Sets the locale of the text in the source code.

Only languages that have one singular and one plural form can be used as source code locales, since trn(String, String, long) takes exactly these two forms as parameters.

Parameters:
locale - the locale
Throws:
NullPointerException - if locale is null
Since:
0.9
See Also:
trc(String, String)

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
Since:
0.9

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
Since:
0.9

tr

public final String tr(String text,
                       Object o1)
Convenience method that invokes tr(String, Object[]).

Since:
0.9

tr

public final String tr(String text,
                       Object o1,
                       Object o2)
Convenience method that invokes tr(String, Object[]).

Since:
0.9

tr

public final String tr(String text,
                       Object o1,
                       Object o2,
                       Object o3)
Convenience method that invokes tr(String, Object[]).

Since:
0.9

tr

public final String tr(String text,
                       Object o1,
                       Object o2,
                       Object o3,
                       Object o4)
Convenience method that invokes tr(String, Object[]).

Since:
0.9

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
Since:
0.9

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
Since:
0.9

trn

public final 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.

Since:
0.9

trn

public final 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.

Since:
0.9

trn

public final 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.

Since:
0.9

trn

public final 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.

Since:
0.9

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
Returns:
text if the locale of the underlying resource bundle equals the source code locale, the translation of comment otherwise.
Since:
0.9
See Also:
setSourceCodeLocale(Locale)


Copyright © 2004-2007 Gettext Commons Team. All Rights Reserved.