optimistdigital/nova-lang

A Laravel Nova tool.

1.2.1 2020-05-14 19:21 UTC

This package is auto-updated.

Last update: 2020-07-14 19:37:27 UTC


README

Laravel Nova package that allows you to set localization of content.

NovaLang

Installation

Install the package in a Laravel Nova project via Composer and run migrations:

# Install package
composer require optimistdigital/nova-lang

Publish the nova-lang configuration file and edit it to your preference:

php artisan vendor:publish --provider="OptimistDigital\NovaLang\ToolServiceProvider" --tag="config"

Register the tool with Nova in the tools() method of the NovaServiceProvider:

// in app/Providers/NovaServiceProvider.php

public function tools()
{
    return [
        // ...
        new \OptimistDigital\NovaLang\NovaLang
    ];
}

Defining locales

// in /config/nova-lang.php

// ...
'locales' => [
  'en' => 'English',
  'et' => 'Estonian',
],

// OR

'locales' => function () {
  return Locale::all()->pluck('name', 'key');
},

After defining locales in /config/nova-lang.php, you can use helper function.

$locales = nova_lang_get_all_locales();

//or you can use it in another package.

'locales' => nova_lang_get_all_locales(),

Usage

Creating the field

$fields[] = NovaLangField::make('Locale', 'locale');

If you are using a translation package like nova-locale-field, you can also give locale_parent_id. localeParentId has to be optainable through url query. Make sure you have defined locale_parent_id in your database.

$fields[] = NovaLangField::make('Locale', 'locale', 'locale_parent_id')

Sort resources by locale

//Your resource file, where you have returned $fields array
public static function indexQuery(NovaRequest $request, $query)
{
    $localeColumn = 'your_table_name' . 'locale'
    $query->where(function ($subQuery) use ($localeColumn) {
        $subQuery->where($localeColumn, nova_lang_get_active_locale())
            ->orWhereNotIn($localeColumn, array_keys(nova_lang_get_all_locales()));
    });
    return $query;
}

Credits

License

Nova page manager is open-sourced software licensed under the MIT license.