简体   繁体   中英

Extracting gettext translations from PHP heredoc syntax?

I'm using PHP's gettext functions for doing localization. I'm using Poedit to do the actual translation, and with its "Update from sources" feature it is really easy to extract all the strings that need to be translated - except from inside heredoc syntax.

Poedit is using the xgettext program to generate the .po files from the PHP source files. And it works beautifully when the PHP code looks like this:

echo "<h1>". _("test") ."</h1>";

But the following doesn't get extracted (notice that a pseudo t-object needs to be used):

echo <<<EOD
<h1>{$->_('test')}
EOD;

In PHP code you could workaround the problem in the following way:

<?php
$t = _('test');
echo <<<EOD
<h1>$t</h1>
EOD
?>

But I really would prefer that the xgettext program could extract the string from inside the heredoc block.

A workaround for that has been suggested in the PHP documentation comments. The workaround is to treat tell the xgettext program to treat the PHP source files as Python code. But using this approach in Poedit gives me a lot of syntax errors from the xgettext parser.

Does anyone know a workaround for getting xgettext to extract the translations from PHP heredoc syntax?

A somewhat related ticket has been opened on gettext project's ticket system: http://savannah.gnu.org/bugs/?27740 This indicates that support for the heredoc syntax could be improved?

I am the reporter of the gettext ticket you're refering to in your post. When I submitted the ticket, I had something completely different in mind, something more along those lines:

<?php
  $msg = _(<<<TXT
  He said: "Hello world!".
  TXT
);
?>

Gettext can not extract text from such heredoc/nowdoc strings, but this could be really useful when translating large pieces of text.

In my case, I use gettext in a CLI PHP script to translate chunks of texts that contain XML markup. That markup is part of the original text and has to be translated too. Having to manually escape each and every quote or apostrophe in the markup makes the messages quite hard to read in POedit or any other editor.

In your case, it seems you'd like interpolated code in (heredoc/nowdoc) strings to be extracted. You can easily workaround this problem by preparing the text before you actually use interpolation:

<?php
$t = _('test');
echo <<<EOD
<h1>$t</h1>
EOD
?>

I don't think this should be considered a bug because the exact equivalent to the code you posted using heredoc syntax would be:

<?php
echo "<h1>{$t->_('test')}</h1>";
?>

from which gettext can not extract the "test" message either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM