从 Zend_Form 调用助手
我尝试这段代码,但不起作用:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,
Zend_View
没有注入到 Zend_Form 中。因此,当您调用$this->view
或$this->_view
时,它将不起作用,因为没有任何可返回的内容。为什么getHelper()
有效?因为它通过辅助代理获取视图(如果您正在使用 viewRenderer)。看下面的代码:这就是为什么如果您之前调用
getView()
的话$this->_view->translate()
会起作用,因为它存储为 protected财产。据此,该代码应该完美运行并且适合我:
顺便说一句。如果您使用翻译助手来翻译标签或字段名称,则不必这样做。如果您将翻译器对象设置为 Zend_Form 的静态属性就足够了,最好在引导程序中:
从那时起,所有字段名称和标签都将自动翻译。
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. WhygetHelper()
works? Because it fetches view via helper broker (and if your are using viewRenderer). Look below at the code:This is reason why
$this->_view->translate()
works if you callgetView()
before, because it's stored as protected property.According to this, that code should work perfectly and works for me:
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:And from that moment all fields names and labels will be translated automatically.
我不知道为什么,但是当我将此函数添加到我的表单中时,它起作用了:
这条线的工作原理:
I don't no why, but when I add this function to my form, it work:
this line works:
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.