poEdit 表单中 addMultiOption 文本的 ZEND 翻译

发布于 2024-12-17 21:12:18 字数 1587 浏览 3 评论 0原文

我不知道为什么翻译不能与 Zend_Form 一起使用。 我希望能够翻译选择的选项。 现在我有这样的东西:

我的表单类:

(...)

$this->translate = Zend_Registry::get('translate');
Zend_Form::setDefaultTranslator( Zend_Registry::get('translate') );

(...)
     $select = new Zend_Form_Element_Select('select');
     // $select->addMultiOption('0', $this->translate('Aktywny'));
     $select->addMultiOption('0', $this->translate->_('Aktywny'));
     $select->addMultiOption('1', 'Nieaktywny');

在我的引导文件中我有这样的东西:

    protected function _initTranslate()
{
    Zend_Loader::loadClass('Zend_Translate');
    Zend_Loader::loadClass('Zend_Registry');

    $translate = new Zend_Translate('gettext', APPLICATION_PATH.'/languages',
                                    'browser',
                                    array('scan' => Zend_Translate::LOCALE_FILENAME));

            //changing language and setting it to session if changed                
    $session = new Zend_Session_Namespace('jezyk');
    if(isset($session->language)) {
        $translate->setLocale($session->language);
    } else
        $translate->setLocale('pl');


    $registry = Zend_Registry::getInstance();
    $registry->set('Zend_Translate', $translate);   

}

它适用于控制器,phtml文件和插件,我在

$this->translate('string to translate');

插件中调用它,

$this->view->translate('string to translate');

但这些方法在表单中不起作用。它抛出异常:

警告:表单捕获异常:没有为键“translate”注册任何条目堆栈跟踪:#0

I dont have an idea why translations are not working in with Zend_Form.
I would like to be able translate options for selects.
For now i have something like this:

my form class:

(...)

$this->translate = Zend_Registry::get('translate');
Zend_Form::setDefaultTranslator( Zend_Registry::get('translate') );

(...)
     $select = new Zend_Form_Element_Select('select');
     // $select->addMultiOption('0', $this->translate('Aktywny'));
     $select->addMultiOption('0', $this->translate->_('Aktywny'));
     $select->addMultiOption('1', 'Nieaktywny');

in my bootstrap file i have something like this:

    protected function _initTranslate()
{
    Zend_Loader::loadClass('Zend_Translate');
    Zend_Loader::loadClass('Zend_Registry');

    $translate = new Zend_Translate('gettext', APPLICATION_PATH.'/languages',
                                    'browser',
                                    array('scan' => Zend_Translate::LOCALE_FILENAME));

            //changing language and setting it to session if changed                
    $session = new Zend_Session_Namespace('jezyk');
    if(isset($session->language)) {
        $translate->setLocale($session->language);
    } else
        $translate->setLocale('pl');


    $registry = Zend_Registry::getInstance();
    $registry->set('Zend_Translate', $translate);   

}

and it works fine for controllers, phtml files and plugins where i call it by

$this->translate('string to translate');

and in plugins

$this->view->translate('string to translate');

but those methods won't work in form. It throws exception:

Warning: Exception caught by form: No entry is registered for key 'translate' Stack Trace: #0

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

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

发布评论

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

评论(1

七秒鱼° 2024-12-24 21:12:18

为了使它像我在评论中所写的那样工作,只需更改行:

$this->translate = Zend_Registry::get('translate');

因为

$this->translate = Zend_Registry::get('Zend_Translate');

我没有看到我从注册表中得到了错误的翻译。它应该是像 Bootstrap 文件中那样的 Zend_Translate,而不是像我那样翻译。

这是我的翻译问题的解决方案,现在我可以在表单文件中进行翻译:)

to make it working as i wrote in comment just have to change line:

$this->translate = Zend_Registry::get('translate');

for

$this->translate = Zend_Registry::get('Zend_Translate');

cause i didn't saw that i'm getting wrong translate from registry. It should be Zend_Translate like in Bootstrap file, not translate as i did.

And this is solution for my problems with translate and now i can make translations in form files :)

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