I'm new to Laravel Translatable BootForms , and I was wondering something.
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 :
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. 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:
protected static function getFacadeAccessor() { return 'translatable-bootform'; }
Now, in the file TranslatableBootFormsServiceProvider.php
we can see that translatable-bootform
is an instance of TranslatableBootForm
. So when you call TranslatableBootForm::text
, you will be using the Facade
which resolves to an instance of TranslatableBootForm
Opening the TranslatableBootForm
class, we cannot find the text
method, so there should be a __call
method. The __call
method always returns whatever is returned from the method 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. So renderLocale
or simply render
will do the same thing.
The method locales
just returns an array which is by default empty in the class. If we return back to the TranslatableBootFormsServiceProvider
we will see that there's an important line:
$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
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
Now, you can simply copy the file translatable.php
in your config
folder and set your locales.
Or, you create a new service provider 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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.