java.lang.Object
net.i2p.util.Translate
Direct Known Subclasses:
Messages

public abstract class Translateextends Object
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 Details

    • PROP_LANG

      public static final String PROP_LANG
      Property for the console language.
      See Also:
    • PROP_COUNTRY

      public static final String PROP_COUNTRY
      Property for the console country, two-letter upper case or "".
      See Also:
    • _localeLang

      private static final String _localeLang
      Non-null, two- or three-letter lower case, may be "".
    • _localeCountry

      private static final String _localeCountry
      Non-null, two-letter upper case, may be "".
    • _bundles

      private static final Map<String, ResourceBundle> _bundles
      Bundles already loaded, by name.
    • _missing

      private static final Set<String> _missing
      Bundle names found to be absent.
    • TEST_LANG

      private static final String TEST_LANG
      Use to look for untagged strings.
      See Also:
    • TEST_STRING

      private static final String 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

      public static String getString(String key, I2PAppContext ctx, String bun)
      Look up a single translation key in a resource bundle.
      Parameters:
      key - the message key in the bundle
      ctx - the application context, which supplies the language and country
      bun - base name of the resource bundle to translate in, e.g. "routerconsole"
      Returns:
      the translated string, or key itself if no translation exists
    • getString

      public static String getString(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.
      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 country
      bun - 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

      public static String getString(String s, Object o, Object o2, I2PAppContext ctx, String bun)
      For {0} and {1}.
      Parameters:
      s - the string to be translated, containing {0} and {1} placeholders
      o - the value substituted for {0}, not translated
      o2 - the value substituted for {1}, not translated
      ctx - the application context, which supplies the language and country
      bun - 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

      public static String getString(String s, I2PAppContext ctx, String bun, Object... oArray)
      Varargs form, accepting any number of values substituted for the {0}, {1}, ... placeholders.
      Parameters:
      s - the string to be translated, containing {0}, {1}, ... placeholders
      ctx - the application context, which supplies the language and country
      bun - 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

      public static String getString(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-forms
      Parameters:
      n - item count; 1 selects the singular, anything else the plural
      s - 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 country
      bun - 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

      public static String getLanguage(I2PAppContext ctx)
      Two- or three-letter lower case.
      Parameters:
      ctx - the application context holding the routerconsole.lang property
      Returns:
      lang in routerconsole.lang property, else current locale
    • isRTL

      public static boolean isRTL(I2PAppContext ctx)
      Are we configured for a right-to-left language?
      Parameters:
      ctx - the application context holding the routerconsole.lang property
      Returns:
      true for supported RTL languages
    • isRTL

      public static boolean isRTL(String lang)
      Is this a right-to-left language?
      Parameters:
      lang - Two- or three-letter lower case
      Returns:
      true for supported RTL languages
    • getCountry

      public static String getCountry(I2PAppContext ctx)
      Two-letter upper case or "".
      Parameters:
      ctx - the application context holding the routerconsole.country property
      Returns:
      country in routerconsole.country property, else current locale
    • setLanguage

      public static void setLanguage(String lang, String country)
      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 default
      country - Two-letter upper case, or null for default, or "" for none
    • findBundle

      private static ResourceBundle findBundle(String bun, String lang, String country)
      Cache both found and not found for speed.
      Parameters:
      bun - base name of the resource bundle, e.g. "routerconsole"
      lang - non-null, if "" returns null
      country - 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 JVM Locale.getDisplayLanguage() if available, else default param.
      Parameters:
      langCode - two- or three-letter lower-case
      dflt - name returned untranslated when no bundle entry or JVM display name is available
      ctx - the application context, which supplies the current language
      bun - 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.