All Projects → spatie → Nova Translatable

spatie / Nova Translatable

Licence: mit
Making Nova fields translatable

Projects that are alternatives of or similar to Nova Translatable

Laravel Nova Lang
🌌 Language files for Laravel Nova translated into 40+ languages. Feel free to submit your language or update an existing one!
Stars: ✭ 275 (+131.09%)
Mutual labels:  laravel, nova, translations
Nova Mega Filter
Allows you to control the columns and filters shown on any Nova resource index
Stars: ✭ 49 (-58.82%)
Mutual labels:  laravel, nova
Laravel Translatable
It's a Laravel database translations manager
Stars: ✭ 47 (-60.5%)
Mutual labels:  laravel, translations
Nova Custom Email Sender
A Laravel Nova tool that sends ad-hoc email messages from the dashboard.
Stars: ✭ 62 (-47.9%)
Mutual labels:  laravel, nova
Scout Extended
Scout Extended: The Full Power of Algolia in Laravel
Stars: ✭ 330 (+177.31%)
Mutual labels:  laravel, nova
Nova Laravel Update Card
Check if you're running the latest Laravel version right from your Nova dashboard.
Stars: ✭ 34 (-71.43%)
Mutual labels:  laravel, nova
Laravel Nova Permission
A Laravel Nova tool for the Spatie Permission package
Stars: ✭ 59 (-50.42%)
Mutual labels:  laravel, nova
Nova Backup Tool
A Laravel Nova tool to backup your app
Stars: ✭ 260 (+118.49%)
Mutual labels:  laravel, nova
Nova Stripe Theme
A Laravel Nova theme closely resembling Stripe.
Stars: ✭ 71 (-40.34%)
Mutual labels:  laravel, nova
Awesome Nova
🎉 A curated list of awesome things related to Laravel Nova
Stars: ✭ 92 (-22.69%)
Mutual labels:  laravel, nova
Collapsible Resource Manager
A custom sidebar menu with collapsible groups
Stars: ✭ 100 (-15.97%)
Mutual labels:  laravel, nova
Nova Permission
A Laravel Nova tool for Spatie's laravel-permission library
Stars: ✭ 294 (+147.06%)
Mutual labels:  laravel, nova
Laravel Translations Loader
Webpack loader to import Laravel translation files (PHP or JSON) into your JS bundle as JSON.
Stars: ✭ 109 (-8.4%)
Mutual labels:  laravel, translations
Nova Time Field
Laravel Nova Time Field
Stars: ✭ 45 (-62.18%)
Mutual labels:  laravel, nova
Nova Tabs
Laravel Nova Tabs Package
Stars: ✭ 265 (+122.69%)
Mutual labels:  laravel, nova
Nova Route Viewer
Route viewer tool for Laravel Nova
Stars: ✭ 54 (-54.62%)
Mutual labels:  laravel, nova
Nova Tags Field
A tags field to use in your Nova apps
Stars: ✭ 204 (+71.43%)
Mutual labels:  laravel, nova
Laravel Nova Excel
🚀 Supercharged Excel exports for Laravel Nova Resources
Stars: ✭ 259 (+117.65%)
Mutual labels:  laravel, nova
Nova Advanced Image Field
🌄📐 A Laravel Nova advanced image field with cropping and resizing using Cropper.js and Intervention Image
Stars: ✭ 67 (-43.7%)
Mutual labels:  laravel, nova
Nova Indicator Field
A colour-coded indicator field for Laravel Nova
Stars: ✭ 108 (-9.24%)
Mutual labels:  laravel, nova

Making Nova fields translatable

Latest Version on Packagist GitHub Workflow Status Check & fix styling Total Downloads

This package contains a Translatable class you can use to make any Nova field type translatable.

Imagine you have this fields method in a Post Nova resource:

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        Translatable::make([
            Text::make('title'),
            Trix::make('text'),
        ]),
    ];
}

That Post Nova resource will be rendered like this.

screenshot

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Requirements

This Nova field requires Nova 3 specifically and MySQL 5.7.8 or higher.

Installation

First you must install spatie/laravel-translatable into your Laravel app. In a nutshell, this package will store translations for your model in a json column in your table. On top of that, it provides many handy functions to store and retrieve translations. Be sure to read the entire readme of laravel-translatable before using this Nova package.

Next, you can install this Nova package into a Laravel app that uses Nova via composer:

composer require spatie/nova-translatable

Usage

In order to use the package you must first let Translatable know which locales your app is using using the Translatable::defaultLocales() method. You can put this code in AppServiceProvider or a dedicated service provider of your own.

// in any service provider

\Spatie\NovaTranslatable\Translatable::defaultLocales(['en', 'fr']);

Next, you must prepare your model as explained in the readme of laravel-translatable. In short: you must add json columns to your model's table for each field you want to translate. Your model must use the Spatie\Translatable\HasTranslations on your model. Finally, you must also add a $translatable property on your model that holds an array with the translatable attribute names.

Now that your model is configured for translations, you can use Translatable in the related Nova resource. Any fields you want to display in a multilingual way can be passed as an array to `Translatable.

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        Translatable::make([
            Text::make('title'),
            Trix::make('text'),
        ]),
    ];
}

Customizing the locales per translatable

If you have a Nova resource where you want different locales than the ones configured globally, you can call the locales method on Translatable.

Translatable::make([
    Text::make('title'),
    Trix::make('text'),
])->locales(['de', 'es']),

These fields will now use the de and es locales.

Customizing the name of a translatable

By default translatable fields get ($locale) appended to their name. You can customize this behaviour globally by providing a closure to displayLocalizedNameByDefaultUsing on Translatable. This callback will be used to render the localized field names.

Translatable::displayLocalizedNameByDefaultUsing(function(Field $field, string $locale) {
   return ucfirst($field->name) . " [{$locale}]";
})

With this in place all names of translatable fields will get [$locale] appended.

You can also customize the localized field name per resource by passing a closure the displayLocalizedNameUsing function.

Translatable::make([
    Text::make('title'),
    Trix::make('text'),
])->displayLocalizedNameUsing(function(Field $field, string $locale) {
   return ucfirst($field->name) . " --- {$locale}]";
}),

With this in place, the localized field names will be suffixed with --- $locale.

Of course you can still customize the name of a field as usual.

Translatable::make([
    Text::make('My title', 'title'),
    Trix::make('text'),
])->displayLocalizedNameUsing(function(Field $field, string $locale) {
   return ucfirst($field->name) . " [{$locale}]";
}),

Using the code about above the name for the title field will be "My title ['en']".

On customizing the UI

You might wonder why we didn't render the translatable fields in tabs, panels or with magical unicorns displayed next to them. The truth is that everybody wants translations to be displayed a bit different. That's why we opted to keep them very simple for now.

If Nova gains the ability to better structure a long form natively, we'd probably start leveraging that in a new major version of the package.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].