A new middleware UserLocale.php has been added. This middleware sets the language locale based on each user's preference. It operates by checking if the user's preferred language is set during the request cycle and if so, it changes the app's locale accordingly. This feature facilitates personalization by displaying the app in a user's preferred language.

Also, registering the middleware in the `Kernel.php` allows it to be used throughout the application
This commit is contained in:
Leonid Nikitin 2023-07-10 21:44:00 +06:00
parent 907bac5586
commit 9319c2d92d
Signed by: kor-elf
GPG Key ID: 7DE8F80C5CEC2C0D
2 changed files with 20 additions and 0 deletions

View File

@ -63,5 +63,6 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequestsWithRedis::class,
'verified' => \App\Http\Middleware\EnsureUserIsVerified::class,
'user.locale' => \App\Http\Middleware\UserLocale::class,
];
}

View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
final class UserLocale
{
public function handle(Request $request, Closure $next)
{
if ($request->user() && $request->user()->lang) {
App::setLocale($request->user()->lang->getLocale());
}
return $next($request);
}
}