简体   繁体   English

从PHP heredoc语法中提取gettext转换?

[英]Extracting gettext translations from PHP heredoc syntax?

I'm using PHP's gettext functions for doing localization. 我正在使用PHP的gettext函数进行本地化。 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进行实际翻译,并且通过其“从源代码更新”功能,可以很容易地提取所有需要翻译的字符串 - 除了heredoc语法内部。

Poedit is using the xgettext program to generate the .po files from the PHP source files. Poedit正在使用xgettext程序从PHP源文件生成.po文件。 And it works beautifully when the PHP code looks like this: 当PHP代码如下所示,它工作得很好:

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

But the following doesn't get extracted (notice that a pseudo t-object needs to be used): 但是没有提取以下内容(请注意需要使用伪t对象):

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

In PHP code you could workaround the problem in the following way: 在PHP代码中,您可以通过以下方式解决问题:

<?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. 但我真的希望xgettext程序可以从heredoc块中提取字符串。

A workaround for that has been suggested in the PHP documentation comments. PHP文档注释中提出解决方法 The workaround is to treat tell the xgettext program to treat the PHP source files as Python code. 解决方法是告诉xgettext程序将PHP源文件视为Python代码。 But using this approach in Poedit gives me a lot of syntax errors from the xgettext parser. 但是在Poedit中使用这种方法会给我带来很多来自xgettext解析器的语法错误。

Does anyone know a workaround for getting xgettext to extract the translations from PHP heredoc syntax? 有没有人知道获取xgettext从PHP heredoc语法中提取翻译的解决方法?

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? 在gettext项目的票证系统上打开了一个有点相关的票证: http ://savannah.gnu.org/bugs/?27740这表明可以改进对heredoc语法的支持?

I am the reporter of the gettext ticket you're refering to in your post. 我是你在帖子中提到的gettext票的记者。 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. Gettext无法从这样的heredoc / nowdoc字符串中提取文本,但这在翻译大块文本时非常有用。

In my case, I use gettext in a CLI PHP script to translate chunks of texts that contain XML markup. 在我的例子中,我在CLI PHP脚本中使用gettext来翻译包含XML标记的文本块。 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. 必须手动转义标记中的每个引号或撇号,这使得消息很难在POedit或任何其他编辑器中读取。

In your case, it seems you'd like interpolated code in (heredoc/nowdoc) strings to be extracted. 在你的情况下,似乎你想要提取(heredoc / nowdoc)字符串中的插值代码。 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: 我不认为这应该被视为一个错误,因为与使用heredoc语法发布的代码完全等效的是:

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

from which gettext can not extract the "test" message either. gettext无法从中提取“测试”消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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