route('locale'); $available = config('app.available_locales'); if (! in_array($locale, $available, true)) { abort(404); } App::setLocale($locale); // Lets route() calls omit {locale} anywhere in the request lifecycle // (controllers, views) — without this, every route() call inside the // {locale} group would need locale passed explicitly every time. URL::defaults(['locale' => $locale]); $altLocale = collect($available)->first(fn ($l) => $l !== $locale); $routeName = $request->route()->getName(); View::share('currentLocale', $locale); View::share('altLocale', $altLocale); View::share( 'altLocaleUrl', $routeName ? route($routeName, array_merge($request->route()->parameters(), ['locale' => $altLocale])) : url('/'.$altLocale), ); return $next($request); } }