Skip to main content
Tutorial Free

Set Laravel User Locale in Middleware

May 13, 2023
2 min read

Tutorial last revisioned on May 27, 2025 with Laravel 12

To set the global Laravel language (locale), you need to use the app()->setLocale() method. But the question is: where to put this method if you want to set the locale based on a DB field like users.language?

Such global settings are often put in the AppServiceProvider, but in this case, it wouldn't work, because you don't have access to Auth:::user() there, you would get an error "Attempt to read property on null":

The reason is that at the time of loading the AppServiceProvider, the session isn't initialized yet, and the User object doesn't exist.

That's why we need to use Middleware.

php artisan make:middleware SetLocale

Then, in the Middleware, you can set the locale from the $request->user() or auth()->user():

app/Http/Middleware/SetLocale.php

class SetLocale
{
public function handle($request, Closure $next)
{
if(! $request->user()) {
return $next($request);
}
 
$language = $request->user()->language;
 
if (isset($language)) {
app()->setLocale($language);
}
 
return $next($request);
}
}

And then you have 2 options to use that Middleware.


Option 1: Adding Middleware to Global Middleware Stack

bootstrap/app.php:

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->web([
\App\Http\Middleware\SetLocale::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();

This way, the locale will be set for all routes which are in the web middleware group, which means all routes in the routes/web.php file.


Option 2: Adding Middleware to Specific Route(s)

bootstrap/app.php

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'setLocale' => \App\Http\Middleware\SetLocale::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();

And then in your routes file add it to the routes or route groups:

Route::group(['middleware' => ['auth', 'setLocale']], function () {
// ...
});

This way, only specific routes will have the locale set.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

VR
Vasil Raminashvili βœ“ Link copied!

Hi there greate little post and defently usful.

I wanted to like or bookmark it in my profile for future when needed. Is there similiar functionality or are you thinking of adding one ?

PK
Povilas Korop βœ“ Link copied!

I've been recommending people to use Browser bookmark feature, but since you are not the first asking for it, I guess we are "forced" to build our own "My bookmarks" :)

Adding to the to-do list.

TZ
Thaer Zuirkat βœ“ Link copied!

That would be awesome @Povilas, the amount of valuable content you guys put out is amazing, and getting it lost inside a browser bookmarks is seriously a shame. I asked for this on Discord sometime back, and i recieved the same response 'browser bookmark' from one of LD team members as well.

This feature would be invaluable, and most welcomed

οΏ½D
Γ‡ağlar Dursun βœ“ Link copied!

Nice post, thanks!

R
rafabayona βœ“ Link copied!

This is not valid for Laravel 12 since there is no Kernel.php anymore. I tried to add it to bootstap/app.php but it looks it's too early to access session data, I had to add the middleware via routes

M
Modestas βœ“ Link copied!

Yes, Laravel 12 has a bit of a difference. Will see if we should update this tutorial

N
Nerijus βœ“ Link copied!

Lesson is updated for Laravel 12

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.