Appendix H. Localization

Localization is an undocumented Bash feature.

A localized shell script echoes its text output in the language defined as the system's locale. A Linux user in Berlin, Germany, would get script output in German, whereas his cousin in Berlin, Maryland, would get output from the same script in English.

To create a localized script, use the following template to write all messages to the user (error messages, prompts, etc.).

#!/bin/bash
# localized.sh
# Script by Stephane Chazelas, modified by Bruno Haible

. gettext.sh

E_CDERROR=65

error()
{
  printf "$@" >&2
  exit $E_CDERROR
}

cd $var || error "`eval_gettext \"Can't cd to \$var.\"`"
read -p "`gettext \"Enter the value: \"`" var
# ...

bash$ bash -D localized.sh
"Can't cd to %s."
 "Enter the value: "
This lists all the localized text. (The -D option lists double-quoted strings prefixed by a $, without executing the script.)

bash$ bash --dump-po-strings localized.sh
#: a:6
 msgid "Can't cd to %s."
 msgstr ""
 #: a:7
 msgid "Enter the value: "
 msgstr ""
The --dump-po-strings option to Bash resembles the -D option, but uses gettext "po" format.

Note

Bruno Haible points out:

Starting with gettext-0.12.2, xgettext -o - localized.sh is recommended instead of bash --dump-po-strings localized.sh, because xgettext . . .

1. understands the gettext and eval_gettext commands (whereas bash --dump-po-strings understands only its deprecated $"..." syntax)

2. can extract comments placed by the programmer, intended to be read by the translator.

This shell code is then not specific to Bash any more; it works the same way with Bash 1.x and other /bin/sh implementations.

Now, build a language.po file for each language that the script will be translated into, specifying the msgstr. As an example:

fr.po:
#: a:6
msgid "Can't cd to %s."
msgstr "Impossible de se positionner dans le répertoire %s."
#: a:7
msgid "Enter the value: "
msgstr "Entrez la valeur : "

Then, run msgfmt.

msgfmt -o localized.sh.mo fr.po

Place the resulting localized.sh.mo file in the /usr/local/share/locale/fr/LC_MESSAGES directory, and at the beginning of the script, insert the lines:
TEXTDOMAINDIR=/usr/local/share/locale
TEXTDOMAIN=localized.sh

If a user on a French system runs the script, she will get French messages.

Note

With older versions of Bash or other shells, localization requires gettext, using the -s option. In this case, the script becomes:

#!/bin/bash
# localized.sh

E_CDERROR=65

error() {
  local format=$1
  shift
  printf "$(gettext -s "$format")" "$@" >&2
  exit $E_CDERROR
}
cd $var || error "Can't cd to %s." "$var"
read -p "$(gettext -s "Enter the value: ")" var
# ...

The TEXTDOMAIN and TEXTDOMAINDIR variables need to be exported to the environment.

---

This appendix written by Stephane Chazelas, with modifications suggested by Bruno Haible, maintainer of GNU gettext.