从 PHP Heredoc 语法中提取 gettext 翻译?

发布于 2024-08-21 04:07:42 字数 960 浏览 7 评论 0原文

我正在使用 PHP 的 gettext 函数进行本地化。我正在使用 Poedit 进行实际翻译,通过其“从源更新”功能,可以很容易地提取所有需要翻译的字符串 - 除了来自定界符语法内部的字符串。

Poedit 使用 xgettext 程序从 PHP 源文件生成 .po 文件。当 PHP 代码如下所示时,它工作得很好:

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

但是以下内容不会被提取(请注意,需要使用伪 t 对象):

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

在 PHP 代码中,您可以通过以下方式解决该问题:

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

但我真的希望 xgettext 程序能够从heredoc 块内提取字符串。

PHP 文档注释中已建议解决方法。解决方法是告诉 xgettext 程序将 PHP 源文件视为 Python 代码。但是在 Poedit 中使用这种方法会导致 xgettext 解析器出现很多语法错误。

有谁知道让 xgettext 从 PHP Heredoc 语法中提取翻译的解决方法?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思故 2024-08-28 04:07:42

我是您在帖子中提到的 gettext 票证的记者。
当我提交票证时,我的想法完全不同,更多的是这样的事情:

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

Gettext 无法从此类heredoc/nowdoc 字符串中提取文本,但这在翻译大段文本时可能非常有用。

就我而言,我在 CLI PHP 脚本中使用 gettext 来翻译包含 XML 标记的文本块。该标记是原始文本的一部分,也必须翻译。必须手动转义标记中的每个引号或撇号使得消息在 POedit 或任何其他编辑器中很难阅读。

就您而言,您似乎希望提取 (heredoc/nowdoc) 字符串中的内插代码。您可以通过在实际使用插值之前准备文本来轻松解决此问题:

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

我认为这不应被视为错误,因为与您使用heredoc语法发布的代码完全相同的是:

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

从中 gettext 无法提取“测试”消息。

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.

从来不烧饼 2024-08-28 04:07:42

作为多年来的 gettext 支持者,很难不注意到这张票已经有 13 年的历史了,而且 gettext 项目似乎并不认为这种情况是一个错误或某种严重的情况。

问题本身是 xgettext 提取器不将 heredoc/nowdoc php 语法视为有效,尤其是自 7.3 以来可用的缩进变体。

为了继续使用 gettext 工作流程,特别是 POedit,我们开发了一个库,允许处理翻译提取(通过 POT 文件),可在 https://github.com/vertilia/text 或作为作曲家要求:

composer require vertilia/text

这可能有助于其他开发人员继续走 gettext 道路。

免责声明:我是 vertilia/text 的开发人员。

Being a gettext proponent for years, it's hard not to notice this ticket is 13 years old now, and seems like gettext project does not see the situation as a bug or somehow critical.

The problem in itself is xgettext extractor does not treat heredoc/nowdoc php syntax as valid, and especially it's indented variation available since 7.3.

To continue using gettext workflow and POedit in particular, we developed a library that allows to handle translation extraction (via POT files), available at https://github.com/vertilia/text or as a composer requirement:

composer require vertilia/text

This may help other developers stay on gettext road.

Disclaimer: I am a developer of vertilia/text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文