在 symfony 2.3 中, service.yml 中的这一行可以找到翻译器
在 service.yml
arguments: [@translator,....
在 serviceFunctions.php 中
public function __construct(Translator $translator,...) {
$this->translator = $translator;
现在我得到错误:
must be an instance of Symfony\Component\Translation\Translator, instance of Symfony\Component\Translation\DataCollectorTranslator given
如何在生产模式下也能在 2.7 的 dev 中访问该服务?
最佳答案
尝试按照以下步骤操作:
类(class):
use Symfony\Component\Translation\TranslatorInterface;
public function __construct(TranslatorInterface $translator) {
$this->translator = $translator;
}
public function yourFunction(){
$this->translator->trans('key', array(), 'yourDomain');
}
服务:
yourService:
class: yourClass
arguments: [@translator]
tags:
- { name : kernel.event_listener, event: kernel.request, method: yourFunction }
我在我的代码中使用它并且它的工作;)
关于symfony2.7 在服务容器中传递翻译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33188288/