Difference between revisions of "Internationalize shell scripts"

From Salix OS
Jump to: navigation, search
(Notes)
Line 59: Line 59:
 
   xgettext --from-code=utf-8 -L shell -o keyboardconfig.pot keyboardconfig  
 
   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):
+
* 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
 
   msginit -i name_of_shell_script.pot -o name_of_shell_script-name_of_locale.po
 
Example:
 
Example:
 
   msginit -i keyboardconfig.pot -o keyboardconfig-fr.po
 
   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.
+
* Looking into the file is self explanatory, using a text editor of his choice each translator translates the strings in his language '''.po''' file.
 
Example...
 
Example...
 
:*Before:
 
:*Before:
Line 70: Line 70:
 
   #, sh-format
 
   #, sh-format
 
   msgid "Keyboard configuration"
 
   msgid "Keyboard configuration"
   msgstr ""
+
   msgstr '''
 
:* After:
 
:* After:
 
   #: keyboardconfig:93 keyboardconfig:103
 
   #: keyboardconfig:93 keyboardconfig:103
Line 79: Line 79:
 
== Extending the translations to the shell script ==
 
== 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):
+
* 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
 
   msgfmt name_of_shell_script-name_of_locale.po -o name_of_shell_script.mo
 
Example:
 
Example:
 
   msgfmt keyboardconfig-fr.po -o keyboardconfig.mo
 
   msgfmt keyboardconfig-fr.po -o keyboardconfig.mo
  
* Then he places the **mo** file he generated into its appropriate locale directory:
+
* Then he places the '''.mo''' file he generated into its appropriate locale directory:
 
   /usr/share/locale/name_of_locale/LC_MESSAGES/
 
   /usr/share/locale/name_of_locale/LC_MESSAGES/
 
Example:
 
Example:
Line 95: Line 95:
 
Maintenance of the shell script & future translations:
 
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.
+
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.
+
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
 
   msgmerge -U name_of_shell_script-name_of_locale.po name_of_shell_script.pot
 
Exemple:
 
Exemple:
Line 106: Line 106:
 
== Translate using PoEdit ==
 
== Translate using PoEdit ==
  
The easiest way to localize a gettext software is to install **poedit** from the repositories.
+
The easiest way to localize a gettext software is to install '''poedit''' from the repositories.
  
If there is already a **.po** file for your language and you just want to update/correct it, you can open the **.po** file directly with poedit.
+
If there is already a '''.po''' file for your language and you just want to update/correct it, you can open the '''.po''' file directly with poedit.
  
If a **.po** file for your language doesn't exist yet, you can create one easily with poedit, just select **"New catalog from .POT file"** from the file menu, add your details and start translating!
+
If a '''.po''' file for your language doesn't exist yet, you can create one easily with poedit, just select '''"New catalog from .POT file'''" from the file menu, add your details and start translating!
  
Remember to save the **.po** file you created like this:
+
Remember to save the '''.po''' file you created like this:
 
   softname-langcode.po
 
   softname-langcode.po
 
Example:
 
Example:
Line 119: Line 119:
 
== Translate without PoEdit ==
 
== Translate without PoEdit ==
  
If there is no **.po** file for your language yet, you can create one from the **.pot** file with a command like this:
+
If there is no '''.po''' file for your language yet, you can create one from the '''.pot''' file with a command like this:
 
   msginit -i name_of_shell_script.pot -o name_of_shell_script-name_of_locale.po
 
   msginit -i name_of_shell_script.pot -o name_of_shell_script-name_of_locale.po
 
Example:
 
Example:
 
   msginit -i keyboardsetup.pot -o keyboardsetup-fr.po
 
   msginit -i keyboardsetup.pot -o keyboardsetup-fr.po
  
When you create a **.po** file, or if one is already there, you can start editing it directly. Looking into the file is self explanatory, using a text editor of his choice each translator translate the strings in his language po file.
+
When you create a '''.po''' file, or if one is already there, you can start editing it directly. 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:
 
