I was searching for the answer to this too. I found this question while looking for the answer. I was on the right track searching for how to use .po and .pot files in bash scripts.
It looks like the gettext program and friends can be used to do this.
I found this good blog post that explains it well. Although he is using this for echo
this can also be used for the strings for Zenity.
The basics are this. To work with gettext
you need to set two environment variables at the top of the script. Lines like these:
export TEXTDOMAIN=$(basename $0) # Name of this script
export TEXTDOMAINDIR==$(dirname "$(readlink -f "$0")")/locale # Location of this script
This will allow you to put your translations in folders next to your script the default location for TEXTDOMAINDIR if you do not set it is /usr/share/locale
.
and source the gettext.sh script in your script with
source gettext.sh
After that you can change your Zenity text strings that need translated by wrapping them with eval_gettext
in a subcommand.
For example:
zenity --info --title="Now opening programs!" --text="I will now open the starup programs for you. This will help you get the computer setup quicker. I will let you know when I am finished" --timeout=5
Becomes:
zenity --info --title="$(eval_gettext "Now opening programs!")" --text="$(eval_gettext "I will now open the starup programs for you. This will help you get the computer setup quicker. I will let you know when I am finished")" --timeout=5
So the syntax for use with Zenity text options, like --title
and --text
, looks like this:
"$(eval_gettext "Text string")"
We have the "Text string"
in double quotes.
This is inside a eval_gettext
subcommand: $(eval_gettext "Text string")
which is wrapped in double quotes so that Zenity gets the string returned from the subcommand in double quotes.
"$(eval_gettext "Text string")"
Next you need to create a translation template file (.pot) using xgettext.
xgettext -L Shell -o myscript.pot myscript
The .pot file that is generated can be given to your translator(s) who can use programs like Poedit to create .po files for their language. The translator(s) can then send you the .po file to be included in your project.
If you use Poedit will also create the .mo file for you when you save it. In the TEXTDOMAINDIR
above then you can create in the same folder where your script is the following folder structure:
locale/<LANG>/LC_MESSAGES/
Replace with the language code of the translation. I placed the .po file to translate in the LC_MESSAGES
folder, then saved it with Poedit to create the .mo file. The .po should be named the same as theTEXTDOMAIN
variable above plus .mo. If your script ends in .sh include this to. ie. For myscript.sh
the .mo file would be myscript.sh.mo
.
If you do not use Poedit you can also use msgfmt
to make the .mo file:
msgfmt -v myscript.sh.po -o myscript.sh.mo
To test your script with a language you can run it like so. ie. For the German language code de
LANGUAGE=de ./myscript.sh
File structure for myscript.sh, German (de), .po, .mo and folders:
- myscript.sh
- locale
- de
- LC_MESSAGES
- myscript.sh.mo
- myscript.sh.po