optimistdigital/nova-lang

A Laravel Nova tool.

Installs: 8 867

Dependents: 0

Suggesters: 0

Security: 0

Stars: 14

Watchers: 3

Forks: 2

Open Issues: 0

Language:Vue

1.3.0 2020-10-28 14:59 UTC

This package is auto-updated.

Last update: 2024-12-01 12:33:48 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.