代码之家  ›  专栏  ›  技术社区  ›  SaidbakR

配置文件中不存在Laravel类转换器

  •  5
  • SaidbakR  · 技术社区  · 7 年前

    是否有任何方法允许在 配置文件 在里面 应用

    我有一个 风俗 配置文件位于 config/fox-reports.php 我试图设置一个可翻译的配置值,如下所示:

    return [
        'attrs' => [
           'Product' => __('Product Title')
        ]
    ] 
    

    当我跑步时 php artisan config:cache 生成以下错误:

    在容器中。php第729行:

      Class translator does not exist
    
    2 回复  |  直到 3 年前
        1
  •  7
  •   Alexey Mezenin    7 年前

    您不能使用 __() 配置文件中的帮助程序,因为它使用 Translator 班Laravel在周期开始时加载config,此时大多数服务尚未初始化。

        2
  •  0
  •   miken32 Amit D    3 年前

    为了完整起见,为了补充亚历克斯的回答,像这样的翻译应该在以后处理。使用默认值设置配置文件,如果不存在翻译,将使用该值。

    配置/福克斯报告。php

    return [
        'attrs' => [
           'Product' => 'Product Title'
        ]
    ];
    

    然后在 localization JSON files ,例如。

    资源/lang/fr/fr.json

    {
        "Product Title": "Titre de produit"
    }
    

    在控制器中或任何地方,您可以将呼叫包装到 config() 在翻译功能中:

    // not like this:
    // $title = config('foxreports.attrs.Product');
    // but like this:
    $title = __(config('foxreports.attrs.Product'));
    

    如果需要自动本地化工具检测默认值,只需将其添加到某个存根类中,例如。

    <?php
    
    namespace App\Stubs;
    
    class Localization
    {
        public method __construct()
        {
            __('Product Title');
        }
    }
    
    推荐文章