Working with multiple translation sources
- Posted by thomasw at 00:46:24 // //
- Zend Framework, I18N, Translate
Sometimes, when working with translation, you can come into a situation where you need to use multiple adapters.
Especially when you want to use the new resources, which are pre-translated validation messages and are available since 1.10.
There is a simple way how this can be archived by joining two translations. How does this work?
First, you need to have two translation sources:
$translation = new Zend_Translate(
'gettext',
'\path\to\translations\',
'en',
array('scan' => Zend_Translate::LOCALE_DIRECTORY)
);
$translation_addme = new Zend_Translate(
'array',
'\resources\languages\',
'en',
array('scan' => Zend_Translate::LOCALE_DIRECTORY)
);
In our example we used array resources and a gettext adapter.
Now we combine them to one translation, simply by:
$translation->addTranslation($translation_addme);
That’s it… now our first instance holds all translations from the second instance.
You can combine as much instances as you want.
The only limitation is, that you should not try to combine same keys by using different translations for these keys. This can (of course) lead to unexpected behaviour when you define two different translations for the same key.
To save resources you can also combine only one language from the second adapter.
$translation->addTranslation($translation_addme, 'en');
Note that this feature is available as with 1.10.3. There was no reason for me to wait for 2.0 for this feature. ;-)
I hope you find this feature useful for your daily work.
When not, you should probably take the I18n webinar about translation. ;-)
I will give a repetition in a few weeks.
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
