php - 消息合并 : `invalid control sequence` when printing a price with a dollar sign

标签 php gettext

例如,我想打印 $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/

相关文章:

php - 如何提交字段名重复的表单数据?

php - 使用 PHP 5.6 无法为 Ubuntu 16.04.3 加载 cURL

php - 如何选择句子的前 10 个单词?

php - 数据表从服务器端处理在客户端搜索数据

python - Django makemessages 错误未知编码 "utf8"

php - 在php中更改ISO日期时间格式

c - 如何在 c 中将 gettext 与 __VA_ARGS__ 一起使用

linux - 无法通过 debian 包翻译我的程序,但使用 autotools 可以

localization - gettext 如何处理 a/an 或 d'/de?

javascript - 如何在 vue 中使用 gettext 翻译文本?