Add internationalization utility class #281

Merged
jwharm merged 1 commit from i18n into main 2025-10-11 22:04:17 +02:00
jwharm commented 2025-10-11 22:04:04 +02:00 (Migrated from github.com)

I've added a utility class to help translate Java-GI applications with GNU Gettext.

To use it, call Intl.bindtextdomain() and Intl.textdomain() in your main method to set the domain (for example, the name of the app) and the directory where the translations (locale-specific .mo files) are located. Then, pass all user-visible text messages to Intl.i18n(). This is all you need to do in your application source code.

To create the translation files, follow these steps:

  • Run xgettext --keyword=i18n on the Java source files. This will generate a .pot file.
  • Create locale-specific .po files from it with msginit.
    • The .po files are checked into version control, and will be edited by translators.
    • Whenever the .pot file changes later on, use msgmerge to update the .po files.
  • Finally, compile the .po files into binary .mo files with msgfmt. Make sure the .mo files are in the standard directory structure: locale/<lang>/LC_MESSAGES/<textdomain>.mo. This directory is passed to the textdomain() method mentioned above.

Note: Gettext can work with Java ResourceBundles instead of .mo files, but this is not supported by this utility class. The reason is that .mo files are required to translate strings in Gtk template class .ui files, and it doesn't make much sense to put some messages in ResourceBundles and others in .mo files.

I've added a utility class to help translate Java-GI applications with GNU Gettext. To use it, call `Intl.bindtextdomain()` and `Intl.textdomain()` in your main method to set the domain (for example, the name of the app) and the directory where the translations (locale-specific `.mo` files) are located. Then, pass all user-visible text messages to `Intl.i18n()`. This is all you need to do in your application source code. To create the translation files, follow these steps: * Run `xgettext --keyword=i18n` on the Java source files. This will generate a `.pot` file. * Create locale-specific `.po` files from it with `msginit`. * The `.po` files are checked into version control, and will be edited by translators. * Whenever the `.pot` file changes later on, use `msgmerge` to update the `.po` files. * Finally, compile the `.po` files into binary `.mo` files with `msgfmt`. Make sure the `.mo` files are in the standard directory structure: `locale/<lang>/LC_MESSAGES/<textdomain>.mo`. This directory is passed to the `textdomain()` method mentioned above. Note: Gettext can work with Java ResourceBundles instead of `.mo` files, but this is not supported by this utility class. The reason is that `.mo` files are required to translate strings in Gtk template class `.ui` files, and it doesn't make much sense to put some messages in ResourceBundles and others in `.mo` files.
Sign in to join this conversation.
No description provided.