我试图在必须将变量值注入(inject)结果字符串并使字符串尊重复数形式的情况下使用 Zend_translate。在 View 脚本中使用常规(非复数) View 助手 $this->translate() 我可以将变量注入(inject)到字符串中:
$this->translate('You have %1$s questions to answer', 3)
// would result in "You have 3 questions to answer" being output
但是,当使用 Zend 所说的现代复数表示法时,我该怎么做呢?显然 $this->translate() View 助手本身不支持复数符号,而是我必须调用
$this->translate()->getTranslator()->translate(
array('You have %1$s question to answer',
'You have %1$s questions to answer', $someNr )
)
但那时我只有带有变量占位符的复数字符串,我没有带有注入(inject)值的字符串。换句话说,我得到的是:
You have %1$s questions to answer
我想要的是
You have 2 questions to answer
所以问题是,Zend_Translate 是否以某种方式支持这种使用复数的方式? IE。将变量值注入(inject)复数字符串?还是我必须在复数形式之前和之后拆分字符串,分别翻译每个字符串,然后在输出时连接?
最佳答案
在 Controller (或其他地方)中:
<?php
$translate = new Zend_Translate (array (
'adapter' => 'Zend_Translate_Adapter_Array',
'content' => array (
'test' => 'You have %1$s %2$s to answer'
),
'locale' => 'en'
));
在 View 中:
<?php
$x = 1;
echo $this->translate ('test', $x, $this->translate (array (
'question',
'questions',
$x
)));
?>
但是你可能想看看http://framework.zend.com/manual/en/zend.translate.plurals.html一种更智能的方法来做到这一点。
关于zend-framework - 在复数模式下使用 Zend_Translate 时,你能在文本中注入(inject)变量吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9485260/