manogi / nova-translatable
A laravel-translatable extension for Laravel Nova.
Requires
- php: ^8.0.2|^8.1
- laravel/nova: ^4.0
- spatie/laravel-translatable: ^4.0 || ^5.0 || ^6.0
- dev-master
- v2.0.0
- 1.12.0
- 1.11.1
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.1
- 1.9.0
- 1.8.4
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.12
- 1.6.11
- 1.6.10
- 1.6.9
- 1.6.8
- 1.6.7
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- dev-dependabot/npm_and_yarn/minimist-1.2.6
This package is auto-updated.
Last update: 2024-11-06 21:05:50 UTC
README
This Laravel Nova allows you to make any input field spatie/laravel-translatable
compatible and localisable.
Requirements
laravel/nova: ^4.0
spatie/laravel-translatable: ^5.0 || ^6.0
Features
- Supports almost all fields (including third party ones)
- Supports default validation automatically
- Simple to implement with minimal code changes (after
spatie/laravel-translatable
support) - Locale tabs to switch between different locale values of the same field
- Double click on a tab to switch all fields to that locale
- Supports nova-settings package
Known non-working fields
Image
andFile
- Workarounds:
- optimistdigital/nova-media-field
- or any library that uploads images/files using XHR
- Workarounds:
Limitations
- The following methods can not be used, as this package uses them internally:
resolveUsing
fillUsing
displayUsing
(might be fixed eventually)
Screenshots
Installation
Firstly, set up spatie/laravel-translatable.
Install the package in a Laravel Nova project via Composer:
# Install nova-translatable composer require optimistdigital/nova-translatable # Publish configuration (optional, but useful for setting default locales) php artisan vendor:publish --tag="nova-translatable-config"
Usage
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', ]),
Validation
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 KraenkVisuell\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}}"]; }), ]; } }
In this example, rules will be added to the following values
max: name.* required: name.en unique: name.en & name.et
Configuration
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"
Fill other locales from config option
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.
One select for all fields on a page
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 KraenkVisuell\NovaTranslatable\Fields\LocaleSelect
field to your Nova resource. This will render a single select for all fields.
Edge cases
BelongsToMany allowDuplicateRelations corner-case
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
.
Credits
License
This project is open-sourced software licensed under the MIT license.