From ac085e8bbd3b7f7be55883aff3c56c8a516ddf3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Parient=C3=A9?= <41431456+arthurpar06@users.noreply.github.com> Date: Tue, 19 Nov 2024 21:06:30 +0100 Subject: [PATCH] Add user language preference detection as default fallback (#1894) Co-authored-by: Nabeel S. --- app/Http/Middleware/SetActiveLanguage.php | 10 +++++++++- config/languages.php | 9 +++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/Http/Middleware/SetActiveLanguage.php b/app/Http/Middleware/SetActiveLanguage.php index 21b36c094..c6ea7f314 100644 --- a/app/Http/Middleware/SetActiveLanguage.php +++ b/app/Http/Middleware/SetActiveLanguage.php @@ -18,7 +18,15 @@ class SetActiveLanguage */ public function handle(Request $request, Closure $next) { - App::setLocale($request->cookie('lang', config('app.locale', 'en'))); + $preferredLanguage = 'en'; + if ($request->hasCookie('lang')) { + $preferredLanguage = $request->cookie('lang', config('app.locale', 'en')); + } else { + $preferredLanguage = $request->getPreferredLanguage(array_keys(config('languages'))); + } + + App::setLocale($preferredLanguage); + return $next($request); } } diff --git a/config/languages.php b/config/languages.php index 1aea9e5e3..394bf8a86 100644 --- a/config/languages.php +++ b/config/languages.php @@ -1,14 +1,15 @@ [ - 'display' => 'German', - 'flag-icon' => 'de', - ], + // First in the list is the default 'en' => [ 'display' => 'English', 'flag-icon' => 'us', ], + 'de' => [ + 'display' => 'German', + 'flag-icon' => 'de', + ], 'es-es' => [ 'display' => 'Spanish (Spain)', 'flag-icon' => 'es',