PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Combining multiple translation sources

When you are working with multiple translations you may come into a situation where you want to use different source types. For example the resource files which are provided by the framework and your own translations which are available by using the gettext adapter.

By combining multiple translation adapters you can use them within one instance. See the following example:

<?php
$translate 
= new Zend_Translate(
    array(
        
'adapter' => 'gettext',
        
'content' => '\path\to\translation.mo',
        
'locale'  => 'en'
    
)
);

$translate_second = new Zend_Translate(
    array(
        
'adapter' => 'array',
        
'content' => '\resources\languages\en\Zend_Validate.php',
        
'locale'  => 'en'
    
)
);

$translate->addTranslation(array('content' => $translate_second));

Now the first instance holds all translations from the second instance and you can use it within the application even if you used different source types.

Memory savings

As you may have noted the second instance is no longer used as soon as it has been added to the first instance. To save some memory you may want to unset it.

When you are scanning for directories you may additionally want to use only one defined language. The predefined resources for example are available in more than 10 languages. But your application is not available in all of those language. Therefor you can also add only one language from the second adapter.

<?php
$translate
->addTranslation(
    array(
        
'content' => $translate_second,
        
'locale'  => 'en'
    
)
);

This allows you still to scan through the directories and still add only those languages which are relevant for your application.

Zend Framework