'locales' => [ 'en' => 'English', 'it' => 'Italiano'],
CREATE A CUSTOM MIDDLEWARE
php artisan make:middleware Language
This will create Language.php in App\Http\Middleware. Modify the handle method adding your own logic, in our example, we will use a query parameter _locale to modify the application locale and set a _locale session variable to store this choice and maintain the locale across various pages.
public function handle($request, Closure $next) { $locales = config('app.locales'); $locale = $request->get('_locale'); if(!is_null($locale) && is_array($locales) && array_key_exists($locale, $locales)){ app()->setLocale($locale); session(['_locale'=>$locale]); }elseif(!is_null(session('_locale'))){ app()->setLocale(session('_locale')); } return $next($request); }
MODIFY THE MIDDLEWARE WEB GROUP
/** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, \App\Http\Middleware\Language::class ], 'api' => [ 'throttle:60,1', 'bindings', ], ];
That’s it, now you can change your app locale simply using ?_locale=it in the URL.
Related Articles
Laravel from scratch on was EC2
By Vimuth Somarathna
Laravel 5.3 add multi-language functionality
By Luigi Laezza
Mailtrap with symfony, testing emails has never been so easy
By Luigi Laezza
Improve your social media strategy with Semrush
By Luigi Laezza
On SMASHINGMAGAZINE an introduction to building and sending HTML email for web developers
By Luigi Laezza
Inexpensive stage lamp server with Raspberry PI3 and Ubunto server
By Luigi Laezza
Secure your SSH connection disabling SSH password logins
By Luigi Laezza
Analizzare tecniche e strumenti per conquistare nuovi clienti e far crescere la tua attività
By Luigi Laezza