Mvc\I18n\Translator -> setLocale #5942

Merged
merged 3 commits into from Mar 11, 2014

Projects

None yet

5 participants

@robintema

Hi,
I have upgraded to 2.3 dev from 2.2.6 to test. It seems that the translator does not have the setLocale method anymore. Is this a known BC break?
Here is the error I got:

2014/03/11 04:01:45 [error] 1288#0: *1385 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught exception 'Zend\Mvc\Exception\BadMethodCallException' with message 'Unable to call method "setLocale"; does not exist in translator' in /vagrant/vendor/zendframework/zendframework/library/Zend/Mvc/I18n/Translator.php:47
Stack trace:
#0 /vagrant/module/BAppBase/Module.php(121): Zend\Mvc\I18n\Translator->__call('setLocale', Array)
#1 /vagrant/module/BAppBase/Module.php(121): Zend\Mvc\I18n\Translator->setLocale('en')
#2 [internal function]: BAppBase\Module->onBootstrap(Object(Zend\Mvc\MvcEvent))
#3 /vagrant/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#4 /vagrant/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('bootstrap', Object(Zend\Mvc\MvcEvent), Array)

Regards,
Norbert.

@robintema

This happens if I try to set the locale for the translator service in the modules bootstrap event:

$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator->setLocale('en');
@samsonasik
Contributor

It just work on mine. can you show the 'service_manager' key on your ./module/Application/config/module.config.php ? have you use latest zendskeletonapplication ?

@weierophinney
Member

@robintema Starting in 2.3.0, if you do not have any configuration defined under the translator configuration key, you will get a Zend\Mvc\I18n\DummyTranslator instance. This instance only implements the methods defined in Zend\I18n\Translator\TranslatorInterface, which include only translate() and translatePlural().

The setLocale() method of Zend\I18n\Translator\Translator is intended for setting the default locale, but is not a required part of the interface as the interface methods allow passing the locale as a parameter.

However, it's sounding like, at a minimum, we should have the DummyTranslator have a setLocale() method that acts as a no-op to ensure backwards compatibility.

@weierophinney
Member

Ping @DASPRiD -- please see previous comments in this issue.

@robintema

@samsonasik I have checked the new dev version on a project I am working on and not on a skeleton app.

This is my service config which worked fine until the upgrade:

'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translator' => 'MvcTranslator',
        ),
    )

@weierophinney so I have to setup the translator in the config from now on to use the Zend\I18n\Translator\Translator ?
Could you please tell me or send me a link about why was this change implemented?
I just want to understand it.

@weierophinney
Member

@robintema I'm not arguing that -- I'm actually explaining why it doesn't work. :) You don't have anything configured for a translator, so the DummyTranslator is getting selected. As such, there is no setLocale() method. I'm trying to get hold of @DASPRiD to see if he objects to putting that method in the DummyTranslator as a no-op, in order to keep BC.

@DASPRiD
Member
DASPRiD commented Mar 11, 2014

@robintema Have you configured the translator programmaticaly instead via configuration?

@weierophinney
Member

@DASPRiD I'm working up a PR. Returning the DummyTranslator by default is going to be a no-go, because previously, we returned an Zend\I18n\Translator\Translator instance, even if no configuration was present -- which allowed developers to configure it programmatically instead of via configuration.

@weierophinney weierophinney [#5942] Ensure TranslatorServiceFactory remains BC with < 2.3.0
- Only use the `DummyTranslator` if `ext/intl` is disabled, or if
  `'translator' => false` configuration is provided
- If no translator configuration is provided, and ext/intl is enabled,
  use a `Zend\I18n\Translator\Translator` instance to seed the
  `Zend\Mvc\I18n\Translator`.
9be9748
@weierophinney weierophinney restored the weierophinney:hotfix/mvc-translator-bc branch Mar 11, 2014
@weierophinney
Member

@DASPRiD Converted this issue to a pull request. The PR does the following:

  • If a Zend\I18n\Translator\TranslatorInterface service is configured, seed the MvcTranslator instance with that service.
  • Next, if ext\intl is disabled, seed the MvcTranslator instance with a DummyTranslator.
  • If configuration exists:
    • if translator => false, seed the MvcTranslator instance with a DummyTranslator
    • otherwise, create an I18n translator instance from configuration, and seed it to the MvcTranslator instance
  • Finally, if no configuration exists, and ext/intl is enabled, seed an MvcTranslator instance with an unconfigured I18n translator instance.

The above retains previous behavior, and makes usage of the DummyTranslator opt-in. While I now that your goals was to make the DummyTranslator the default if no translator was configured, this was not backwards compatible due to how the translator service factory worked previously.

@weierophinney weierophinney added this to the 2.3.0 milestone Mar 11, 2014
@weierophinney weierophinney Use --prefer-source flag for composer
- so that travis will be able to build properly
4482fa0
@DASPRiD
Member
DASPRiD commented Mar 11, 2014

That behaviour looks okay to me, approved.

@weierophinney weierophinney [#5942] skip some tests when ext/intl is not present
- When @ralphschindler ran using a PHP version that did not have
  ext/intl, these two tests failed. They should not run in that
  situation.
a370817
@ralphschindler ralphschindler added a commit that referenced this pull request Mar 11, 2014
@ralphschindler ralphschindler Merge #5942
Merge branch 'hotfix/mvc-translator-bc' of git://github.com/weierophinney/zf2 into hotfix/5942

* 'hotfix/mvc-translator-bc' of git://github.com/weierophinney/zf2:
  [#5942] skip some tests when ext/intl is not present
  Use --prefer-source flag for composer
  [#5942] Ensure TranslatorServiceFactory remains BC with < 2.3.0
a1fb2c2
@ralphschindler ralphschindler added a commit that referenced this pull request Mar 11, 2014
@ralphschindler ralphschindler Merging #5942
Merge branch 'hotfix/5942' into develop

* hotfix/5942:
  [#5942] skip some tests when ext/intl is not present
  Use --prefer-source flag for composer
  [#5942] Ensure TranslatorServiceFactory remains BC with < 2.3.0
f4fb3ec
@ralphschindler ralphschindler merged commit a370817 into zendframework:develop Mar 11, 2014
@weierophinney weierophinney deleted the weierophinney:hotfix/mvc-translator-bc branch Mar 11, 2014
@robintema

@DASPRiD yes, the translator is configured programmaticaly.
@weierophinney thank you. I will make more tests and report if I find anything.

@weierophinney weierophinney pushed a commit to zendframework/zend-i18n-resources that referenced this pull request May 28, 2015
@ralphschindler ralphschindler Merge zendframework/zendframework#5942
Merge branch 'hotfix/mvc-translator-bc' of git://github.com/weierophinney/zf2 into hotfix/5942

* 'hotfix/mvc-translator-bc' of git://github.com/weierophinney/zf2:
  [zendframework/zendframework#5942] skip some tests when ext/intl is not present
  Use --prefer-source flag for composer
  [zendframework/zendframework#5942] Ensure TranslatorServiceFactory remains BC with < 2.3.0
4f9a730
@weierophinney weierophinney pushed a commit to zendframework/zend-i18n-resources that referenced this pull request May 28, 2015
@ralphschindler ralphschindler Merging zendframework/zendframework#5942
Merge branch 'hotfix/5942' into develop

* hotfix/5942:
  [zendframework/zendframework#5942] skip some tests when ext/intl is not present
  Use --prefer-source flag for composer
  [zendframework/zendframework#5942] Ensure TranslatorServiceFactory remains BC with < 2.3.0
32a5624
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment