[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Use of gettext in scripts

Here's a question.  In a C program, it makes sense to have a command
such as:

  printf(gettext("I am a %s, what are you?\n"), name);

for then the translations will place the noun (represented by %s) in
the appropriate place in the translation.  (This may be a silly
example, but it will suffice.)

But what would the equivalent be in a shell script or a Perl script?
The following "solution" doesn't work, because the substitutions
happen before gettext sees the string:

sh:    gettext -s "I am a $name, what are you?"
perl:  print gettext("I am a $name, what are you?\n");

and these don't because the word-order is not guaranteed to be the
same in all languages:

sh:    gettext -n -s "I am a "; echo $name; gettext -s ", what are you?"
perl:  print gettext("I am a "), $name, gettext(", what are you?\n");

So the following looks like a possibility, but it's a bit yucky:

sh:    text=`gettext -s 'I am a $name, what are you?'`
       eval echo $text
perl:  $text=gettext 'I am a $name, what are you?';
       eval "print $text, '\n'";

Here's the question: is there any standard, preferably nicer, way of
handling strings containing variables in shell or Perl scripts?



  Julian Gilbey, Dept of Maths, QMW, Univ. of London. J.D.Gilbey@qmw.ac.uk
             Debian GNU/Linux Developer.  jdg@debian.org
       -*- Finger jdg@master.debian.org for my PGP public key. -*-

Reply to: