4

我试图在必须将变量值注入结果字符串并使字符串尊重复数形式的情况下使用 Zend_translate。在视图脚本中使用常规(非复数)视图助手 $this->translate() 我可以将变量注入字符串:

$this->translate('You have %1$s questions to answer', 3) 
// would result in "You have 3 questions to answer" being output

但是,当使用 Zend 所说的现代复数表示法时,我该怎么做呢?显然 $this->translate() 视图助手本身不支持复数符号,而是我必须调用

$this->translate()->getTranslator()->translate( 
    array('You have %1$s question to answer', 
    'You have %1$s questions to answer', $someNr ) 
)

但是那时我只有带有变量占位符的复数字符串,我没有带有注入值的字符串。换句话说,我得到的是:

您有 %1$s 个问题要回答

我想要的是

你有 2 个问题要回答

所以问题是,Zend_Translate 是否以某种方式支持这种使用复数的方式?即在复数字符串中注入一个变量值?或者我是否必须在复数形式之前和之后拆分字符串,分别翻译每个字符串,然后在输出时连接?

4

1 回答 1

2

在控制器(或其他地方)中:

<?php
        $translate = new Zend_Translate (array (
            'adapter' => 'Zend_Translate_Adapter_Array',
            'content' => array (
                'test' => 'You have %1$s %2$s to answer'
            ),
            'locale' => 'en'
        ));

在视图中:

<?php
$x = 1;
echo $this->translate ('test', $x, $this->translate (array (
    'question', 
    'questions', 
    $x 
)));
?>

但是您可能想查看http://framework.zend.com/manual/en/zend.translate.plurals.html以获得更智能的方法来执行此操作。

于 2012-02-29T07:11:20.170 回答