Skip to content

Commit

Permalink
Add user language preference detection as default fallback (nabeelio#…
Browse files Browse the repository at this point in the history
…1894)

Co-authored-by: Nabeel S. <[email protected]>
  • Loading branch information
arthurpar06 and nabeelio authored Nov 19, 2024
1 parent 5862b40 commit ac085e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 9 additions & 1 deletion app/Http/Middleware/SetActiveLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
9 changes: 5 additions & 4 deletions config/languages.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

return [
'de' => [
'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',
Expand Down

0 comments on commit ac085e8

Please sign in to comment.