4jZW7jVSdS4U6PC's avatar

Set Locale with Middleware in Laravel 5

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?

0 likes
6 replies
bestmomo's avatar

Hello,

There is app\Http\Middleware\App.php handle method where you can set your locale.

You can use app()->setLocale(...) there or in a command or a service, as you like it.

What I do is dispatch a command in app middleware :

$this->bus->dispatch($this->setLocaleCommand);

And I manage locale in command :

public function handle()
{
    if(!session()->has('locale'))
    {
        session()->put('locale', Request::getPreferredLanguage($this->languages));
    }
    app()->setLocale(session('locale'));
}
2 likes
pmall's avatar
pmall
Best Answer
Level 56
public function handle($request, Closure $next)
{
    // Set the language
    if(in_array($request->segment(1), config('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('app.fallback_locale'));
    }

    app()->setLocale(Session::get('cisgo.language_locale'));

    return $next($request);
}

If the $next($request) is after the logic, the middleware behaves as a before filter. If it is after the logic it behaves as an after filter.

You can inject any dependency you want in the constructor of a middleware.

3 likes
4jZW7jVSdS4U6PC's avatar

@pmail the next position is something that I didn't know.

@bestmomo your idea to dispatch the command is very cool but at the moment I can't add more logic to the rewriting phase, I'll bookmark that for future use

Thanks all for the answers by the way.

sevealex's avatar

ludo237 you need to edit the /app/Http/kernel.php The line with your middleware language must be write after 'Illuminate\Session\Middleware\StartSession'

1 like
amjadkhan896's avatar

@pmall your solution really helps me. But I think there is no need of

return Redirect::to(substr($request->path(), 3));

Thanks once again. I was really stuck..

Please or to participate in this conversation.