gettext: Preparing Shell Scripts

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