In symfony 2.3 it was this line in service.yml to get to the translator
In service.yml
arguments: [@translator,....
in serviceFunctions.php
public function __construct(Translator $translator,...) {
$this->translator = $translator;
Now I get the error:
must be an instance of Symfony\Component\Translation\Translator, instance of Symfony\Component\Translation\DataCollectorTranslator given
How can I get to the service in 2.7 in dev also in production mode?
Try to folow this steps :
Class:
use Symfony\Component\Translation\TranslatorInterface;
public function __construct(TranslatorInterface $translator) {
$this->translator = $translator;
}
public function yourFunction(){
$this->translator->trans('key', array(), 'yourDomain');
}
Service:
yourService:
class: yourClass
arguments: [@translator]
tags:
- { name : kernel.event_listener, event: kernel.request, method: yourFunction }
I use this in my code and it's work ;)
Try using the interface rather than the actual translator class. By using interfaces as type hint you can use anything as long as it fits the interface, for instance you could pass in a debug translator in development with a regular one in production without needing to change your code.
use Symfony\Component\Translation\TranslatorInterface;
...
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With