加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

zend-framework – 在多模式下使用Zend_Translate时,是否可以将

发布时间:2020-12-13 16:15:13 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用Zend_translate,我必须将一个变量值注入到结果字符串中,并使字符串尊重复数形式.在视图脚本中使用常规(非复数)视图助手$this- translate()我可以将一个变量注入到字符串中: $this-translate('You have %1$s questions to answer',3) // would
我正在尝试使用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 ) 
)

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

You have %1$s questions to answer

我想要的是

You have 2 questions to answer

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

解决方法

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

<?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以更智能的方式来做到这一点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读