ludo237
2625
6
General

Set Locale with Middleware in Laravel 5

Posted 4 years ago by ludo237

Hello everyone.

I've an Application that's currently using Laravel 4.1.x stable. But I'm writing the new version with laravel 5, not converting the actual project but rewriting from scratch because I've the time to do it.

Anyway I use to have the language locale inside filters.php in Laravel 4 with this snippet


App::before(function ($request) { // Set the language if(in_array(Request::segment(1), Config::get('app.locale'))) { Session::put('cisgo.language_locale', Request::segment(1)); return Redirect::to(substr(Request::path(), 3)); } // Check if the session has the language if(!Session::has('cisgo.language_locale')) { Session::put('cisgo.language_locale', Config::get('app.fallback_locale')); } App::setLocale(Session::get('cisgo.language_locale')); });

But in Laravel 5, with middleware, I really don't understand where I should use this?

I think I've to create a new Middleware called language and inject the Application contracts but it doesn't have any method called before or setLocale so I'm a bit lost here. Also on http://laravel.com/api/master/index.html it seems that there's no App::before.

Anybody with an hint for me?

Please sign in or create an account to participate in this conversation.