PHP gettext中的变量

Ter*_*den 13 php localization gettext internationalization po

可能重复:
gettext如何处理动态内容?

我正在使用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"
Run Code Online (Sandbox Code Playgroud)

但我认为无法传递一个或多个变量.这可能吗?

Luc*_*one 32

poedit认识到了变种.

msgid "Are you sure you want to block %s?"
msgstr "Sind Sie sicher, dass Sie %s blockieren?"
Run Code Online (Sandbox Code Playgroud)

在PHP中

sprintf(_('Are you sure you want to block %s?'),'Alice');
Run Code Online (Sandbox Code Playgroud)

  • sprintf的?为什么?printf打印字符串,sprintf返回它.也许你写'echo sprintf`. (2认同)
  • @chumkiu - 这里可能使用`sprintf()`是出于与gettext和本地化无关的原因.(应用程序本身可能需要存储/操作生成的字符串,而不是直接将其输出到stdout.) (2认同)