gettext: Interpolation II

1 
1 15.5.18.6 Valid Uses Of String Interpolation
1 ............................................
1 
1    Perl is often used to generate sources for other programming
1 languages or arbitrary file formats.  Web applications that output HTML
1 code make a prominent example for such usage.
1 
1    You will often come across situations where you want to intersperse
1 code written in the target (programming) language with translatable
1 messages, like in the following HTML example:
1 
1      print gettext <<EOF;
1      <h1>My Homepage</h1>
1      <script language="JavaScript"><!--
1      for (i = 0; i < 100; ++i) {
1          alert ("Thank you so much for visiting my homepage!");
1      }
1      //--></script>
1      EOF
1 
1    The parser will extract the entire here document, and it will appear
1 entirely in the resulting PO file, including the JavaScript snippet
1 embedded in the HTML code.  If you exaggerate with constructs like the
1 above, you will run the risk that the translators of your package will
1 look out for a less challenging project.  You should consider an
1 alternative expression here:
1 
1      print <<EOF;
1      <h1>$gettext{"My Homepage"}</h1>
1      <script language="JavaScript"><!--
1      for (i = 0; i < 100; ++i) {
1          alert ("$gettext{'Thank you so much for visiting my homepage!'}");
1      }
1      //--></script>
1      EOF
1 
1    Only the translatable portions of the code will be extracted here,
1 and the resulting PO file will begrudgingly improve in terms of
1 readability.
1 
1    You can interpolate hash lookups in all strings or quote-like
1 expressions that are subject to interpolation (see the manual page ‘man
1 perlop’ for details).  Double interpolation is invalid, however:
1 
1      # TRANSLATORS: Replace "the earth" with the name of your planet.
1      print gettext qq{Welcome to $gettext->{"the earth"}};
1 
1    The ‘qq’-quoted string is recognized as an argument to ‘xgettext’ in
1 the first place, and checked for invalid variable interpolation.  The
1 dollar sign of hash-dereferencing will therefore terminate the parser
1 with an “invalid interpolation” error.
1 
1    It is valid to interpolate hash lookups in regular expressions:
1 
1      if ($var =~ /$gettext{"the earth"}/) {
1         print gettext "Match!\n";
1      }
1      s/$gettext{"U. S. A."}/$gettext{"U. S. A."} $gettext{"(dial +0)"}/g;
1