zend-framework - 在复数模式下使用 Zend_Translate 时,你能在文本中注入(inject)变量吗

标签 zend-framework zend-translate plural

我试图在必须将变量值注入(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/

相关文章:

修改 $_POST, $_FILES 的 PHP 模块

php - Zend Framework 数据层架构

php - 如何保存ajax数据而不因点击后退按钮而丢失?

php - 在 zend 地址栏中使用 _redirect() 时不显示新的 url

php - Zend Framework 2 - 翻译标准表单验证和错误消息

const string plus boolean 在 C 中复数

java - 在Java中使用正则表达式获取单数或复数字符串

php - 使用 PHP Gettext 无需安装语言环境

zend-framework - 您如何组织已注册的gettext翻译文件?

ruby-on-rails-3 - Rails 中关联的复数名称