Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : App::setLocale doesn't work

I'm using laravel 5.1, I'm trying to update locale in app file like this : In Locale Middleware file :

...
public function handle($request, Closure $next)
    {       
        if(Session::has('locale'))
        {
            $lang = Session::get('locale');            
            App::setLocale($lang);        
        }

        return $next($request);
    }

Any idea about this ??

like image 952
BKF Avatar asked Jan 02 '16 13:01

BKF


2 Answers

Oooof finally after two hours ><' !! It's the line place of locale class in middleware -.-' !!! I set it in last line like this :

    ...
    ...
    \App\Http\Middleware\VerifyCsrfToken::class,
        \App\Http\Middleware\Locale::class,        
    ];

and All is fine and working ! thanks for you all :))))

like image 97
BKF Avatar answered Sep 22 '22 00:09

BKF


The only solution which I found was set locale in constructor method of middle ware, like this:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;

class Localization
{

    protected $app;

    public function __construct(Application $app, Request $request)
    {
        if($locale = $request->header('Content-Language')){
            if(in_array($locale, ['en', 'fa'])){
                $app->setLocale($locale);
            }
        }
    }

    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request);
    }
}

With ♥♥♥ and over 2 hours trying!