[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
15.5.2.1 Preparing Shell Scripts for Internationalization
Preparing a shell script for internationalization is conceptually similar to the steps described in Preparing Program Sources. The concrete steps for shell scripts are as follows.
-
Insert the line
. gettext.sh
near the top of the script.
gettext.sh
is a shell function library that provides the functionseval_gettext
(see Invoking theeval_gettext
function) andeval_ngettext
(see Invoking theeval_ngettext
function). You have to ensure thatgettext.sh
can be found in thePATH
. -
Set and export the
TEXTDOMAIN
andTEXTDOMAINDIR
environment variables. UsuallyTEXTDOMAIN
is the package or program name, andTEXTDOMAINDIR
is the absolute pathname corresponding to$prefix/share/locale
, where$prefix
is the installation location.TEXTDOMAIN=@PACKAGE@ export TEXTDOMAIN TEXTDOMAINDIR=@LOCALEDIR@ export TEXTDOMAINDIR
- Prepare the strings for translation, as described in Preparing Translatable Strings.
-
Simplify translatable strings so that they don’t contain command substitution
(
"`...`"
or"$(...)"
), variable access with defaulting (like${variable-default}
), access to positional arguments (like$0
,$1
, ...) or highly volatile shell variables (like$?
). This can always be done through simple local code restructuring. For example,echo "Usage: $0 [OPTION] FILE..."
becomes
program_name=$0 echo "Usage: $program_name [OPTION] FILE..."
Similarly,
echo "Remaining files: `ls | wc -l`"
becomes
filecount="`ls | wc -l`" echo "Remaining files: $filecount"
-
For each translatable string, change the output command ‘echo’ or
‘$echo’ to ‘gettext’ (if the string contains no references to
shell variables) or to ‘eval_gettext’ (if it refers to shell variables),
followed by a no-argument ‘echo’ command (to account for the terminating
newline). Similarly, for cases with plural handling, replace a conditional
‘echo’ command with an invocation of ‘ngettext’ or
‘eval_ngettext’, followed by a no-argument ‘echo’ command.
When doing this, you also need to add an extra backslash before the dollar sign in references to shell variables, so that the ‘eval_gettext’ function receives the translatable string before the variable values are substituted into it. For example,
echo "Remaining files: $filecount"
becomes
eval_gettext "Remaining files: \$filecount"; echo
If the output command is not ‘echo’, you can make it use ‘echo’ nevertheless, through the use of backquotes. However, note that inside backquotes, backslashes must be doubled to be effective (because the backquoting eats one level of backslashes). For example, assuming that ‘error’ is a shell function that signals an error,
error "file not found: $filename"
is first transformed into
error "`echo \"file not found: \$filename\"`"
which then becomes
error "`eval_gettext \"file not found: \\\$filename\"`"
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on January 24, 2013 using texi2html 5.0.