Thursday
25
March
2010

Working with multiple translation sources

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

Back to top
  1. Anselm

    Friday, March 26, 2010 - 00:11:16

    Nice! But
    >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.

    wouldn’t we want to have an exception here?

  2. thomasw

    Friday, March 26, 2010 - 00:43:52

    No, you would not

  3. Haruka

    Friday, March 26, 2010 - 16:40:12

    I find this feature really helpful! Thanks for adding it! :D

  4. Matthias Zeis

    Saturday, March 27, 2010 - 08:36:28

    @1. Anselm:
    There are valid cases where you do want to define the same key several times.
    An example: The eCommerce platform Magento provides translations using CSV files. This is translation source number 1. When you customise your shop, Magento provides a handy function called “inline translation” to replace the default text with your own wording. These strings are stored in the database and represent translation source number 2. With addTranslation(), you can first load the default translation and then override it with the custom translation.
    (I hope this is the way addTranslation() works. If not, in my opinion it definitely should ;-)).

  5. thomasw

    Saturday, March 27, 2010 - 13:01:06

    That’s correct… the second adapter overwrites previous set translations.

    As described before this allows to overwrite default translations with new defined ones and is expected behaviour. This worked already before this release. New is, that you can now add also adapters and not only files or directories.

  6. Marco

    Monday, May 31, 2010 - 21:26:59

    Hi Thomas,

    I was playing around with the nice i18n features, discovering the great possibilities of Zend_Form combined with configuration put in Zend_Config combined with a custom tmx file combined with custom Validation Messages.

    unfortunately,
    Zend_Validate_Abstract::setDefaultTranslator($translator);
    Zend_Form::setDefaultTranslator($translatorValidation);
    seemed to overwrite each other.

    your suggestion
    $1->addTranslation($2, ‘en’);

    brought the solution of course! :)
    thanx alot, that saved me quite some time.

  7. pramendra

    Saturday, March 19, 2011 - 07:33:55

    it works for me, thanks alot for the great post

  8. Rent Cottages

    Sunday, December 18, 2011 - 05:52:37

    I don’t understand translations

  9. ivr system

    Wednesday, April 25, 2012 - 12:52:18

    There is a simple way how this can be archived by joining two translations. How does this work?

  10. voice logger

    Wednesday, April 25, 2012 - 13:33:20

    This is translation source number 1. When you customise your shop,

  11. conference bridge

    Wednesday, April 25, 2012 - 13:34:56

    i working with translation, you can come into a situation where you need to use multiple adapters

Add comment

Fill out the form below to add your own comments

User data




Add your comment