Example:
Line 131: Line 131:
 
   #, sh-format
 
   #, sh-format
 
   msgid "Keyboard configuration"
 
   msgid "Keyboard configuration"
   msgstr ""
+
   msgstr '''
 
* After:
 
* After:
 
   #: keyboardsetup:93 keyboardsetup:103
 
   #: keyboardsetup:93 keyboardsetup:103
Line 141: Line 141:
 
== Testing your translations ==
 
== Testing your translations ==
  
If you want to test your translation, you need to create a **.mo** file from the **.po** file, like this:
+
If you want to test your translation, you need to create a '''.mo''' file from the '''.po''' file, like this:
 
   msgfmt name_of_shell_script-name_of_locale.po -o name_of_shell_script.mo
 
   msgfmt name_of_shell_script-name_of_locale.po -o name_of_shell_script.mo
 
Example:
 
Example:
 
   msgfmt keyboardsetup-fr.po -o keyboardsetup.mo
 
   msgfmt keyboardsetup-fr.po -o keyboardsetup.mo
  
The **.mo** filename should be exactly the same as the tool you are translating.
+
The '''.mo''' filename should be exactly the same as the tool you are translating.
  
You can then place the **.mo** file you generated into its appropriate locale directory:
+
You can then place the '''.mo''' file you generated into its appropriate locale directory:
 
   /usr/share/locale/name_of_locale/LC_MESSAGES/
 
   /usr/share/locale/name_of_locale/LC_MESSAGES/
 
Example:
 
Example:
Line 155: Line 155:
 
== Sending your translations ==
 
== Sending your translations ==
  
You can send your **.po** files to the salix-main mailing list (or the software author email) and they will be included in future versions of salixtools or other software. If it's a salixtools, just send an email to [mailto:salix-main@lists.sourceforge.net] attaching your **.po** files.
+
You can send your '''.po''' files to the salix-main mailing list (or the software author email) and they will be included in future versions of salixtools or other software. If it's a salixtools, just send an email to [mailto:salix-main@lists.sourceforge.net] attaching your '''.po''' files.
  
 
[[Category:Developer Documentation]]
 
[[Category:Developer Documentation]]

Revision as of 22:53, 8 January 2010

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 translates 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!

Maintenance

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.

Translate using PoEdit

The easiest way to localize a gettext software is to install poedit from the repositories.

If there is already a .po file for your language and you just want to update/correct it, you can open the .po file directly with poedit.

If a .po file for your language doesn't exist yet, you can create one easily with poedit, just select "New catalog from .POT file" from the file menu, add your details and start translating!

Remember to save the .po file you created like this:

 softname-langcode.po

Example:

 keyboardsetup-de.po (for German)

Translate without PoEdit

If there is no .po file for your language yet, you can create one from the .pot file with a command like this:

 msginit -i name_of_shell_script.pot -o name_of_shell_script-name_of_locale.po

Example:

 msginit -i keyboardsetup.pot -o keyboardsetup-fr.po

When you create a .po file, or if one is already there, you can start editing it directly. 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:
 #: keyboardsetup:93 keyboardsetup:103
 #, sh-format
 msgid "Keyboard configuration"
 msgstr 
  • After:
 #: keyboardsetup:93 keyboardsetup:103
 #, sh-format
 msgid "Keyboard configuration"
 msgstr "Configuration du clavier"


Testing your translations

If you want to test your translation, you need to create a .mo file from the .po file, like this:

 msgfmt name_of_shell_script-name_of_locale.po -o name_of_shell_script.mo

Example:

 msgfmt keyboardsetup-fr.po -o keyboardsetup.mo

The .mo filename should be exactly the same as the tool you are translating.

You can then place the .mo file you generated into its appropriate locale directory:

 /usr/share/locale/name_of_locale/LC_MESSAGES/

Example:

 /usr/share/locale/fr/LC_MESSAGES/

Sending your translations

You can send your .po files to the salix-main mailing list (or the software author email) and they will be included in future versions of salixtools or other software. If it's a salixtools, just send an email to [1] attaching your .po files.