|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.xnap.commons.i18n.I18n
public class I18n
Provides methods for internationalization.
To learn how message strings wrapped in one of the tr*()
methods can be extracted and localized, see this tutorial.
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 |
---|
public I18n(ResourceBundle bundle)
bundle
- must not be null
NullPointerException
- if bundle
is nullpublic I18n(String baseName, Locale locale, ClassLoader loader)
setResources(String, Locale,
ClassLoader)
.
MissingResourceException
- if the resource bundle could not be loaded
NullPointerException
- if one of the arguments is null
Method Detail |
---|
public ResourceBundle getResources()
public Locale getLocale()
getResources()
.
public void setResources(ResourceBundle bundle)
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.
public void setResources(String baseName, Locale locale, ClassLoader loader)
ResourceBundle.getBundle(java.lang.String, java.util.Locale,
java.lang.ClassLoader)
.
MissingResourceException
- if the bundle could not be loaded
NullPointerException
- if one of the arguments is null
public static final String marktr(String text)
text
to be translated, but doesn't return the
translation but text
itself.
public boolean setLocale(Locale 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(ResourceBundle)
.
MissingResourceException
- if the resource bundle for locale
could not be
found
NullPointerException
- if locale
is nullpublic void setSourceCodeLocale(Locale locale)
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.
locale
- the locale
NullPointerException
- if locale
is null
trc(String, String)
public final String tr(String text)
text
translated into the currently selected
language. Every user-visible string in the program must be wrapped into
this function.
text
- text to translate
public final String tr(String text, Object[] objects)
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[])
.
text
- text to translateobjects
- arguments to MessageFormat.format()
public final String tr(String text, Object o1)
tr(String, Object[])
.
public final String tr(String text, Object o1, Object o2)
tr(String, Object[])
.
public final String tr(String text, Object o1, Object o2, Object o3)
tr(String, Object[])
.
public final String tr(String text, Object o1, Object o2, Object o3, Object o4)
tr(String, Object[])
.
public final String trn(String text, String pluralText, long n)
n
of the translation of
text
.
text
- the key string to be translated.pluralText
- the plural form of text
.n
- value that determines the plural form
public final String trn(String text, String pluralText, long n, Object[] objects)
n
of the translation of
text
.
text
- the key string to be translated.pluralText
- the plural form of text
.n
- value that determines the plural formobjects
- object args to be formatted and substituted.
public final String trn(String text, String pluralText, long n, Object o1)
trn(String, String, long, Object[])
passing Object
arguments as an array.
public final String trn(String text, String pluralText, long n, Object o1, Object o2)
trn(String, String, long, Object[])
passing Object
arguments as an array.
public final String trn(String text, String pluralText, long n, Object o1, Object o2, Object o3)
trn(String, String, long, Object[])
passing Object
arguments as an array.
public final String trn(String text, String pluralText, long n, Object o1, Object o2, Object o3, Object o4)
trn(String, String, long, Object[])
passing Object
arguments as an array.
public final String trc(String comment, String text)
comment
- the text translated + a disambiguation hint in brackets.text
- the ambiguous key string
text
if the locale of the underlying resource
bundle equals the source code locale, the translation of
comment
otherwise.setSourceCodeLocale(Locale)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |