- Bash Cookbook
- Ron Brash Ganesh Naik
- 377字
- 2021-07-23 19:17:38
How to do it...
Let's begin our activity as follows:
- 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
- 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
- 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?"
- 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
- 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
- Upon executing translator.sh, review the results for both executions of translator.sh:
$ bash translator.sh
推薦閱讀
- Vue 3移動Web開發與性能調優實戰
- Raspberry Pi for Python Programmers Cookbook(Second Edition)
- JavaScript 從入門到項目實踐(超值版)
- Scala Design Patterns
- Python機器學習經典實例
- Learning Salesforce Einstein
- Java:High-Performance Apps with Java 9
- Mastering Business Intelligence with MicroStrategy
- Java零基礎實戰
- 軟件供應鏈安全:源代碼缺陷實例剖析
- MySQL入門很輕松(微課超值版)
- Java程序設計與項目案例教程
- Extending Unity with Editor Scripting
- Appcelerator Titanium:Patterns and Best Practices
- Python程序設計教程