From e447db35badbeee6854420129199f46b138dcbf9 Mon Sep 17 00:00:00 2001 From: Ben Scholzen Date: Fri, 5 Apr 2013 14:02:02 +0200 Subject: [PATCH 1/3] Add enabling event manager in translator to factory --- src/Translator/Translator.php | 5 +++++ test/Translator/TranslatorTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index eaac68e9..0b3345fc 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -213,6 +213,11 @@ public static function factory($options) } } + // event manager enabled + if (isset($options['event_manager_enabled']) && $options['event_manager_enabled']) { + $translator->enableEventManager(); + } + return $translator; } diff --git a/test/Translator/TranslatorTest.php b/test/Translator/TranslatorTest.php index 806acac1..9b0972bc 100644 --- a/test/Translator/TranslatorTest.php +++ b/test/Translator/TranslatorTest.php @@ -247,6 +247,17 @@ public function testEnableDisableEventManger() $this->assertFalse($this->translator->isEventManagerEnabled()); } + public function testEnableEventMangerViaFactory() + { + $translator = Translator::factory(array( + 'event_manager_enabled' => true + )); + $this->assertTrue($translator->isEventManagerEnabled()); + + $translator = Translator::factory(array()); + $this->assertFalse($translator->isEventManagerEnabled()); + } + public function testMissingTranslationEvent() { $actualEvent = null; From f570f54caa9c0ddae37393076794f32b094e4882 Mon Sep 17 00:00:00 2001 From: Ben Scholzen Date: Sun, 28 Apr 2013 22:20:34 +0200 Subject: [PATCH 2/3] Minot changes to event manager integration in translator --- src/Translator/Translator.php | 18 ++++++++++++++---- test/Translator/TranslatorTest.php | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 0b3345fc..dfd65889 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -25,6 +25,16 @@ */ class Translator { + /** + * Event fired when the translation for a message is missing. + */ + const EVENT_MISSING_TRANSLATION = 'missingTranslation'; + + /** + * Event fired when no messages were loaded for a locale/text-domain combination. + */ + const EVENT_NO_MESSAGES_LOADED = 'noMessagesLoaded'; + /** * Messages loaded by the translator. * @@ -427,7 +437,7 @@ protected function getTranslatedMessage( if ($this->isEventManagerEnabled()) { $this->getEventManager()->trigger( - 'getTranslatedMessage.missing-translation', + self::EVENT_MISSING_TRANSLATION, $this, array( 'message' => $message, @@ -548,7 +558,7 @@ protected function loadMessages($textDomain, $locale) if (!$messagesLoaded) { if ($this->isEventManagerEnabled()) { $this->getEventManager()->trigger( - 'loadMessages.no-messages-loaded', + self::EVENT_NO_MESSAGES_LOADED, $this, array( 'locale' => $locale, @@ -696,8 +706,8 @@ public function setEventManager(EventManagerInterface $events) { $events->setIdentifiers(array( __CLASS__, - get_called_class(), - 'module_manager', + get_class($this), + 'translator', )); $this->events = $events; return $this; diff --git a/test/Translator/TranslatorTest.php b/test/Translator/TranslatorTest.php index 9b0972bc..74c0d99c 100644 --- a/test/Translator/TranslatorTest.php +++ b/test/Translator/TranslatorTest.php @@ -263,7 +263,7 @@ public function testMissingTranslationEvent() $actualEvent = null; $this->translator->enableEventManager(); - $this->translator->getEventManager()->attach('getTranslatedMessage.missing-translation', function(EventInterface $event) use (&$actualEvent) { + $this->translator->getEventManager()->attach(Translator::EVENT_MISSING_TRANSLATION, function(EventInterface $event) use (&$actualEvent) { $actualEvent = $event; }); @@ -291,7 +291,7 @@ public function testNoMessagesLoadedEvent() $actualEvent = null; $this->translator->enableEventManager(); - $this->translator->getEventManager()->attach('loadMessages.no-messages-loaded', function(EventInterface $event) use (&$actualEvent) { + $this->translator->getEventManager()->attach(Translator::EVENT_NO_MESSAGES_LOADED, function(EventInterface $event) use (&$actualEvent) { $actualEvent = $event; }); From 256b9bcf92fb09ab1e23d51aff2e27574dab09b1 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 29 Apr 2013 13:37:51 -0500 Subject: [PATCH 3/3] [zendframework/zf2#4187] Intercept results of events - If a string value is returned from the `EVENT_MISSING_TRANSLATION` event, we should use it as the translation. - If a `TextDomain` is returned from the `EVENT_NO_MESSAGES_LOADED` event, we should use it for the text domain. --- src/Translator/Translator.php | 30 ++++++++++++++---- test/Translator/TranslatorTest.php | 49 ++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/Translator/Translator.php b/src/Translator/Translator.php index 7e0ded70..5b2c1566 100644 --- a/src/Translator/Translator.php +++ b/src/Translator/Translator.php @@ -443,15 +443,22 @@ protected function getTranslatedMessage( } if ($this->isEventManagerEnabled()) { - $this->getEventManager()->trigger( + $results = $this->getEventManager()->trigger( self::EVENT_MISSING_TRANSLATION, $this, array( 'message' => $message, 'locale' => $locale, 'text_domain' => $textDomain, - ) + ), + function ($r) { + return is_string($r); + } ); + $last = $results->last(); + if (is_string($last)) { + return $last; + } } return null; @@ -563,19 +570,30 @@ protected function loadMessages($textDomain, $locale) $messagesLoaded |= $this->loadMessagesFromFiles($textDomain, $locale); if (!$messagesLoaded) { + $discoveredTextDomain = null; if ($this->isEventManagerEnabled()) { - $this->getEventManager()->trigger( + $results = $this->getEventManager()->trigger( self::EVENT_NO_MESSAGES_LOADED, $this, array( 'locale' => $locale, 'text_domain' => $textDomain, - ) + ), + function ($r) { + return ($r instanceof TextDomain); + } ); + $last = $results->last(); + if ($last instanceof TextDomain) { + $discoveredTextDomain = $last; + } } - $this->messages[$textDomain][$locale] = null; - } elseif ($cache !== null) { + $this->messages[$textDomain][$locale] = $discoveredTextDomain; + $messagesLoaded = true; + } + + if ($messagesLoaded && $cache !== null) { $cache->setItem($cacheId, $this->messages[$textDomain][$locale]); } } diff --git a/test/Translator/TranslatorTest.php b/test/Translator/TranslatorTest.php index 74c0d99c..3f1cb614 100644 --- a/test/Translator/TranslatorTest.php +++ b/test/Translator/TranslatorTest.php @@ -286,6 +286,28 @@ public function testMissingTranslationEvent() $this->assertNull($actualEvent); } + public function testListenerOnMissingTranslationEventCanReturnString() + { + $trigger = null; + $doNotTriger = null; + + $this->translator->enableEventManager(); + $this->translator->getEventManager()->attach(Translator::EVENT_MISSING_TRANSLATION, function(EventInterface $event) use (&$trigger) { + $trigger = true; + }); + $this->translator->getEventManager()->attach(Translator::EVENT_MISSING_TRANSLATION, function(EventInterface $event) { + return 'EVENT TRIGGERED'; + }); + $this->translator->getEventManager()->attach(Translator::EVENT_MISSING_TRANSLATION, function(EventInterface $event) use (&$doNotTrigger) { + $doNotTrigger = true; + }); + + $result = $this->translator->translate('foo', 'bar', 'baz'); + $this->assertTrue($trigger); + $this->assertEquals('EVENT TRIGGERED', $result); + $this->assertNull($doNotTrigger); + } + public function testNoMessagesLoadedEvent() { $actualEvent = null; @@ -312,4 +334,31 @@ public function testNoMessagesLoadedEvent() $this->translator->translate('foo', 'bar', 'baz'); $this->assertNull($actualEvent); } + + public function testListenerOnNoMessagesLoadedEventCanReturnTextDomainObject() + { + $trigger = null; + $doNotTrigger = null; + $textDomain = new TextDomain(array( + 'foo' => 'BOOYAH', + )); + + $this->translator->enableEventManager(); + $events = $this->translator->getEventManager(); + $events->attach(Translator::EVENT_NO_MESSAGES_LOADED, function(EventInterface $event) use (&$trigger) { + $trigger = true; + }); + $events->attach(Translator::EVENT_NO_MESSAGES_LOADED, function(EventInterface $event) use ($textDomain) { + return $textDomain; + }); + $events->attach(Translator::EVENT_NO_MESSAGES_LOADED, function(EventInterface $event) use (&$doNotTrigger){ + $doNotTrigger = true; + }); + + $result = $this->translator->translate('foo', 'bar', 'baz'); + + $this->assertTrue($trigger); + $this->assertNull($doNotTrigger); + $this->assertEquals('BOOYAH', $result); + } }