Class Intl

java.lang.Object
org.javagi.util.Intl

@NullMarked public class Intl extends Object
Utility class to translate text to the current locale using GNU Gettext.

To use this class, call bindtextdomain and textdomain at the start of your application, ideally at the top of your main() method, but in any case before Gtk is initialized. For every text message that is displayed to the users, call one of the i18n(String) methods to retrieve a translation in the current locale. When no translation is present, the original text is returned.

It is recommended to statically import the i18n(String) methods in all classes where it is used.

Before marking strings as internationalizable, uses of the string concatenation operator need to be converted to MessageFormat or String.format(String, Object...) syntax. For example, "file " + filename + " not found" becomes MessageFormat.format("file {0} not found", filename) or String.format("file %s not found", filename). Only after this is done, can the strings be marked and extracted.

This class requires GNU Gettext to be installed. Specifically, it will try to load the libgettextlib shared library. If that did not work, all methods will silently fallback to return the original (English) messages.

Gettext offers tools to extract a message catalogue from your application sources and create and compile per-language translation files. Consult the GNU Gettext documentation for details.

  • Constructor Details

    • Intl

      public Intl()
  • Method Details

    • bindtextdomain

      public static @Nullable String bindtextdomain(String domainname, String dirname)
      Set directory containing message catalogs for Gettext.
      Parameters:
      domainname - the message domain
      dirname - the base directory of the hierarchy containing message catalogs for domainname
      Returns:
      the current base directory for domain domainname, or null if an error occured.
    • textdomain

      public static @Nullable String textdomain(String domainname)
      Set or retrieve the current text domain for Gettext.
      Parameters:
      domainname - the message domain
      Returns:
      the message domain, or null if an error occured.
    • i18n

      public static String i18n(String msgid)
      Translate the string.
      Parameters:
      msgid - the string to translate
      Returns:
      the translation, or msgid if none is found
      See Also:
    • i18n

      public static String i18n(String msgid, String plural, int n)
      Translate a string that can be singular or plural.
      Parameters:
      msgid - the string to translate
      plural - the string to translate (plural form)
      n - determines whether singular or plural translation is chosen
      Returns:
      the translation, or msgid or plural if none is found
      See Also:
    • i18n

      public static String i18n(String context, String msgid)
      Translate a string with context.
      Parameters:
      context - context of the string to translate
      msgid - the string to translate
      Returns:
      the translation, or msgid if none is found
      See Also: