2026-02-10 15:57:07 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
|
use Illuminate\Support\Str;
|
2026-02-10 17:35:33 +01:00
|
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
2026-02-10 15:57:07 +01:00
|
|
|
|
|
|
|
|
|
|
class ForceLocale
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* This middleware forces the locale:
|
|
|
|
|
|
* - when it is changed from the back office
|
|
|
|
|
|
* - and when the form submitted in the request belongs to a different locale
|
|
|
|
|
|
*
|
|
|
|
|
|
* The issue occurred when two browser tabs were open in different locales:
|
|
|
|
|
|
* the data being saved could end up using the other tab’s locale.
|
|
|
|
|
|
*
|
2026-02-12 16:14:11 +01:00
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
|
* @param Closure $next
|
2026-02-10 15:57:07 +01:00
|
|
|
|
* @return mixed
|
2026-02-12 16:14:11 +01:00
|
|
|
|
* @throws ValidationException Not supported locale.
|
2026-02-10 15:57:07 +01:00
|
|
|
|
*/
|
|
|
|
|
|
public function handle(Request $request, Closure $next): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
$routeName = $request->route()?->getName();
|
|
|
|
|
|
if (! $routeName || ! Str::startsWith($routeName, 'back.')) {
|
|
|
|
|
|
return $next($request);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 15:43:47 +01:00
|
|
|
|
$locale = (Session::has('lang')) ? Session::get('lang') : config('app.fallback_locale');
|
2026-02-10 17:35:33 +01:00
|
|
|
|
App::setLocale($locale);
|
2026-02-10 15:43:47 +01:00
|
|
|
|
|
2026-02-10 15:57:07 +01:00
|
|
|
|
// 1. Is Post method or Inertia request ?
|
|
|
|
|
|
if (
|
|
|
|
|
|
in_array($request->method(), ['POST', 'PUT', 'PATCH'], true) and
|
2026-02-10 17:35:33 +01:00
|
|
|
|
($request->headers->has('X-Inertia') && $request->headers->get('X-Inertia') === 'true')
|
2026-02-10 15:57:07 +01:00
|
|
|
|
) {
|
|
|
|
|
|
// 1. Request Locale
|
2026-02-10 17:35:33 +01:00
|
|
|
|
$requestLocale = $request->locale ?? config('app.fallback_locale');
|
2026-02-10 15:57:07 +01:00
|
|
|
|
// 2. If prefix is not in supportedLocales
|
2026-02-10 17:35:33 +01:00
|
|
|
|
if (!in_array($requestLocale, LaravelLocalization::getSupportedLanguagesKeys(), true)) {
|
2026-02-12 16:14:11 +01:00
|
|
|
|
throw ValidationException::withMessages(["Langue" => "Not supported locale."]);
|
2026-02-10 15:57:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
// 3. Set new locale if locale and prefix are not the same
|
|
|
|
|
|
if ($requestLocale !== app()->getLocale()) {
|
2026-02-10 17:35:33 +01:00
|
|
|
|
$this->changeLang($requestLocale);
|
2026-02-10 15:57:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
|
|
}
|
2026-02-10 17:35:33 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Change lang function
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $lang
|
|
|
|
|
|
* @return void
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function changeLang(string $lang): void
|
|
|
|
|
|
{
|
|
|
|
|
|
Session::put('lang', $lang);
|
|
|
|
|
|
app()->setLocale($lang);
|
|
|
|
|
|
}
|
2026-02-10 15:57:07 +01:00
|
|
|
|
}
|