- Bash Cookbook
- Ron Brash Ganesh Naik
- 676字
- 2021-07-23 19:17:38
How it works...
It goes without saying that translation can be a tricky beast, especially when managing encoding and when producing results that make sense at the human language level. Furthermore, even a slight change in the value within the script can break the PO file and the resulting script will not be fully translated (sometimes, not even at all).
Take care not to break the keys when making modifications to the scripts at a later date.
- Step one is fairly straightforward—you just create a script. If you run the script, you will see purely an English result, but at least the plural and non-plural output is correct. Notice . gettext.sh; this line preps gettext to prepare and to be ran for internationalization/localization. In the script, we also use gettext, eval_gettext, and eval_ngettext. These are functions that allow the translation to occur. Use gettext for simple translations, eval_gettext for a translation that contains a variable, and eval_ngettext when you have translations that contain plural objects. As you may have noticed, eval_ngettext is a bit more complex: $(eval_ngettext "I have \$COUNT electronic device" "I have \$COUNT electronic devices" $COUNT). The first parameter for eval_ngettext is the singular translation, the second is the plural, and the count is the variable used to determine if a singular or plural value is used. Variables are referred to in the original script with an escape \$COUNT, and the translated string that contains the variable will appear as $COUNT inside of the translation file without the escape:
./hellobonjour.sh
Hello
What is your name?
My name is rbrash
Do you have electronics?
I have 0 electronic devices
I have 1 electronic device
I have 2 electronic devices
- In step two, we create the language file called a PO file using xgettext. PO is short for Portable Object. Notice that we omitted the header because it will produce extra output. It is particularly useful when you want to write notes, versions, and even specify the encoding used.
- Instead of writing the translations from scratch, we used our trusty friend Google translate to produce a few basic translations and we copy them over the output from xgettext. Xgettext created almost the same file! Notice msgid, msgstr, msgplural, and msgstr[...]. Msgid and msgid_plural are used to match the original values as if they were a key. For example, as the script runs, gettext sees "I have $COUNT electronic device", and then knows to output a specific translation that matches that same msgid:
msgid "I have $COUNT electronic device"
msgid_plural "I have $COUNT electronic devices"
msgstr[0] ".."
- hellobonjour_fr.po contains all of our translations, and now we can use a command called msgfmt, which is used to produce a MO file or Machine Object. If you open this file with an editor like vi, you will notice that it contains a bunch of symbols representing binary and the strings. This file should not be edited, but rather the input PO file itself.
- Next, we create a file called translator.sh. It runs hellobonjour.sh and contains a few lines that set three important variables: TEXTDOMAIN, TEXTDOMAINDIR, and LANGUAGE. TEXTDOMAIN is typically the variable used to describe the binary or shell script (think of it as a namespace), and TEXTDOMAINDIR is the directory for gettext to look for the translation. Notice that it's in a local relative directory, and not /usr/share/locale (which it could be). Finally, we set LANGUAGE to fr for French.
- When we execute translator.sh, hellobonjour.sh is run twice and outputs once in English, and the second time in French:
$ bash translator.sh
Hello
What is your name?
My name is rbrash
Do you have electronics?
I have 0 electronic devices
I have 1 electronic device
I have 2 electronic devices
Bonjour
Comment t'appelles tu?
Mon nom est rbrash
Avez-vous des appareils electroniques?
J'ai 0 appareils electroniques
J'ai 1 appareil electronique
J'ai 2 appareils electroniques
Do not use the old format of $"my string" for translation. It is subject to security risks!
推薦閱讀