Zend_Form 值和错误的 I18n
假设这种情况
$form->addElement('select', 'companies', array(
'disableTranslator' => true,
'label' => Zend_Registry::get('Zend_Translate')->_('companies'),
'filters' => array('Int'),
'required' => true,
'multiOptions' => array(1 => 'Company 1', 2 => 'Company 2')
));
if($_POST && $form->isValid($_POST)) {
$form->save();
}
$form->render();
此选择用于从给定的选项列表中选择公司。
该表单有一个默认翻译器集,其中包含 1 个翻译短语 "my_translated_text" => “翻译来了”
。
选择框中的选项是根据用户输入的数据填充的,这意味着用户可以添加名为 my_translated_text
的公司,该公司又应在选择框中显示为“my_translated_text”的新选项。
如果没有 disableTranslator =>; true
选项,my_translated_text
公司将被翻译为这里是翻译
,因此显示为翻译文本 ->我们需要禁用翻译。
但如果有 disableTranslator =>; true
错误消息(例如“该值是必需的”)不会被翻译。
理想情况下,我不想翻译这些值,但想翻译有关选择框的其他所有内容。
您如何处理这种情况?你有遇到过类似的问题吗?
谢谢
assume this situation
$form->addElement('select', 'companies', array(
'disableTranslator' => true,
'label' => Zend_Registry::get('Zend_Translate')->_('companies'),
'filters' => array('Int'),
'required' => true,
'multiOptions' => array(1 => 'Company 1', 2 => 'Company 2')
));
if($_POST && $form->isValid($_POST)) {
$form->save();
}
$form->render();
This select serves to select companies from a given list of options.
The form has a default translator set with 1 translated phrase "my_translated_text" => "here comes the translation"
.
The options in select box are populated based on data entered by users which means users may add a company with name my_translated_text
which in turn should appear as new option in the select box saying "my_translated_text".
If there was no disableTranslator => true
option, the my_translated_text
company would be translated to here comes the translation
and thus appear as translated text -> we need to disable translations.
But if there's disableTranslator => true
the error messages (such as "The value is required") are not translated.
Ideally I don't want to translate the values, but want to translate everything else regarding the select box.
How do you deal with such situation? Have you ever had similar problem?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在理解这个问题时遇到一些困难,但如果我理解正确,您不希望翻译选项值。无法从
Zend_Form
执行此操作。正如您所说,您只能启用/禁用整个元素的翻译器。您最好的选择是创建自定义表单元素或装饰器。我不完全确定翻译发生在哪里,但我认为选项已经在 Zend_Form_Multi 的
_translateValue()
方法。另一个需要注意的类是渲染选择框的视图助手。旁注:当有默认翻译器集时,您不需要这样做,
因为 标签默认翻译。即使您必须这样做,最好通过 < 检索翻译器code>Zend_Form::getTranslator() 而不是通过
Zend_Registry
来避免耦合。I have some trouble understand the question, but if I understand correctly, you do not want the option values translated. There is no way to do this from
Zend_Form
. Like you said, you can only en/disable the translator for the entire element.Your best bet would be to create a custom Form Element or Decorator. I am not entirely sure where the translations take place, but I think the options are already translated in Zend_Form_Multi's
_translateValue()
method. Another class to look at would the View Helper rendering the Select Box.On a sidenote: when there is a default translator set, you should not need to do
because labels are translated by default. And even if you had to do this, the translator is better retrieved by
Zend_Form::getTranslator()
instead of viaZend_Registry
to avoid coupling.