DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

(gettext.info.gz) Interpolation II

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