views:

248

answers:

2

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?

A: 

This seems to be right, make sure that you do not put any space or indentation to the heredoc identifier, EOD in your case.

Sarfraz
+1  A: 

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.

François Poirotte
Thanks for answering! Ups - apparently I didn't read the gettext ticket thoroughly enough. Because what I want is - kind of - the opposite: remove the markup from extracted strings in Poedit.I was heading in the direction you suggest; with preparing the text outside before the interpolation. But I still see this as a workaround, it would be really useful to extract text from inside heredoc syntax (IMHO).
Emil Rasmussen