Package Data | |
---|---|
Maintainer Username: | Tarpsvo |
Maintainer Contact: | tarvoreinpalu@gmail.com (Tarvo Reinpalu) |
Package Create Date: | 2019-12-20 |
Package Last Update: | 2024-10-19 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-10 15:01:09 |
Package Statistics | |
---|---|
Total Downloads: | 220,121 |
Monthly Downloads: | 9,760 |
Daily Downloads: | 30 |
Total Stars: | 199 |
Total Watchers: | 7 |
Total Forks: | 56 |
Total Open Issues: | 13 |
This Laravel Nova allows you to make any input field spatie/laravel-translatable
compatible and localisable.
laravel/nova: ^2.9 || ^3.0
spatie/laravel-translatable: ^4.0 || ^5.0 || ^6.0
spatie/laravel-translatable
support)Image
and File
resolveUsing
fillUsing
displayUsing
(might be fixed eventually)Firstly, set up spatie/laravel-translatable.
Install the package in a Laravel Nova project via Composer:
# Install nova-translatable
composer require outl1ne/nova-translatable
# Publish configuration (optional, but useful for setting default locales)
php artisan vendor:publish --tag="nova-translatable-config"
Call ->translatable()
on any field, like so:
// Any Nova field
Text::make('Name')
->rules('required', 'min:2')
->translatable(),
// Any third-party input field
Multiselect::make('Football teams')
->rules('required')
->translatable(),
// Optionally pass custom locales on a per-field basis
Number::make('Population')
->translatable([
'en' => 'English',
'et' => 'Estonian',
]),
It's possible to define locale specific validation rules.
To do so, add the ->rulesFor()
on your field and the HandlesTranslatable
trait to your Nova resource.
->rulesFor
accepts array|string|callable
locales and array|callable
rules.
use Outl1ne\NovaTranslatable\HandlesTranslatable;
class Product extends Resource
{
use HandlesTranslatable;
public function fields(Request $request)
{
return [
Text::make(__('Name'), 'name')
->sortable()
->translatable()
->rules(['max:255'])
->rulesFor('en', [
'required',
])
->rulesFor(['en', 'et'], function ($locale) {
return ["unique:products,name->$locale{{resourceId}}"];
}),
];
}
}
max: name.*
required: name.en
unique: name.en & name.et
You can define default locales for all the translatable
fields in the config file. The config file can be published using:
php artisan vendor:publish --tag="nova-translatable-config"
The configuration option fill_other_locales_from
allows you to pre-fill other locales from just one locale. This requires the resources to also have the HandlesTranslatable
trait.
If you don't want to display the locale select next to each field, you can set the display_type
to none
and add a Outl1ne\NovaTranslatable\Fields\LocaleSelect
field to your Nova resource. This will render a single select for all fields.
When using this field inside a BelongsToMany as a pivot field with ->allowDuplicateRelations()
and you want to filter out exact matches using the NotExactlyAttached
rule, use the BelongsToManyTranslatable
field instead of the regular BelongsToMany
.
This project is open-sourced software licensed under the MIT license.