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'
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模数学建模需要
  • ¥20 Java Sound Api 调用Mixer.getLine偶现Line unsupported错误。
  • ¥15 使用cuda加速opencv运算但是报错AttributeError: module 'cv2.cuda' has no attribute 'getCudaEnabledDeviceCount'
  • ¥15 java输入输出异常
  • ¥15 三子连珠对弈小游戏制作
  • ¥15 C++扑克牌游戏的编程
  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格