在多模式下使用Zend_Translate时,是否可以将变量注入文本中

时间:2012-02-28 15:57:20

标签: zend-framework zend-translate plural

我正在尝试使用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是否支持这种使用复数的方式?即将变量值注入复数字符串?或者我必须在复数形式之前和之后拆分字符串,单独翻译然后在输出处连接?

1 个答案:

答案 0 :(得分: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以更智能的方式来执行此操作。