从 Zend_Form 调用助手

发布于 2024-10-18 01:34:40 字数 201 浏览 3 评论 0原文

我尝试这段代码,但不起作用:


$this->getView()->translate("Name"); //not work
$this->_view->translate("Name"); //not work
$this->view->translate("Name"); //not work

I try this codes, but not works:


$this->getView()->translate("Name"); //not work
$this->_view->translate("Name"); //not work
$this->view->translate("Name"); //not work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪意 2024-10-25 01:34:40

首先,Zend_View 没有注入到 Zend_Form 中。因此,当您调用 $this->view$this->_view 时,它将不起作用,因为没有任何可返回的内容。为什么 getHelper() 有效?因为它通过辅助代理获取视图(如果您正在使用 viewRenderer)。看下面的代码:

// Zend/Form.php
public function getView()
{
    if (null === $this->_view) {
        require_once 'Zend/Controller/Action/HelperBroker.php';
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $this->setView($viewRenderer->view);
    }

    return $this->_view;
}

这就是为什么如果您之前调用 getView() 的话 $this->_view->translate() 会起作用,因为它存储为 protected财产。
据此,该代码应该完美运行并且适合我:

class My_Form extends Zend_Form
{
    public function init() 
    {
        echo $this->getView()->translate('name'); //fires 'translate' view helper and translating value
        //below will also work, because you have view now in _view: getView() fetched it.
        echo $this->_view->translate("another thing");
    }
}

顺便说一句。如果您使用翻译助手来翻译标签或字段名称,则不必这样做。如果您将翻译器对象设置为 Zend_Form 的静态属性就足够了,最好在引导程序中:

Zend_Form::setDefaultTranslator($translator);

从那时起,所有字段名称和标签都将自动翻译。

First of all, Zend_View is not injected into Zend_Form. So when you call $this->view or $this->_view it wont work, because there is nothing to return. Why getHelper() works? Because it fetches view via helper broker (and if your are using viewRenderer). Look below at the code:

// Zend/Form.php
public function getView()
{
    if (null === $this->_view) {
        require_once 'Zend/Controller/Action/HelperBroker.php';
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $this->setView($viewRenderer->view);
    }

    return $this->_view;
}

This is reason why $this->_view->translate() works if you call getView() before, because it's stored as protected property.
According to this, that code should work perfectly and works for me:

class My_Form extends Zend_Form
{
    public function init() 
    {
        echo $this->getView()->translate('name'); //fires 'translate' view helper and translating value
        //below will also work, because you have view now in _view: getView() fetched it.
        echo $this->_view->translate("another thing");
    }
}

BTW. If your using translate helper to translate labels or names of fields, you don't have to. Will be enough, if you set translator object as a static property of Zend_Form, best in your bootstrap:

Zend_Form::setDefaultTranslator($translator);

And from that moment all fields names and labels will be translated automatically.

ぇ气 2024-10-25 01:34:40

我不知道为什么,但是当我将此函数添加到我的表单中时,它起作用了:


public function init() {
        $this->getView();
    }

这条线的工作原理:


$this->_view->translate("Name");

I don't no why, but when I add this function to my form, it work:


public function init() {
        $this->getView();
    }

this line works:


$this->_view->translate("Name");

北凤男飞 2024-10-25 01:34:40

View 没有注入 Zend_Form (不要问我为什么,当它需要渲染时)。您必须扩展 Zend_Form 并将视图注入到您自己内部。其他选项是使用 FrontController->getInstance() >获取静态助手> viewRenderer 并从中接收视图。

View is not injected into Zend_Form (don't ask me why, when it's required for rendering). You have to extend Zend_Form and inject view inside yourself. Other option is using FrontController->getInstance() > getStaticHelper > viewRenderer and recieve view from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文