Laravel : App::setLocale doesn't work
Asked Answered
S

4

10

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 ??

Sordello answered 2/1, 2016 at 13:37 Comment(11)
You have a recursion issue here, what does the parameter $next hold?Huffy
what are the symptoms of "doesnt work"? are you sure locale is set in the session?Dragonet
Yes I can see its value using : Session::get('locale') :/Sordello
@Huffy It's a feature of the Laravel framework, it's supposed to look like that. See e.g. docs here: laravel.com/docs/5.1/middlewareQuach
Do you have the locale you're trying to set installed on the OS?Quach
Have you added the App facade? And i think the App facade is outdatedAdmetus
@JoelHinz Yes of course !Sordello
@JonasHoffmann what do you mean : App facade is outdated ?Sordello
It was a mistake, it is not outdated. But i think you just have to write: use App;Admetus
Yes I set : use App;Sordello
Is there way to set this : trans('test.name',currentLang) ?Sordello
S
8

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 :))))

Sordello answered 2/1, 2016 at 17:28 Comment(1)
I have same issue. Line place was one of first items for me, but it doesn't work yet!Pathology
P
2

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!

Pathology answered 9/9, 2016 at 17:47 Comment(0)
S
0

Thanks,

:) I had the same problem and the solution was put the middleware in the file App\Http\Kernel.php in the section of protected $middleware = []

\App\Http\Middleware\VerifyCsrfToken::class,
    \App\Http\Middleware\myNewMiddleware::class,        
];
Sally answered 8/2, 2017 at 18:19 Comment(1)
... as answered one year ago. Don't duplicate answers here.Wilding
S
0

I have solved my issue as follow:

1 Step. Created a middleware. php artisan make:middleware SetLocale

2 Step. Updated the middleware file. File: app/Http/Middleware/SetLocale.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class SetLocale
{
    private $locales = ['ar', 'en'];

    // ...
    public function handle($request, Closure $next, $locale)
    {
        if (array_search($locale, $this->locales) === false) {
            return redirect('/');
        }

        App::setLocale($locale);

        return $next($request);
    }
}

3 Step. Appended my new middleware to app/Http/Kernel.php $routeMiddleware array.

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    // ...
    protected $routeMiddleware = [
        // ...
        'locale' => \App\Http\Middleware\SetLocale::class,
    ];
}

4 Step. I have used my the middleware in my routes/web.php file.

Route::group(['prefix' => 'ar', 'namespace' => 'Arabic', 'middleware' => 'locale:ar'], function() {
    Route::get('/', 'PashtoHomeController@index')->name('arHome');
    // ...
});

Route::group(['prefix' => 'en', 'namespace' => 'English', 'middleware' => 'locale:en'], function() {
    Route::get('/', 'PashtoHomeController@index')->name('enHome');
    // ...
});

Route::get('/', function() {
    return redirect()->route('arHome');
});
Serdab answered 3/11, 2020 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.