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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

Then, run msgfmt.<br />

msgfmt −o localized.sh.mo fr.po<br />

Place the resulting localized.sh.mo file in the /usr/local/share/locale/fr/LC_MESSAGES<br />

directory, and at the beginning of the script, insert the lines:<br />

TEXTDOMAINDIR=/usr/local/share/locale<br />

TEXTDOMAIN=localized.sh<br />

If a user on a French system runs the script, she will get French messages.<br />

With older versions of Bash or other shells, localization requires gettext, using the −s option. In this<br />

case, the script becomes:<br />

#!/bin/bash<br />

# localized.sh<br />

E_CDERROR=65<br />

error() {<br />

local format=$1<br />

shift<br />

printf "$(gettext −s "$format")" "$@" >&2<br />

exit $E_CDERROR<br />

}<br />

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

read −p "$(gettext −s "Enter the value: ")" var<br />

# ...<br />

The TEXTDOMAIN and TEXTDOMAINDIR variables need to be exported to the environment.<br />

−−−<br />

This appendix written by Stephane Chazelas.<br />

Appendix E. Localization 416

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

Saved successfully!

Ooh no, something went wrong!