LaravelPackages.net
Acme Inc.
Toggle sidebar
kraenkvisuell/nova-astrotranslatable

An Astrotomic Translatable extension for Laravel Nova.

11.506
6
v2.1.0
About kraenkvisuell/nova-astrotranslatable

kraenkvisuell/nova-astrotranslatable is a Laravel package for an astrotomic translatable extension for laravel nova.. It currently has 6 GitHub stars and 11.506 downloads on Packagist (latest version v2.1.0). Install it with composer require kraenkvisuell/nova-astrotranslatable. Discover more Laravel packages by kraenkvisuell or browse all Laravel packages to compare alternatives.

Last updated

Nova Astrotomic Translatable

This Laravel Nova field allows you to make any input field astrotomic/laravel-translatable compatible and localisable.

It is made from a fork from Optimist Digital's Laravel Nova Translatable field, which is tailored for the Spatie Laravel Translatable package - so huge thanks to them, 99% of the work was made by them!

Requirements

  • laravel/nova: ^2.9 || ^3.0
  • astrotomic/laravel-translatable: ^11.0

Features

  • Supports almost all fields (including third party ones)
  • Supports default validation automatically
  • Simple to implement with minimal code changes (after astrotomic/laravel-translatable support)
  • Locale tabs to switch between different locale values of the same field

Known non-working fields

  • Image and File

Limitations

  • The following methods can not be used, as this package uses them internally:
    • resolveUsing
    • fillUsing
    • displayUsing (might be fixed eventually)

Screenshots

Detail View

Form View

Form View w/ Validation Errors

Installation

Firstly, set up astrotomic/laravel-translatable.

Install the package in a Laravel Nova project via Composer:

# Install nova-translatable
composer require kraenkvisuell/nova-astrotranslatable

# 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\NovaAstrotranslatable\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"

Credits

License

This project is open-sourced software licensed under the MIT license.

Comments