Symfony: LoggingTranslator vs Translator

那年仲夏 提交于 2019-12-10 03:09:23

问题


I want to inject my translations string into a service, so I used this in the service definition:

arguments: [@doctrine.orm.entity_manager, @translator]

I used this in the constructor:

public function __construct(\Doctrine\ORM\EntityManager $entityManager, \Symfony\Component\Translation\Translator $translator)

But I get this error:

.... __construct() must be an instance of Symfony\Component\Translation\Translator, instance of Symfony\Component\Translation\LoggingTranslator given...

What is the difference between the two?


回答1:


In according with the news announcement, from the version 2.6 the translator component is defined as service like translator.default.

So change your service definition:

arguments: [@doctrine.orm.entity_manager, @translator]

with

arguments: [@doctrine.orm.entity_manager, @translator.default]



回答2:


Symfony 2.6 introduced missing translations logging and for this the "translator" service alias was replaced by some kind of proxy to the real translator class.

As said in the other (and currently accepted) answer, the real translator class is now on the "translator.default" service. But using this service instead of "translator" will disable this new Symfony feature, so you may want to avoid that.

To fix your issue and still have access to the new features, change the code of your constructor to accept any implementation of TranslatorInterface :

public function __construct(\Doctrine\ORM\EntityManager $entityManager, \Symfony\Component\Translation\TranslatorInterface $translator)


来源:https://stackoverflow.com/questions/29162346/symfony-loggingtranslator-vs-translator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!