route()?->getName(); if (! $routeName || ! Str::startsWith($routeName, 'back.')) { return $next($request); } // 1️⃣ Session if (Session::has('lang')) { $locale = Session::get('lang'); } // 2️⃣ FallBack else { $locale = config('app.fallback_locale'); } App::setLocale($locale); // 1. Is Post method or Inertia request ? if ( in_array($request->method(), ['POST', 'PUT', 'PATCH'], true) and $request->headers->has('X-Inertia') ) { // 1. Request Locale $requestLocale = $request->locale; // 2. If prefix is not in supportedLocales if (!in_array($requestLocale, app('laravellocalization')->getSupportedLocales(), true)) { app()->setLocale(config('app.fallback_locale')); } // 3. Set new locale if locale and prefix are not the same if ($requestLocale !== app()->getLocale()) { app()->setLocale($requestLocale); } } return $next($request); } }