例如,我想打印$ 10,其中的数字存储在变量中,我还希望能够在价格附近定位字符串。
这是我的代码:
__("I understand I will be charged \${$cost} for listing this item.");
此打印:
I understand I will be charged $10 for listing this item.
如果我在变量前不使用\ $,则会得到以下内容,且没有美元符号或费用:
我知道我会因为列出该物品而被收费。
好的,很好。
除了我运行msgmerge生成用于翻译的本地化文件时,它抱怨:invalid control sequence
似乎不喜欢“ \ $”部分,因为它在我删除时通过。但是我不确定该如何安抚msgmerge并获得正确打印的费用。
恐怕不是要使用gettext的方式。 PHP源代码中对国际化功能的调用需要具有静态字符串文字。否则,msgmerge之类的帮助器工具将看不到文本,因为它们不执行PHP,而只是阅读源代码。
通常的方法是:
// Print to stdout
printf(_('I understand I will be charged $%s for listing this item.'), $cost);
...或:
// Save to a variable
$string = sprintf(_('I understand I will be charged $%s for listing this item.'), $cost);
这是正在发生的事情:
<?php
$cost = "money";
$money = 10;
$text = "The cost is ${$cost}";
var_dump($text);
这将打印:The cost is 10;
因为$ cost变成金钱而$ money变成10;
在您的情况下,它失败了,因为它变成$ 10,在PHP中无效。