简体   繁体   English

在Laravel Translatable BootForms中配置lang

[英]Configure langs in Laravel Translatable BootForms

I'm new to Laravel Translatable BootForms , and I was wondering something. 我是Laravel Translatable BootForms的新手 ,我在想什么。

When I use this code : 当我使用此代码时:

{!!
    TranslatableBootForm::text('Nom', 'name')
        ->required()
!!}

The render is as follows : 渲染如下:

我不记得已配置的语言列表

I don't know where this language list comes from. 我不知道该语言列表的来源。

I only want to list some languages specified in my database, as I do with this workaround : 我只想列出数据库中指定的某些语言,就像使用此解决方法一样:

@foreach($availableLangs as $availableLang)
    {!!
        TranslatableBootForm::text('Nom', 'name')
        ->renderLocale($availableLang['locale'])
    !!}
@endforeach

Which gives me this : 这给了我这个:

预期的渲染

My two questions are : 我的两个问题是:

  • Where does this language list come from ? 该语言列表从何而来?
  • How can I replace it by my own language list ? 如何用自己的语言列表替换它?

Answering the first question may lead to an automatic answer for the second, though) 回答第一个问题可能会自动回答第二个问题)

In Laravel, you should always try to read the Service Providers, they provide important clues about the project structures. 在Laravel中,您应该始终尝试阅读服务提供商,它们提供了有关项目结构的重要线索。 Let's try to follow the trail of the function calls: 让我们尝试遵循函数调用的轨迹:

TranslatableBootForm is a facade and it resolves to and instance of translatable-bootform from the Service Container according to this line: TranslatableBootForm是一个外观,它根据以下translatable-bootform从服务容器解析为translatable-bootform实例:

protected static function getFacadeAccessor() { return 'translatable-bootform'; }

Now, in the file TranslatableBootFormsServiceProvider.php we can see that translatable-bootform is an instance of TranslatableBootForm . 现在,在文件TranslatableBootFormsServiceProvider.php我们可以看到translatable-bootformTranslatableBootForm一个实例。 So when you call TranslatableBootForm::text , you will be using the Facade which resolves to an instance of TranslatableBootForm 因此,当您调用TranslatableBootForm::text ,将使用Facade解析为TranslatableBootForm的实例。

Opening the TranslatableBootForm class, we cannot find the text method, so there should be a __call method. 打开TranslatableBootForm类,我们找不到text方法,因此应该有一个__call方法。 The __call method always returns whatever is returned from the method render . __call方法始终返回从render方法返回的内容。 So that's where the action is happening. 这就是行动发生的地方。

Reading the code there, you will find that it gets the locales from a method called locales and it will intersect it with the func_get_args() function to get whatever languages you pass to it. 在这里阅读代码,您会发现它是从名为locales的方法获取语言locales ,并将其与func_get_args()函数相交以获取传递给它的任何语言。 So renderLocale or simply render will do the same thing. 因此renderLocale或简单地render会做同样的事情。

The method locales just returns an array which is by default empty in the class. 方法locales仅返回一个数组,该数组在类中默认为空。 If we return back to the TranslatableBootFormsServiceProvider we will see that there's an important line: 如果我们回到TranslatableBootFormsServiceProvider我们将看到一条重要的线:

$formBuilder->setLocales($this->getLocales());

Which gets the locales from Translatable\\TranslatableWrapper which is just a wrapper around this file in another package: https://github.com/dimsav/laravel-translatable/blob/master/src/Translatable/Translatable.php 它从Translatable\\TranslatableWrapper获取语​​言环境,而该语言只是另一个文件中该文件的包装器: https : //github.com/dimsav/laravel-translatable/blob/master/src/Translatable/Translatable.php

Looking at the configuration file in the laravel-translatable package, we can see the languages: https://github.com/dimsav/laravel-translatable/blob/master/src/config/translatable.php 查看laravel-translatable软件包中的配置文件,我们可以看到以下语言: https : //github.com/dimsav/laravel-translatable/blob/master/src/config/translatable.php

Solutions 解决方案

Now, you can simply copy the file translatable.php in your config folder and set your locales. 现在,您只需在config文件夹中复制文件translatable.php并设置区域设置即可。

Or, you create a new service provider MyTranslatableBootFormsServiceProvider 或者,您创建一个新的服务提供程序MyTranslatableBootFormsServiceProvider

class MyTranslatableBootFormsServiceProvider extends TranslatableBootFormsServiceProvider 
{

    /**
     * Get Translatable's locales.
     * 
     * @return array
     */
    protected function getLocales()
    {
        // You can return a config key
        // return config('yourconfig.locales');
        // Or directly the array containing the languages
        return ['en', 'fr', 'nl'];

    }
}

Then, you will use this provider in your config/app.php instead of the original TranslatableBootFormsServiceProvider 然后,您将在config/app.php使用此提供程序,而不是原始的TranslatableBootFormsServiceProvider

Disclaimer: I didn't try the code, you might have a bug, but you get the idea now how to find your way around Laravel packages. 免责声明:我没有尝试使用代码,您可能会遇到一个错误,但是现在您有了想法,可以找到如何使用Laravel软件包的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM