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

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
主站蜘蛛池模板: 新龙县| 普宁市| 柘荣县| 江城| 柏乡县| 南通市| 五台县| 安图县| 漠河县| 武义县| 天台县| 敖汉旗| 合肥市| 方正县| 高邑县| 赤水市| 尉氏县| 河东区| 拉孜县| 河源市| 准格尔旗| 壶关县| 社旗县| 仲巴县| 军事| 梓潼县| 阿城市| 新兴县| 大方县| 玉田县| 五大连池市| 那坡县| 唐山市| 津南区| 长子县| 抚宁县| 孝感市| 临沧市| 乌鲁木齐县| 黄骅市| 富锦市|