An open API service indexing awesome lists of open source software.

https://github.com/outl1ne/nova-lang

This Laravel Nova package allows you to sort Nova resource's index view and assign locales to resources
https://github.com/outl1ne/nova-lang

laravel-nova laravel-nova-tool locale nova

Last synced: 3 months ago
JSON representation

This Laravel Nova package allows you to sort Nova resource's index view and assign locales to resources

Awesome Lists containing this project

README

        

# Nova Lang

[Laravel Nova](https://nova.laravel.com) package that allows you to set localization of content.

![NovaLang](./docs/novalang.gif)

## Installation

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

```bash
# Install package
composer require optimistdigital/nova-lang
```

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

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

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

```php
// in app/Providers/NovaServiceProvider.php

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

## Defining locales

```php
// 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.

```php
$locales = nova_lang_get_all_locales();

//or you can use it in another package.

'locales' => nova_lang_get_all_locales(),
```

## Usage

### Creating the field

```php
$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.

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

### Sort resources by locale

```php
//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

- [Kaspar Rosin](https://github.com/KasparRosin)

## License

Nova page manager is open-sourced software licensed under the [MIT license](LICENSE.md).