Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mnrafg's avatar

App::setLocale is not working as expected in multiple route groups?

By using the following sample code when I visit http://app.dev/ar the result of {{ App::getLocale() }} in my relevant view (the view for /ar) is printing en instead of ar.

Note: I am using Laravel 5.4.

Route::group(['prefix' => 'ar', 'namespace' => 'Arabic'], function() {
    App::setlocale('ar');

    Route::get('/', 'ArabicHomeController@index')->name('arHome');
    // ...
});

Route::group(['prefix' => 'en', 'namespace' => 'English'], function() {
    App::setlocale('en');

    Route::get('/', 'EnglishHomeController@index')->name('enHome');
    // ...
});

// Setting default route
Route::get('/', function() {
    return redirect()->route('arHome');
});
0 likes
7 replies
mnrafg's avatar

Hi,

Thank you for a quick reply.

Sorry for not mentioning the actual issue which is App::setLocale('ar'); is not working as expected.

When I visit http://app.dev/ar the line {{ App::getLocale() }} is still printing en instead of ar in my relevant view.

pawelmysior's avatar

Well, that a whole different thing then :)

Let me think for a second.

pawelmysior's avatar

Ok, so in the documentation you have an example like this:

Route::get('welcome/{locale}', function ($locale) {
    App::setLocale($locale);

    //
});

Which works because you're defining the route itself as a closure and setting the locale in the closure.

What you're trying to do will not work, because Route::group(function () {}); is pretty much just a dummy grouper. Any non-routing code that you define there will not be executed outside of function scope.

What you could do instead is set the locale in a middleware. Judging from the code you wrote I suggest using a package like this https://github.com/mcamara/laravel-localization or this https://github.com/ARCANEDEV/Localization.

1 like
mnrafg's avatar

Yes that is the case with my issue, OK I will try with your suggestions.

Thank you

mnrafg's avatar
mnrafg
OP
Best Answer
Level 9

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 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');
});

And that's all.

Thanks, Muhammad Nasir Rahimi

1 like
rcvioleta's avatar

Hi All,

How this will only work by prefixing URL to example: http:://localhost/fr/home But how about with links? How are we support to handle it because if you set links to href="{{ route('post.show') }}" I'm sure it's a mess.

Please or to participate in this conversation.