This Laravel Nova allows you to make any input field using the spatie/laravel-translatable
package compatible and localisable.
This package is inspired by optimistdigital/nova-translatable.
laravel/nova: ^3.0
spatie/laravel-translatable: ^4.0
You first need to set up the spatie/laravel-translatable package.
The package can be installed via composer:
composer require ngiraud/nova-translatable-v2
The package will automatically register the service provider.
You can publish the config file in order to use custom locales.
php artisan vendor:publish --tag="nova-translatable-config"
This is the contents of the published config file:
return [
/**
* the locales which the `translatable` wrapper will use by default.
*
* can be a:
* - keyed array (['en' => 'english])
* - callable that returns a keyed array
*/
'locales' => ['en' => 'English'],
];
Simply call ->translatable()
on a Nova field:
Text::make('Name')
->rules('required', 'min:2')
->translatable()
Optionally you can pass custom locales:
Number::make('Population')
->translatable([
'en' => 'English',
'et' => 'Estonian',
])
You can add validation rules for a single locale.
To do so, add the ->rulesFor()
on your field and the HasTranslatable
trait to your Nova resource:
class Product extends Resource
{
use \NGiraud\NovaTranslatable\HasTranslatable;
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make(__('Name'), 'name')
->sortable()
->translatable()
->rules(['max:255'])
->rulesFor('fr', [
'required',
'unique:products,sku->fr,{{resourceId}}'
])
}
}
In this example, the rule max
will be applied for name.*
and the rule required
will be applied for name.fr
.
- Adding tests to the package
Please see CHANGELOG for more information about recent changes.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email contact@ngiraud.me instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.