PHP code example of eolica / nova-locale-switcher

1. Go to this page and download the library: Download eolica/nova-locale-switcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eolica / nova-locale-switcher example snippets

 php
use Illuminate\Http\Request;

public function tools()
{
    return [
        \Eolica\NovaLocaleSwitcher\LocaleSwitcher::make()
            ->setLocales(config('nova.locales'))
            ->onSwitchLocale(function (Request $request) {
                $locale = $request->post('locale');

                if (array_key_exists($locale, config('nova.locales'))) {
                    $request->user()->update(['locale' => $locale]);
                }
            }),
    ];
}
 php
return [

    // Rest of Nova configuration

    ...

    /*
    |--------------------------------------------------------------------------
    | Nova Locales
    |--------------------------------------------------------------------------
    */

    'locales' => [
        'en' => 'English',
        'de' => 'Deutsch',
        'es' => 'Español',
    ],
];
 php
use Laravel\Nova\Events\ServingNova;

final class NovaServiceProvider extends NovaApplicationServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        Nova::serving(function (ServingNova $event) {
            $user = $event->request->user();

            if (array_key_exists($user->locale, config('nova.locales'))) {
                app()->setLocale($user->locale);
            }
        });
    }

    ...
}
 bash
php artisan nova:publish