douzi1117 2017-03-21 10:55
浏览 39
已采纳

自定义服务中的Symfony转换无法正常工作

I had a custom service and the translation inside it doesn't work. Symfony Version 3.2.3

Constructor:

public function __construct(TranslatorInterface $translator){
    $this->translator = $translator;
}

Method:

public function iConvertDateTimeString(){
     ...
if ($dateTime === false) {
    throw new \InvalidArgumentException(
        $this->translator->trans(
            'elasticsearch.exception.service.date.wrong_format'
        ),
        1488549113
    );
}

services.yml:

services:
    elastic_search.elasticsearch:
        class: MyCompany\ElasticSearchBundle\Services\ElasticSearchService
        arguments: [ "@translator" ]

exceptions.de_DE.yml:

elasticsearch:
    exception:
        service:
            date:
                wrong_format: Der DateTime String hat das falsche Format. Benutze 'Y-m-d H:i:s' oder 'Y-m-d H:i:s.u'.

Output:

elasticsearch.exception.service.date.wrong_format
500 Internal Server Error - InvalidArgumentException

Whats the problem? Allready cleared the cache for prod and dev with warm-up after it. Translation are found and displayed in console.

  • 写回答

1条回答 默认 最新

  • dqysi86208 2017-03-21 10:58
    关注

    Replace

    $this->translator->trans(
            'elasticsearch.exception.service.date.wrong_format'
    )
    

    With

    $this->translator->trans(
            'elasticsearch.exception.service.date.wrong_format', array(), 'exceptions'
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?