Once you’ve marked the strings in your script that you want to translate using $"...", you create a gettext "template" file using the command
bash --dump-po-strings scriptname > domain.pot
The domain is your message domain. It’s just an arbitrary string that’s used to identify the files gettext needs, like a package or script name. It needs to be unique among all the message domains on systems where you install the translations, so gettext knows which translations correspond to your script. You’ll use the template file to create translations for each target language. The template file conventionally has the suffix ‘.pot’.
You copy this template file to a separate file for each target language you want to support (called "PO" files, which use the suffix ‘.po’). PO files use various naming conventions, but when you are working to translate a template file into a particular language, you first copy the template file to a file whose name is the language you want to target, with the ‘.po’ suffix. For instance, the Spanish translations of your strings would be in a file named ‘es.po’, and to get started using a message domain named "example," you would run
cp example.pot es.po
Ultimately, PO files are often named domain.po and installed in directories that contain multiple translation files for a particular language.
Whichever naming convention you choose, you will need to translate the strings in the PO files into the appropriate languages. This has to be done manually.
When you have the translations and PO files complete, you’ll use the
gettext tools to produce what are called "MO" files, which are compiled
versions of the PO files the gettext tools use to look up translations
efficiently.
MO files are also called "message catalog" files.
You use the msgfmt
program to do this.
For instance, if you had a file with Spanish translations, you could run
msgfmt -o es.mo es.po
to produce the corresponding MO file.
Once you have the MO files, you decide where to install them and use the
TEXTDOMAINDIR
shell variable to tell the gettext tools where they are.
Make sure to use the same message domain to name the MO files
as you did for the PO files when you install them.
Your users will use the LANG
or LC_MESSAGES
shell variables to
select the desired language.
You set the TEXTDOMAIN
variable to the script’s message domain.
As above, you use the message domain to name your translation files.
You, or possibly your users, set the TEXTDOMAINDIR
variable to the
name of a directory where the message catalog files are stored.
If you install the message files into the system’s standard message catalog
directory, you don’t need to worry about this variable.
The directory where the message catalog files are stored varies between
systems.
Some use the message catalog selected by the LC_MESSAGES
shell variable.
Others create the name of the message catalog from the value of the
TEXTDOMAIN
shell variable, possibly adding the ‘.mo’ suffix.
If you use the TEXTDOMAIN
variable, you may need to set the
TEXTDOMAINDIR
variable to the location of the message catalog files,
as above.
It’s common to use both variables in this fashion:
$TEXTDOMAINDIR
/$LC_MESSAGES
/LC_MESSAGES/$TEXTDOMAIN
.mo.
If you used that last convention, and you wanted to store the message catalog files with Spanish (es) and Esperanto (eo) translations into a local directory you use for custom translation files, you could run
TEXTDOMAIN=example TEXTDOMAINDIR=/usr/local/share/locale cp es.mo ${TEXTDOMAINDIR}/es/LC_MESSAGES/${TEXTDOMAIN}.mo cp eo.mo ${TEXTDOMAINDIR}/eo/LC_MESSAGES/${TEXTDOMAIN}.mo
When all of this is done, and the message catalog files containing the
compiled translations are installed in the correct location,
your users will be able to see translated strings
in any of the supported languages by setting the LANG
or
LC_MESSAGES
environment variables before running your script.