laravel “ReflectionException”类翻译器不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45506239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:27:18  来源:igfitidea点击:

'ReflectionException' Class translator does not exists

laravellaravel-5localization

提问by Sagar Naliyapara

I'm trying to use localization as follow,

我正在尝试使用本地化如下,

$types = trans('constants.type');

I have created constants.phpfile as follows (which is in config directory) and this are it's language files,

我创建constants.php了如下文件(在 config 目录中),这是它的语言文件,

/resources
    /lang
        /en
            constants.php
        /es
            constants.php

constants.php

常量.php

return ['type' => 'Type'];

same way it translated in other file with same format, but this things gives me following error

它以相同的方式翻译成具有相同格式的其他文件,但是这件事给了我以下错误

Note:I have used this in config directory's file,

注意:我在 config 目录的文件中使用了它,

(1/1) ReflectionException Class translator does not exist

in Container.php (line 729) at ReflectionClass->__construct('translator') in Container.php (line 729) at Container->build('translator') in Container.php (line 608) at Container->resolve('translator') in Container.php (line 575) at Container->make('translator')

(1/1) ReflectionException 类翻译器不存在

在 Container.php (line 729) at ReflectionClass->__construct('translator') in Container.php (line 729) at Container->build('translator') in Container.php (line 608) at Container->resolve( 'translator') 在 Container.php (line 575) at Container->make('translator')

Problem:

问题:

trans()method not working in constants.phpfile which i have created in configdirectory as follow,

trans()方法在constants.php我在config目录中创建的文件中不起作用,如下所示,

/config constants.php

/配置常量.php

回答by ntzm

You can't use trans()in any config files, because they are loaded before the translation service is registered (which is why you are getting Class translator does not exist).

您不能trans()在任何配置文件中使用,因为它们是在注册翻译服务之前加载的(这就是为什么您会得到Class translator does not exist)。