Skip to content

Instantly share code, notes, and snippets.

@gbroques
Last active February 13, 2022 03:26
Show Gist options
  • Save gbroques/cdfefd626637bde0ea8cebc5b33e3129 to your computer and use it in GitHub Desktop.
Save gbroques/cdfefd626637bde0ea8cebc5b33e3129 to your computer and use it in GitHub Desktop.
Translating Words on Ubuntu

gettext

A common way for people to boost their language skills is to set the locale of their computer to the target language they want to learn.

Using programs when you don't know many words can be a frustrating experience unless you can easily look up the translation when needed.

The following is a guide for how to do this on Linux based operating systems like Ubuntu.

Where are translations stored?

The following article offers a good overview for how internationalization works on Ubuntu works for many open source programs.

The key points from that article are:

  • many programs use another program called gettext to look up translations
  • these translations are stored in .mo files, which are binary and meant for machines
  • .po files are human-readable, created by translators, and then processed into .mo files for use by programs.

Performing Translation: An Example With GIMP

Consider the following example for translating "Image", one of the options in the menu bar for GIMP, into Greek.

We can use the same gettext program that GIMP uses to perform the translation.

$ gettext --help ⏎
gettext [OPTION] [[TEXTDOMAIN] MSGID]
...

We need the following three pieces of information:

  1. The text domain (TEXTDOMAN)
  2. and the message id (MSGID)
  3. the language we want to translate to (i.e. Greek)
    1. This will be passed as a LANGUAGE environment variable which gettext reads.

Text Domain

First, for the text domain, find GIMP translation files by running the following command:

$ sudo find / -type f -name '*gimp*.mo' 2>/dev/null ⏎
...
/snap/gimp/380/usr/share/locale/el/LC_MESSAGES/gimp20.mo
...

Note: 2>/dev/null is a common way to filter out errors like "Permission Denied".

This will output the translation files for all the various languages like Greek (el).

From the article mentioned at the beginning, the text domain is the name of the .mo file (i.e. gimp20).

Message ID

The message ID, by convention, is simply the word or phrase in English. In this example, Image.

Target Language

To acquire the target language, list the various locales installed on your machine:

$ locale -a ⏎
el_GR.utf8
en_US.utf8

el_GR.utf8 is Greek, while en_US.utf8 is the US dialect of English.

For more information, see this article.

Translating Image

Finally, combine all 3 elements in the final command to translate "Image" into Greek the same way GIMP would.

$ LANGUAGE=el_GR.utf8 gettext gimp20 Image ⏎
Εικόνα

Reverse Translation

The reverse process, translating from Greek into English, is not as straight-forward.

It requires converting the .mo file back into a .po file using a program called msgunfmt.

First, ensure the msgunfmt program is installed.

If you see "command not found", then see this website and execute the command appropriate for your operating system.

For Ubuntu, you may need to run:

sudo apt-get install gettext

Using the msgunfmt program is simple. Just pass the name of the .mo file as the first and only argument.

$ msgunfmt /snap/gimp/380/usr/share/locale/el/LC_MESSAGES/gimp20.mo ⏎

This will output the translations in a human-readable format, like it would exist as a .po file.

Then we can pipe the output into grep with the Greek word we want to look up (i.e. Εικόνα).

$ msgunfmt /snap/gimp/380/usr/share/locale/el/LC_MESSAGES/gimp20.mo | grep -B 1 "Εικόνα" ⏎
...
msgid "Image"
msgstr "Εικόνα"
...

Note: -B 1 is to display the previous line (e.g. 'msgid "Image"').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment