例如,我想打印 $10,其中数字存储在变量中,并且我还希望能够本地化价格周围的字符串。
这是我的代码:
__("I understand I will be charged \${$cost} for listing this item.");
打印:
I understand I will be charged $10 for listing this item.
如果我不在变量前使用\$,我会得到以下内容,没有美元符号或成本:
我了解我将因列出此商品而被付费。
好的,没问题。
除了当我运行 msgmerge 生成用于翻译目的的本地化文件时,它会提示:无效的控制序列
它似乎不喜欢“\$”部分,因为当我删除它时它就通过了。但我不知道该怎么做才能安抚 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 - 消息合并 : `invalid control sequence` when printing a price with a dollar sign,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59000138/