gpt4 book ai didi

magento - %s 变量在 Magento 中不起作用

转载 作者:行者123 更新时间:2023-12-03 07:49:50 25 4
gpt4 key购买 nike

当出现错误或成功时,我总是使用 %s 变量作为占位符,例如以下代码:

Mage::getSingleton('customer/session')->addError(Mage::helper('module')
->__('Hi %s, there is an error', $name));

问题是:有时有效,有时无效。当然 $name 确实包含一个有效的字符串值(我已经 var_dump 很多次了)。你知道为什么吗?

最佳答案

正如其他人所指出的,您不限于翻译字符串中的一个参数。一些例子:

一个论点,在一个地方:

Mage::helper('module')->__('Hi %s, there is an error', $name);

两个论点,在两个地方:
Mage::helper('module')->__('Hi %s, there is an error with code %s', $name, $code);

一个论点,在两个地方:
Mage::helper('module')->__('Hi %1$s, there is an error. Your name is %1$s', $name);

两个参数,交换位置:
// Template or block file
Mage::helper('module')->__('Hi %1$s %2$s, there is an error', $firstname, $lastname);

// CSV translation
"Hi %1$s %2$s, there is an error", "%2$s, %1$s received an error"

可以在此处找到有关用于支持此功能的 PHP 函数的文档: vsprintf

(PS: knase 所说的双引号仅适用于您的 CSV 文件,其中双引号是转义的单引号。)

关于magento - %s 变量在 Magento 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20021414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com