此语法应该可以满足您的需要。
$category = Mage::helper('catalog/data')->getCategory();
$continues = ($category ? $this->__(' or <a href="%s">continue shopping</a>', $category->getUrl()) : '');
$message = $this->__('%s was added to your shopping cart. Click to <a href="/checkout/cart/" id="itemAdder" class="addSuccess">View Your Cart</a>%s.', Mage::helper('core')->escapeHtml($product->getName()), $continues);
$this->_getSession()->addSuccess($message);
代码中发生了什么:
- 获取类别
- 检查类别是否存在,如果存在,设置继续文本,如果不存在,将其设置为空白值。(使用PHP 三元运算符)
- 在标准消息中包含 $continues 变量
- 将成功消息添加到会话。
快速解释$this__('Text here %s', $variable)
Magento 使用此功能进行大部分文本翻译。
你看到使用的%s
实际上被传递给函数的变量所取代。
例如,假设你有这个:$this__('Hello, my name is %s', $name)
在这种情况下,如果$name = 'John';
,输出将是Hello, my name is John
您也可以定义多个变量。因此,如果您有:
$this__('Hello, my name is %s and I am a %s', $name, $jobtitle)
和$name = 'John'
,$jobtitle = 'Farmer'
输出将是:
Hello, my name is John and I am a Farmer.
以定义变量的%s
顺序替换。所以第一个实例%s
将被第一个变量$name
替换,第二个被替换$jobtitle
,依此类推。您可以根据需要定义任意数量的变量,只需知道变量需要按特定顺序排列即可。