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

Mvc\I18n\Translator -> setLocale #5942

Merged

Conversation

weierophinney
Copy link
Member

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
Copy link
Author

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
Copy link
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
Copy link
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
Copy link
Member

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

@robintema
Copy link
Author

@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
Copy link
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
Copy link
Member

DASPRiD commented Mar 11, 2014

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

@weierophinney
Copy link
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.

…< 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`.
@weierophinney weierophinney restored the hotfix/mvc-translator-bc branch March 11, 2014 15:20
@weierophinney
Copy link
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
- so that travis will be able to build properly
@DASPRiD
Copy link
Member

DASPRiD commented Mar 11, 2014

That behaviour looks okay to me, approved.

- When @ralphschindler ran using a PHP version that did not have
  ext/intl, these two tests failed. They should not run in that
  situation.
ralphschindler pushed a commit that referenced this pull request Mar 11, 2014
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
ralphschindler pushed a commit that referenced this pull request Mar 11, 2014
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
@ralphschindler ralphschindler merged commit a370817 into zendframework:develop Mar 11, 2014
@weierophinney weierophinney deleted the hotfix/mvc-translator-bc branch March 11, 2014 17:43
@robintema
Copy link
Author

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

weierophinney pushed a commit to zendframework/zend-i18n-resources that referenced this pull request May 28, 2015
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
weierophinney pushed a commit to zendframework/zend-i18n-resources that referenced this pull request May 28, 2015
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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants