【问题标题】:Variables in PHP gettext [duplicate]PHP gettext中的变量[重复]
【发布时间】:2012-10-03 09:02:17
【问题描述】:

可能重复:
How does gettext handle dynamic content?

我正在使用 PHP 的 gettext。我想翻译一个包含变量的句子。这可能吗?

例如,英文:

Are you sure you want to block Alice?

(其中“Alice”是用户名。)

但是,在德语中,主语不会出现在句末。

Sind Sie sicher, dass Sie Alice blockieren?

在messages.po中,我有

msgid "BLOCK"
msgstr "Are you sure you want to block"

但我看不到传递一个或多个变量的方法。这可能吗?

【问题讨论】:

  • %ssprintf 等占位符通常用于此目的。此外,您不应将 ABBreviations 用于 gettext 源字符串。对未翻译的消息使用简单的英语。消息在实践中很少发生变化,如果发生这种情况,gettext 目录可以很容易地进行调整。

标签: php localization internationalization gettext po


【解决方案1】:

poedit 识别变量。

msgid "Are you sure you want to block %s?"
msgstr "Sind Sie sicher, dass Sie %s blockieren?"

在 PHP 中

sprintf(_('Are you sure you want to block %s?'),'Alice');

【讨论】:

  • 这很简单!谢谢。已编辑以显示 sprintf。 printf 只显示长度。
  • Sprintf?为什么? Printf 打印字符串并 sprintf 返回它。也许你写echo sprintf
  • @chumkiu - sprintf() 可能出于与 gettext 和本地化无关的原因在这里使用。 (应用程序本身可能需要存储/处理结果字符串,而不是直接将其输出到标准输出。)
  • 可以用<?= __('key') ?>做这样的事情吗?