Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.xnap.commons.i18n.I18n
public class I18n
extends java.lang.Object
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
Constructor Summary | |
| |
|
Method Summary | |
ResourceBundle |
|
static String |
|
boolean |
|
void |
|
void |
|
void |
|
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
String |
|
public I18n(ResourceBundle bundle)
Constructs an I18n object for a resource bundle.
- Parameters:
bundle
- must not benull
public I18n(String baseName, Locale locale, ClassLoader loader)
Constructs an I18n object by callingsetResources(String,Locale,ClassLoader)
.
public ResourceBundle getResources()
Returns the current resource bundle.
public static final String marktr(String text)
Markstext
to be translated, but doesn't return the translation buttext
itself.
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)
.
public void setResources(ResourceBundle newBundle)
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 tosetLocale(Locale)
won't have any effect.
public void setResources(String baseName, Locale locale, ClassLoader loader)
Tries to load a resource bundle usingResourceBundle.getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader)
.
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, sincetrn(String,String,long)
takes exactly these two forms as parameters.
- Parameters:
locale
- the locale
- See Also:
trc(String,String)
public final String tr(String text)
Returnstext
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
public final String tr(String text, Object o1)
Convenience method that invokestr(String,Object[])
.
public final String tr(String text, Object o1, Object o2)
Convenience method that invokestr(String,Object[])
.
public final String tr(String text, Object o1, Object o2, Object o3)
Convenience method that invokestr(String,Object[])
.
public final String tr(String text, Object o1, Object o2, Object o3, Object o4)
Convenience method that invokestr(String,Object[])
.
public final String tr(String text, Object[] objects)
Returnstext
translated into the currently selected language. Occurrences of {number} placeholders in text are replaced byobjects
. InvokesMessageFormat.format(java.lang.String, java.lang.Object[])
.
- Parameters:
text
- text to translateobjects
- arguments toMessageFormat.format()
- Returns:
- the translated text
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 fromgetResources().getLocale()
equals the setsource code locale
,tr(comment)
otherwise.
public final String trn(String text, String pluralText, long n)
Returns the plural form forn
of the translation oftext
.
- Parameters:
text
- the key string to be translated.pluralText
- the plural form oftext
.n
- value that determines the plural form
- Returns:
- the translated text
public final String trn(String text, String pluralText, long n, Object o1)
Overloaded method that invokestrn(String,String,long,Object[])
passingObject
arguments as an array.
public final String trn(String text, String pluralText, long n, Object o1, Object o2)
Overloaded method that invokestrn(String,String,long,Object[])
passingObject
arguments as an array.
public final String trn(String text, String pluralText, long n, Object o1, Object o2, Object o3)
Overloaded method that invokestrn(String,String,long,Object[])
passingObject
arguments as an array.
public final String trn(String text, String pluralText, long n, Object o1, Object o2, Object o3, Object o4)
Overloaded method that invokestrn(String,String,long,Object[])
passingObject
arguments as an array.
public final String trn(String text, String pluralText, long n, Object[] objects)
Returns the plural form forn
of the translation oftext
.
- Parameters:
text
- the key string to be translated.pluralText
- the plural form oftext
.n
- value that determines the plural formobjects
- object args to be formatted and substituted.
- Returns:
- the translated text