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

How to do it...

Let's begin our activity as follows:

  1. Open a new terminal and create a new script called hellobonjour.sh with the following contents:
#!/bin/bash
. gettext.sh
function i_have() {
local COUNT=$1
###i18n: Please leave $COUNT as is
echo -e "\n\t" $(eval_ngettext "I have \$COUNT electronic device" "I have \$COUNT electronic devices" $COUNT)

}

echo $(gettext "Hello")
echo

echo $(gettext "What is your name?")
echo

###i18n: Please leave $USER as is
echo -e "\t" $(eval_gettext "My name is \$USER" )
echo

echo $(gettext "Do you have electronics?")

i_have 0
i_have 1
i_have 2

  1. Run xgettext to generate the appropriate strings. We will not use the results, but this is how you can generate a minimalist PO file:
$ xgettext --add-comments='##i18n' -o hellobonjour_fr.po hellobonjour.sh --omit-header
  1. Copy the already compiled list of strings into the language PO file called hellobonjour_fr.po:
# Hellobonjour.sh
# Copyright (C) 2017 Ron Brash
# This file is distributed under the same license as the PACKAGE package.
# Ron Brash <ron.brash@gmail.com>, 2017
# Please ignore my terrible google translations;
# As always, some is better than none!
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: i18n@example.com\n"
"POT-Creation-Date: 2017-12-08 12:19-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: French Translator <fr@example.org>\n"
"Language: fr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"

#. ##i18n: Please leave $COUNT as is
#: hellobonjour.sh:6
#, sh-format
msgid "I have $COUNT electronic device"
msgid_plural "I have $COUNT electronic devices"
msgstr[0] "J'ai $COUNT appareil electronique"
msgstr[1] "J'ai $COUNT appareils electroniques"

#: hellobonjour.sh:10
msgid "Hello"
msgstr "Bonjour"

#: hellobonjour.sh:13
msgid "What is your name?"
msgstr "Comment t'appelles tu?"

#. ##i18n: Please leave $USER as is
#: hellobonjour.sh:17
#, sh-format
msgid "My name is $USER"
msgstr "Mon nom est $USER"

#: hellobonjour.sh:20
msgid "Do you have electronics?"
msgstr "Avez-vous des appareils electroniques?"
  1. Next, using msgfmt, compile the PO file into a binary language file with the .mo extension and place it in our arbitrary language folder:
$ rm -rf locale/fr/LC_MESSAGES
$ mkdir -p locale/fr/LC_MESSAGES
$ sudo msgfmt -o locale/fr/LC_MESSAGES/hellobonjour.mo hellobonjour_fr.po
  1. Once you have your language file in place, create the following script with the name of translator.sh:
#!/bin/bash
./hellobonjour.sh

export TEXTDOMAIN="hellobonjour"
export TEXTDOMAINDIR=`pwd`/locale

export LANGUAGE=fr
./hellobonjour.sh
  1. Upon executing translator.sh, review the results for both executions of translator.sh:
$ bash translator.sh
主站蜘蛛池模板: 阿拉尔市| 东丽区| 青岛市| 民勤县| 盐津县| 桂阳县| 绍兴县| 海城市| 峨眉山市| 金寨县| 任丘市| 射洪县| 武义县| 尼木县| 襄垣县| 镇平县| 交口县| 驻马店市| 巨鹿县| 上高县| 吉隆县| 吴江市| 梧州市| 天水市| 巍山| 洛阳市| 大邑县| 阿克苏市| 彩票| 肃宁县| 凌源市| 平定县| 磐石市| 辽中县| 寿阳县| 古交市| 疏勒县| 乡城县| 新宾| 綦江县| 莱西市|