27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appendix E. Localization<br />

Localization is an undocumented Bash feature.<br />

A localized shell script echoes its text output in the language defined as the system's locale. A Linux user in<br />

Berlin, Germany, would get script output in German, whereas his cousin in Berlin, Maryland, would get<br />

output from the same script in English.<br />

To create a localized script, use the following template to write all messages to the user (error messages,<br />

prompts, etc.).<br />

#!/bin/bash<br />

# localized.sh<br />

E_CDERROR=65<br />

error()<br />

{<br />

printf "$@" >&2<br />

exit $E_CDERROR<br />

}<br />

cd $var || error $"Can't cd to %s." "$var"<br />

read −p $"Enter the value: " var<br />

# ...<br />

bash$ bash −D localized.sh<br />

"Can't cd to %s."<br />

"Enter the value: "<br />

This lists all the localized text. (The −D option lists double−quoted strings prefixed by a $, without executing<br />

the script.)<br />

bash$ bash −−dump−po−strings localized.sh<br />

#: a:6<br />

msgid "Can't cd to %s."<br />

msgstr ""<br />

#: a:7<br />

msgid "Enter the value: "<br />

msgstr ""<br />

The −−dump−po−strings option to Bash resembles the −D option, but uses gettext "po" format.<br />

Now, build a language.po file for each language that the script will be translated into, specifying the<br />

msgstr. As an example:<br />

fr.po:<br />

#: a:6<br />

msgid "Can't cd to %s."<br />

msgstr "Impossible de se positionner dans le répertoire %s."<br />

#: a:7<br />

msgid "Enter the value: "<br />

msgstr "Entrez la valeur : "<br />

Appendix E. Localization 415

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!