org.xnap.commons.i18n
public class I18n extends Object
To learn how message strings wrapped in one of the tr*()
methods can be extracted and localized, see this tutorial.
Since: 0.9
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 I18n.
|
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[] objects)
Returns text translated into the currently selected
language.
|
String | tr(String text, Object o1)
Convenience method that invokes (String, Object[]) .
|
String | tr(String text, Object o1, Object o2)
Convenience method that invokes (String, Object[]) .
|
String | tr(String text, Object o1, Object o2, Object o3)
Convenience method that invokes (String, Object[]) .
|
String | tr(String text, Object o1, Object o2, Object o3, Object o4)
Convenience method that invokes (String, Object[]) .
|
String | trc(String context, 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[] objects)
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
(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
(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
(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
(String, String, long, Object[]) passing Object
arguments as an array.
|
String | trnc(String context, String singularText, String pluralText, long n)
Returns the plural form for n of the translation of
text .
|
String | trnc(String context, String singularText, String pluralText, long n, Object[] objects)
Returns the plural form for n of the translation of
text .
|
String | trnc(String comment, String singularText, String pluralText, long n, Object obj)
Overloaded method that invokes
(String, String, String, long, Object[]) passing obj
arguments as an array.
|
String | trnc(String comment, String singularText, String pluralText, long n, Object obj1, Object obj2)
Overloaded method that invokes
(String, String, String, long, Object[]) passing obj1 and obj2
arguments as an array.
|
String | trnc(String comment, String singularText, String pluralText, long n, Object obj1, Object obj2, Object obj3)
Overloaded method that invokes
(String, String, String, long, Object[]) passing obj1 , obj2 and obj3
arguments as an array.
|
String | trnc(String comment, String singularText, String pluralText, long n, Object obj1, Object obj2, Object obj3, Object obj4)
Overloaded method that invokes
(String, String, String, long, Object[]) passing obj1 , obj2 , obj3 and obj4
arguments as an array.
|
Parameters: bundle
must not be null
Throws: NullPointerException
if bundle
is null
Since: 0.9
Throws: MissingResourceException
if the resource bundle could not be loaded NullPointerException
if one of the arguments is null
Since: 0.9
Returns: the locale or null, if this instance was directly created from a resource bundle
Since: 0.9
Since: 0.9
text
to be translated, but doesn't return the
translation but text
itself.
Since: 0.9
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.
Throws: MissingResourceException
if the resource bundle for locale
could not be
found NullPointerException
if locale
is null
Since: 0.9
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 won't have any effect.
Since: 0.9
Throws: MissingResourceException
if the bundle could not be loaded NullPointerException
if one of the arguments is null
Since: 0.9
Only languages that have one singular and one plural form can be used as source code locales, since I18n takes exactly these two forms as parameters.
Parameters: locale the locale
Throws: NullPointerException
if locale
is null
Since: 0.9
See Also: I18n
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
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
(String, Object[])
.
Since: 0.9
(String, Object[])
.
Since: 0.9
(String, Object[])
.
Since: 0.9
(String, Object[])
.
Since: 0.9
Parameters: context the context of the text to be translated text the ambiguous key message in the source locale
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
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
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
(String, String, long, Object[])
passing Object
arguments as an array.
Since: 0.9
(String, String, long, Object[])
passing Object
arguments as an array.
Since: 0.9
(String, String, long, Object[])
passing Object
arguments as an array.
Since: 0.9
(String, String, long, Object[])
passing Object
arguments as an array.
Since: 0.9
n
of the translation of
text
.
Parameters: context
the context of the message to disambiguate it when translating 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.5
n
of the translation of
text
.
Parameters: context
the context of the message to disambiguate it when translating 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
(String, String, String, long, Object[])
passing obj
arguments as an array.
Since: 0.9.5
(String, String, String, long, Object[])
passing obj1
and obj2
arguments as an array.
Since: 0.9.5
(String, String, String, long, Object[])
passing obj1
, obj2
and obj3
arguments as an array.
Since: 0.9.5
(String, String, String, long, Object[])
passing obj1
, obj2
, obj3
and obj4
arguments as an array.
Since: 0.9.5