Next: Preparing Shell Scripts for the printf_gettext approach, Previous: Preparing Shell Scripts for the gettext.sh approach, Up: sh - Shell Script [Contents][Index]
printf approach ¶ Preparing a shell script for internationalization is conceptually similar to the steps described in Preparing Program Sources. The concrete steps for shell scripts with this approach are as follows.
TEXTDOMAIN and TEXTDOMAINDIR environment
variables.
Usually TEXTDOMAIN is the package or program name, and
TEXTDOMAINDIR is the absolute pathname corresponding to
$prefix/share/locale, where $prefix is the installation location.
TEXTDOMAIN=@PACKAGE@ export TEXTDOMAIN TEXTDOMAINDIR=@LOCALEDIR@ export TEXTDOMAINDIR
"`...`" 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"
When doing this, you also need to replace the references to shell variables with format string directives (see Shell Format Strings), 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
env printf "`gettext \"Remaining files: %u.\"`"'\n' "$filecount"
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 "`env printf \"\`gettext \\\"file not found: %s\\\"\`\" \"\$filename\"`"