8

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?

Matteo
  • 37,680
  • 11
  • 100
  • 115
b85411
  • 9,420
  • 15
  • 65
  • 119

2 Answers2

13

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]
Matteo
  • 37,680
  • 11
  • 100
  • 115
  • Hi @b85411 if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Matteo Mar 22 '15 at 14:26
8

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)
Derek
  • 1,826
  • 18
  • 25
  • 1
    This is the better answer. I would say to always use `@translator` (never the .default variant) and enable/disable logging as you want it. Then use the `\Symfony\Component\Translation\TranslatorInterface` to typehint in services. – Rein Baarsma Dec 29 '17 at 07:39