Internationalize shell scripts

From Salix OS
Revision as of 22:40, 8 January 2010 by JRD (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Modification of the original script

(We shall take excerpts from the script keyboardconfig as an example.)

  • Add the following lines after the usual introductory comments but before your actual code starts:
 # Gettext internationalization
 export TEXTDOMAIN="name_of_the_program"
 export TEXTDOMAINDIR="destination_path_to_the_compiled_translations"
 . gettext.sh

Example:

  • Before modification:
 # Version 4.3 - 28/07/2009
 #  * replaced xorg.conf by fdi policy rule
 # Take a look at "Xdialog" and use it instead of "dialog" in case X is running
 if [[ "$DISPLAY" && "$(which Xdialog 2>&1 | grep -v "which: no")" ]]; then
  • After modification:
 # Version 4.3 - 28/07/2009
 #  * replaced xorg.conf by fdi policy rule
 # Gettext internationalization
 export TEXTDOMAIN="keyboardconfig"
 export TEXTDOMAINDIR="/usr/share/locale"
 . gettext.sh
 # Take a look at "Xdialog" and use it instead of "dialog" in case X is running
 if [[ "$DISPLAY" && "$(which Xdialog 2>&1 | grep -v "which: no")" ]]; then
  • Put the following tags around the strings that need to be translated:
 `eval_gettext 'string_to_be_translated'`

Exemple:

  • Before modification:
 if [ "$xflag" = "yes" ] ; then
 	answer="$(eval $dialog \
 	--stdout \
 	--title \"Keyboard configuration\" \
 	--default-item \"$currentpathkeymap\" \
 	--cancel-label \"Exit\" \
 	--icon \"keyboardconfig\" \
 	--check \"numlock\" \"on\" \
 	--menu \
 	\"\\n        Please select your prefered keyboard map:\" 20 75 11 "$list" )"	
 else
  • After Modification:
 if [ "$xflag" = "yes" ] ; then
 	answer="$(eval $dialog \
 	--stdout \
 	--title \"`eval_gettext 'Keyboard configuration'`\" \
 	--default-item \"$currentpathkeymap\" \
 	--cancel-label \"`eval_gettext 'Exit'`\" \
 	--icon \"keyboardconfig\" \
 	--check \"numlock\" \"on\" \
 	--menu \
 	\"\\n        `eval_gettext 'Please select your prefered keyboard map:'`\" 20 75 11 "$list" )"
 else

Translation process

  • We first need to extract the strings to be translated from the shell script to a model file that will be distributed to the translators. To do this, run the following command from the working directory where the shell script is located:
 xgettext --from-code=utf-8 -L shell -o name_of_shell_script.pot name_of_shell_script

Example:

 xgettext --from-code=utf-8 -L shell -o keyboardconfig.pot keyboardconfig 
  • Each translator will use the model file to generate his own translation work file by running the following command from the directory where the .pot file (our translation model) is located. It is assumed that the environment locale of his operating system is the same as the locale he wants to translate into (or else he should consult the man page of msginit):
 msginit -i name_of_shell_script.pot -o name_of_shell_script-name_of_locale.po

Example:

 msginit -i keyboardconfig.pot -o keyboardconfig-fr.po
  • Looking into the file is self explanatory, using a text editor of his choice each translator translate the strings in his language po file.

Example...

  • Before:
 #: keyboardconfig:93 keyboardconfig:103
 #, sh-format
 msgid "Keyboard configuration"
 msgstr ""
  • After:
 #: keyboardconfig:93 keyboardconfig:103
 #, sh-format
 msgid "Keyboard configuration"
 msgstr "Configuration du clavier"

Extending the translations to the shell script

  • When the maintainer of the shell script receives back a completed **po** file from a translator, he first compiles it by running the following command (from the working directory where the translated po file is located):
 msgfmt name_of_shell_script-name_of_locale.po -o name_of_shell_script.mo

Example:

 msgfmt keyboardconfig-fr.po -o keyboardconfig.mo
  • Then he places the **mo** file he generated into its appropriate locale directory:
 /usr/share/locale/name_of_locale/LC_MESSAGES/

Example:

 /usr/share/locale/fr/LC_MESSAGES/

That's all!

Notes

Maintenance of the shell script & future translations:

If later on, the shell script is modified, its maintainer will issue a new **pot** (model) file which he can send back to the translators.

Each translator will then have to run the following command in order to generate a new **po** file for their language that will include their previous translation work as well as the new strings net yet translated. Both the new **pot** file & the last translated **po** file should be located in the directory where this command is issued.

 msgmerge -U name_of_shell_script-name_of_locale.po name_of_shell_script.pot

Exemple:

 msgmerge -U keyboardconfig-fr.po keyboardconfig.pot

And then redo points in end of §2 and §3.