How to use gettext in bash script?
I only found this page, but I don't understand it.
Localization
My script is written like this:
#!/bin/bash
. lang_file.sh
echo $LANG_HELLO_WORLD
And lang_file.sh look like that:
#!/bin/bash
LANG_HELLO_WORLD="Hello World"
I want to change lang_file.sh to something using gettext, like this:
#!/bin/bash
LANG_HELLO_WORLD=`some gettext command to get string in user language`
I want to use the code in Launchpad, so other users can translate it (.po, .pot files)
Sorry for bad English, any suggestions?
You need to preform following steps:
Mark the strings to be translated. Following snippet gives example:
Let's call it PRJ.sh
:
#!/bin/sh
alias GETTEXT='gettext "PRJ"'
## Use GETTEXT to mark the string you want to translate
HELLO_WORLD=$(GETTEXT "Hello world")
echo "$HELLO_WORLD"
Produce .pot file so translators can work on it.
Run the following command, it only looks for GETTEXT, the ones you actually want to translate.
xgettext -o PRJ.pot -L Shell --keyword --keyword=GETTEXT PRJ.sh
Optional: Generate .po files.
For each locale you want to cover.
msginit -i PRJ.pot -l fr.UTF-8
Note that "UTF-8" is sugggested, otherwise msginit
may mistakenly choose some obsoleted encoding for you.
Retrieve completed .po files, and convert them to .mo file (the file that machine can read it)
msgfmt -v fr.po -o fr.mo
Install .mo files. Run:
sudo install fr.mo /usr/share/locale/fr/LC_MESSAGES/PRJ.mo
Now you can try the result:
LANGUAGE=fr ./PRJ.sh
and you should see French translation for Hello world.
There is a long-lost, never-documented and almost-deprecated builtin solution in bash.
LANG=foo_BAR.utf8
TEXTDOMAIN="test"
TEXTDOMAINDIR="/usr/share/locale"
echo $"fooMsgid"
# bash --dump-po-strings <scriptfile>
The gettext translation make use of an editable format *.po to store translation, and a compiled format *.mo for loading.
For info of the files format, reference here: section "3 The Format of PO Files" and "10 Producing Binary MO Files" of https://www.gnu.org/software/gettext/manual/html_node/index.html
Here I focus on how to try the gettext
command to get a translation in short.
After you prepared a folder /path/to/your/locale with inside-hierarchy like <lang>/LC_MESSAGES/<textdomain>.mo
(where <lang>
is e.g. ko_KR
for Korean), use the following code in your lang_file.sh
:
#!/bin/bash
export LC_ALL=ko_KR.UTF-8 # if LC_ALL not work, you could try also "LANG" and "LANGUAGE"
export TEXTDOMAINDIR=/path/to/your/locale
# export TEXTDOMAIN="<textdomain>" # <- optional, set this to save the "<textdomain>" argument for `gettext` below
LANG_HELLO_WORLD="$( gettext "<textdomain>" "Your message to translate" )"
What you are looking to do is I think ask the user in the appropriate language? You would probably want the user to pick the language first. The other part of what you are asking is simply just embedding commands like $(get_some_str_func) inside of your variable.
I did not write this code but it might be along the lines of what you are trying to do? I'm not sure, i don't understand fully what you are asking.
function _configure_locale() { # [profile]
local profile=${1:-EN}
case ${profile} in
DE|DE_DE|de_DE)
LC_ALL="de_DE.UTF-8"
LANG="de_DE.UTF-8"
LANGUAGE="de_DE:de:en_US:en"
;;
EN|EN_US|en|en_US)
LC_ALL="en_US.UTF-8"
LANG="en_US.UTF-8"
LANGUAGE="en_US:en"
;;
*)
echo "ALERT" "${FUNCNAME}: unknown profile '${profile}'"
;;
esac
LC_PAPER="de_DE.UTF-8"; # independent from locale
LESSCHARSET="utf-8"; # independent from locale
MM_CHARSET="utf-8" # independent from locale
echo "locale settings" "${LANG}";
export LC_ALL LANG LANGUAGE LC_PAPER LESSCHARSET MM_CHARSET
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With