how to add multiple types of translations in zend

2.5k views Asked by At

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?

1

There are 1 answers

2
Marcin On

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.