官术网_书友最值得收藏!

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.
  1. 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
  1. 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.

 

  1. 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] ".."
  1. 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.
  2. 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 LANGUAGETEXTDOMAIN 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. 
  3. 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!
主站蜘蛛池模板: 青田县| 五原县| 隆德县| 民丰县| 长岛县| 宜川县| 抚顺县| 文昌市| 沽源县| 甘孜县| 健康| 内江市| 伊春市| 博爱县| 莎车县| 乐清市| 江津市| 景宁| 白沙| 咸丰县| 白银市| 涿州市| 邓州市| 井冈山市| 集贤县| 永修县| 如东县| 云南省| 富民县| 东兴市| 怀远县| 囊谦县| 永州市| 新巴尔虎右旗| 博野县| 新兴县| 屏山县| 清远市| 扎鲁特旗| 吉木萨尔县| 凭祥市|