Search code examples
event-handlingzend-framework2zend-translate

Listen to missingTranslation event


I would like to collect strings, that has not been translated because translation is missing.

  1. I would like to enable this event for all translations. Now I can do this by setting it right before translating, but this should be a global setting.
  2. I would like to have a function called if this event is triggered.

Am I able to do this?

Thanks for any help!

(This is Zend Framework 2.2 and Zend\I18n.)


Solution

  • ZF2 actually has this event built-in in the Translator. By default the translator component is not triggering any events, you have to explicitly enable it.

    In you module.config.php

    return array(
        'translator'
            'event_manager_enabled' => true
        )
    );
    

    Now you can attach a listener to the event.

    $translator = $serviceManager->get('MvcTranslator');
    $translator->getEventManager()->attach('missingTranslation', function($event) {
        //Do some stuff when translation is missing
        var_dump($event->getParams());
    });