java.lang.Object
net.i2p.util.Translate
- Direct Known Subclasses:
Messages
Translate strings efficiently. We don't include an English or default ResourceBundle, we simply check for "en" and return the original string. Support real-time language changing with the
routerconsole.lang and routerconsole.country properties.To change language in router context, set the context properties PROP_LANG and PROP_COUNTRY. To change language in app context, set the System properties PROP_LANG and PROP_COUNTRY.
Field Summary
Modifier and TypeFieldDescriptionprivate static final Map<String, ResourceBundle> Bundles already loaded, by name.private static final StringNon-null, two-letter upper case, may be "".private static final StringNon-null, two- or three-letter lower case, may be "".Bundle names found to be absent.static final StringProperty for the console country, two-letter upper case or "".static final StringProperty for the console language.private static final StringUse to look for untagged strings.private static final StringProbe string used to pick a locale.Constructor Summary
ConstructorsConstructorDescriptionConstructor for subclasses; the translation methods in this base class are static.Method Summary
Modifier and TypeMethodDescriptionstatic voidClear the cache.private static ResourceBundlefindBundle(String bun, String lang, String country) Cache both found and not found for speed.static StringgetCountry(I2PAppContext ctx) Two-letter upper case or "".static StringgetDisplayLanguage(String langCode, String dflt, I2PAppContext ctx, String bun) Return the "display language", e.g.static StringgetLanguage(I2PAppContext ctx) Two- or three-letter lower case.static StringgetString(int n, String s, String p, I2PAppContext ctx, String bun) Use GNU ngettext For .po file format see http://www.gnu.org/software/gettext/manual/gettext.html.gz#Translating-plural-formsstatic StringFor {0} and {1}.static StringgetString(String s, Object o, I2PAppContext ctx, String bun) Translate a string with a parameter This is a lot more expensive than getString(s, ctx), so use sparingly.static StringgetString(String key, I2PAppContext ctx, String bun) Look up a single translation key in a resource bundle.static StringgetString(String s, I2PAppContext ctx, String bun, Object... oArray) Varargs form, accepting any number of values substituted for the {0}, {1}, ...static booleanIs this a right-to-left language?static booleanisRTL(I2PAppContext ctx) Are we configured for a right-to-left language?static voidsetLanguage(String lang, String country) Only for use by standalone apps in App Context.
Field Details
PROP_LANG
Property for the console language.- See Also:
PROP_COUNTRY
Property for the console country, two-letter upper case or "".- See Also:
_localeLang
Non-null, two- or three-letter lower case, may be ""._localeCountry
Non-null, two-letter upper case, may be ""._bundles
Bundles already loaded, by name._missing
TEST_LANG
Use to look for untagged strings.- See Also:
TEST_STRING
Probe string used to pick a locale.- See Also:
Constructor Details
Translate
public Translate()Constructor for subclasses; the translation methods in this base class are static.
Method Details
getString
Look up a single translation key in a resource bundle.- Parameters:
key- the message key in the bundlectx- the application context, which supplies the language and countrybun- base name of the resource bundle to translate in, e.g. "routerconsole"- Returns:
- the translated string, or key itself if no translation exists
getString
Translate a string with a parameter This is a lot more expensive than getString(s, ctx), so use sparingly.- Parameters:
s- string to be translated containing {0} The {0} will be replaced by the parameter. Single quotes must be doubled, i.e. ' -> '' in the string.o- parameter, not translated. To translate parameter also, use _t("foo {0} bar", _t("baz")) Do not double the single quotes in the parameter. Use autoboxing to call with ints, longs, floats, etc.ctx- the application context, which supplies the language and countrybun- base name of the resource bundle to translate in, e.g. "routerconsole"- Returns:
- the translated string with {0} replaced by o, or s itself if no translation exists
getString
For {0} and {1}.- Parameters:
s- the string to be translated, containing {0} and {1} placeholderso- the value substituted for {0}, not translatedo2- the value substituted for {1}, not translatedctx- the application context, which supplies the language and countrybun- base name of the resource bundle to translate in, e.g. "routerconsole"- Returns:
- the translated string with {0} and {1} replaced, or s itself if no translation exists
getString
Varargs form, accepting any number of values substituted for the {0}, {1}, ... placeholders.- Parameters:
s- the string to be translated, containing {0}, {1}, ... placeholdersctx- the application context, which supplies the language and countrybun- base name of the resource bundle to translate in, e.g. "routerconsole"oArray- the values substituted for the placeholders, in order- Returns:
- the translated string with the placeholders replaced, or s itself if no translation exists
getString
Use GNU ngettext For .po file format see http://www.gnu.org/software/gettext/manual/gettext.html.gz#Translating-plural-forms- Parameters:
n- item count; 1 selects the singular, anything else the plurals- singluar string, optionally with {0} e.g. "one tunnel"p- plural string optionally with {0} e.g. "{0} tunnels"ctx- the application context, which supplies the language and countrybun- base name of the resource bundle to translate in, e.g. "routerconsole"- Returns:
- the singular or plural form as selected by n, with {0} replaced by n
getLanguage
Two- or three-letter lower case.- Parameters:
ctx- the application context holding therouterconsole.langproperty- Returns:
- lang in
routerconsole.langproperty, else current locale
isRTL
Are we configured for a right-to-left language?- Parameters:
ctx- the application context holding therouterconsole.langproperty- Returns:
- true for supported RTL languages
isRTL
Is this a right-to-left language?- Parameters:
lang- Two- or three-letter lower case- Returns:
- true for supported RTL languages
getCountry
Two-letter upper case or "".- Parameters:
ctx- the application context holding therouterconsole.countryproperty- Returns:
- country in
routerconsole.countryproperty, else current locale
setLanguage
Only for use by standalone apps in App Context. NOT for use in Router Context. Does not persist, apps must implement their own persistence. Does NOT override context properties.- Parameters:
lang- Two- or three-letter lower case, or null for defaultcountry- Two-letter upper case, or null for default, or "" for none
findBundle
Cache both found and not found for speed.- Parameters:
bun- base name of the resource bundle, e.g. "routerconsole"lang- non-null, if "" returns nullcountry- non-null, may be ""- Returns:
- null if not found
getDisplayLanguage
public static String getDisplayLanguage(String langCode, String dflt, I2PAppContext ctx, String bun) Return the "display language", e.g. "English" for the language specified by langCode, using the current language. Uses translation if available, then JVMLocale.getDisplayLanguage()if available, else default param.- Parameters:
langCode- two- or three-letter lower-casedflt- name returned untranslated when no bundle entry or JVM display name is availablectx- the application context, which supplies the current languagebun- base name of the resource bundle to translate in, e.g. "routerconsole"- Returns:
- the display language
clearCache
public static void clearCache()Clear the cache. Call this after adding new bundles to the classpath.