如何在 zend 中添加多种类型的翻译

发布于 2024-10-18 02:26:57 字数 856 浏览 2 评论 0原文

我用了这个代码 尝试从 /resources/linguals 添加翻译数组 和 从 /application/languages/nl.mo (gettext) 添加翻译

$translate = new Zend_Translate(
        array(

            'adapter' => 'array',
            'content' => APPLICATION_PATH . '/resources/languages',
            'locale'  => 'nl',
            'scan' => Zend_Translate::LOCALE_DIRECTORY
        )
    );


    $translate->addTranslation(
        array(
            'adapter' => 'gettext',
            'content' => APPLICATION_PATH.DIRECTORY_SEPARATOR.'languages'.DIRECTORY_SEPARATOR.'nl_NL.mo',
            'locale'  => 'nl'
        )
    );

当我尝试添加单个翻译时,一切正常(在两种情况下) 当我添加第二个翻译时,出现此错误:

致命错误:未捕获的异常 'Zend_Translate_Exception' 与 消息'错误包括数组或文件 '1'' 中 blabla.php 第 61 行

有人可以告诉我如何从引导程序添加这些翻译文件,而不会出现这些错误吗?

I used this code
to try and add a translation array from /resources/languages
ANd
to add a translation from /application/languages/nl.mo (gettext)

$translate = new Zend_Translate(
        array(

            'adapter' => 'array',
            'content' => APPLICATION_PATH . '/resources/languages',
            'locale'  => 'nl',
            'scan' => Zend_Translate::LOCALE_DIRECTORY
        )
    );


    $translate->addTranslation(
        array(
            'adapter' => 'gettext',
            'content' => APPLICATION_PATH.DIRECTORY_SEPARATOR.'languages'.DIRECTORY_SEPARATOR.'nl_NL.mo',
            'locale'  => 'nl'
        )
    );

When i try to add a single translation everything works ok (in both cases)
When i'm adding a second translation i get this error:

Fatal error: Uncaught exception
'Zend_Translate_Exception' with
message 'Error including array or file
'1'' in
blabla.php
on line 61

can someone tell me how to add those translation files from my bootstrap, without getting these errors?

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

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

发布评论

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

评论(1

场罚期间 2024-10-25 02:26:57

我不是 100% 确定,但我认为你不能混合类型的翻译文件。原因是 $translate->addTranslation() 实际上会调用 Zend_Translate_Adapter_Array 上的方法 addTranslation(),而该方法又会调用一个方法 _loadTranslationData 也在 Zend_Translate_Adapter_Array 上。因此,Zend_Translate_Adapter_Array 尝试将文件nl_NL.mo 作为数组读取,这会导致包含数组或文件的错误错误。

但是,如果翻译文件“nl”是来自 zend 资源的文件,并且您只想使用它来翻译 zend_form 消息,我认为您可以为此定义一个单独的转换器,如下所示:

$translate = new Zend_Translate(
    array(

        'adapter' => 'array',
        'content' => APPLICATION_PATH . '/resources/languages',
        'locale'  => 'nl',
        'scan' => Zend_Translate::LOCALE_DIRECTORY
    )
);

 // use this one in Zend_Form 
 Zend_Form::setDefaultTranslator($translate);

并且您可以将“nl_NL”其余默认:

$translate = new Zend_Translate(
                array(
                    'adapter' => 'gettext',
                    'content' => APPLICATION_PATH . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'nl_NL.mo',
                    'locale' => 'nl'
                )
);       

// Save it for the rest of application to use
Zend_Registry::set('Zend_Translate', $translate);

PS
我还没有测试过这个,因此我不能承认它有效,但这就是我至少会尝试这样做的方式。

I'm not 100% sure, but I think you cannot mix types of translation files. The reason is that $translate->addTranslation() will actually call a method addTranslation() on Zend_Translate_Adapter_Array, which in turn will call a method _loadTranslationData also on Zend_Translate_Adapter_Array. Thus Zend_Translate_Adapter_Array tries to read file nl_NL.mo as an array which results in Error including array or file error.

However, if the translation file 'nl' is the one from zend resources, and you want to use it only for translating zend_form messages, I think you could define a separate translator for this as follows:

$translate = new Zend_Translate(
    array(

        'adapter' => 'array',
        'content' => APPLICATION_PATH . '/resources/languages',
        'locale'  => 'nl',
        'scan' => Zend_Translate::LOCALE_DIRECTORY
    )
);

 // use this one in Zend_Form 
 Zend_Form::setDefaultTranslator($translate);

And the 'nl_NL' you can make your default for the rest:

$translate = new Zend_Translate(
                array(
                    'adapter' => 'gettext',
                    'content' => APPLICATION_PATH . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . 'nl_NL.mo',
                    'locale' => 'nl'
                )
);       

// Save it for the rest of application to use
Zend_Registry::set('Zend_Translate', $translate);

P.S.
I have not tested this, hence I cannot grantee it works, but this is how I would at least try to do it.

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