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

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
主站蜘蛛池模板: 孟村| 腾冲县| 南皮县| 锦屏县| 大兴区| 东莞市| 额敏县| 开鲁县| 泌阳县| 奉贤区| 潍坊市| 大田县| 固镇县| 凤庆县| 新竹县| 金秀| 新营市| 紫阳县| 德格县| 涟水县| 明水县| 丹阳市| 英超| 澄城县| 金华市| 台南县| 宁南县| 平和县| 巴彦淖尔市| 德兴市| 浑源县| 巴塘县| 新民市| 平南县| 安康市| 湖州市| 剑阁县| 西城区| 蛟河市| 会宁县| 诸城市|