Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'weierophinney-hotfix/translator-service' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Jan 6, 2014
2 parents 5500fc7 + 0c8e358 commit f2111c1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 11 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ version of PHP available to ensure you have the latest security fixes.

Additional updates that may affect existing applications include:

- [#5406](https://github.com/zendframework/zf2/pull/5406) and
[#5689](https://github.com/zendframework/zf2/pull/5689) make the i18n
component optional for the MVC. 5406 does this by introducing a new interface,
`Zend\I18n\Translator\TranslatorInterface` and a `DummyTranslator`
implementation, and altering the MvcTranslator service factory to instantiate
a `DummyTranslator` to inject into the `Zend\Mvc\I18n\Translator` instance.
5689 updates the MVC translator factory to look for one of the following:

- A defined `Zend\I18n\Translator\TranslatorInterface` service; if found,
this will be injected into a `Zend\Mvc\I18n\Translator` instance.
- Defined `translator` configuration; if so, this will be passed to the
`Zend\I18n\Translator\Translator::factory` method, and the result injected
in a `Zend\Mvc\I18n\Translator` instance.
- If no configuration is found, a `DummyTranslator` will be created and injected
into a `Zend\Mvc\I18n\Translator` instance.

- [#5469](https://github.com/zendframework/zf2/pull/5469) adds a new abstract
controller, `Zend\Mvc\Controller\AbstractConsoleController`, for simplifying
the creation of console controllers.
Expand Down
5 changes: 1 addition & 4 deletions library/Zend/Mvc/I18n/DummyTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
namespace Zend\Mvc\I18n;

use Zend\I18n\Translator\TranslatorInterface as I18nTranslatorInterface;
use Zend\Validator\Translator\TranslatorInterface as ValidatorTranslatorInterface;

class DummyTranslator implements
I18nTranslatorInterface,
ValidatorTranslatorInterface
class DummyTranslator implements I18nTranslatorInterface
{
public function translate($message, $textDomain = 'default', $locale = null)
{
Expand Down
23 changes: 18 additions & 5 deletions library/Zend/Mvc/Service/TranslatorServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,35 @@

namespace Zend\Mvc\Service;

use Zend\I18n\Translator\Translator;
use Zend\Mvc\I18n\DummyTranslator;
use Zend\Mvc\I18n\Translator;
use Zend\Mvc\I18n\Translator as MvcTranslator;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Overrides the translator factory from the i18n component in order to
* replace it with the bridge class from this namespace.
*/
class TranslatorServiceFactory
class TranslatorServiceFactory implements FactoryInterface
{
/**
* @param ServiceLocatorInterface $serviceLocator
* @return MvcTranslator
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
if (!$serviceLocator->has('translator')) {
return new DummyTranslator();
if ($serviceLocator->has('Zend\I18n\Translator\TranslatorInterface')) {
return new MvcTranslator($serviceLocator->get('Zend\I18n\Translator\TranslatorInterface'));
}

return new Translator($serviceLocator->get('translator'));
if ($serviceLocator->has('Config')) {
$config = $serviceLocator->get('Config');
if (isset($config['translator']) && !empty($config['translator'])) {
return new MvcTranslator(Translator::factory($config['translator']));
}
}

return new MvcTranslator(new DummyTranslator());
}
}
9 changes: 7 additions & 2 deletions library/Zend/View/HelperPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ public function injectTranslator($helper)
return;
}

if ($locator->has('translator')) {
$helper->setTranslator($locator->get('translator'));
if ($locator->has('Zend\I18n\Translator\TranslatorInterface')) {
$helper->setTranslator($locator->get('Zend\I18n\Translator\TranslatorInterface'));
return;
}

if ($locator->has('Translator')) {
$helper->setTranslator($locator->get('Translator'));
return;
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ZendTest/View/HelperPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,15 @@ public function testIfHelperIsTranslatorAwareAndMvcTranslatorIsUnavailableAndTra
$helper = $this->helpers->get('HeadTitle');
$this->assertSame($translator, $helper->getTranslator());
}

public function testIfHelperIsTranslatorAwareAndBothMvcTranslatorAndTranslatorAreUnavailableAndTranslatorInterfaceIsAvailableItWillInjectTheTranslator()
{
$translator = new Translator();
$services = new ServiceManager();
$services->setService('Zend\I18n\Translator\TranslatorInterface', $translator);
$this->helpers->setServiceLocator($services);

$helper = $this->helpers->get('HeadTitle');
$this->assertSame($translator, $helper->getTranslator());
}
}

0 comments on commit f2111c1

Please sign in to comment.