Skip to content

Commit

Permalink
Feat: set Language in current Browser Language
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahri Meral committed Oct 1, 2023
1 parent 5d76a1f commit 17bf19f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=7.0.0"
"php": ">=7.4"
},
"autoload": {
"psr-4": {
Expand All @@ -32,4 +32,4 @@
"sort-packages": true
},
"minimum-stability": "stable"
}
}
41 changes: 41 additions & 0 deletions src/Controllers/WelcomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,54 @@

class WelcomeController extends Controller
{

/**
* Display the installer welcome page.
*
* @return \Illuminate\Http\Response
*/
public function welcome()
{
$acceptLang = \Request::server('HTTP_ACCEPT_LANGUAGE');
foreach ($this->parse(\Arr::wrap($acceptLang)) as $locale => $quality) {
$locale = \Str::before($locale, '-');

//save lang
app()->setLocale($locale);
session()->put('locale', $locale);

break;
}

return view('vendor.installer.welcome');
}

protected function parse(array $acceptLanguage): array
{
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', \Arr::first($acceptLanguage), $matches);

if (empty($matches[1])) {
return [];
}

/** @var array<string, string> $locales */
$locales = array_combine($matches[1], $matches[4]) ?: [];

foreach ($locales as $locale => $quality) {
if ($quality === '') {
$locales[$locale] = 1;
}

if (\Str::contains($locale, '-')) {
$lang = \Str::before($locale, '-');

$locales[$lang] ??= 0;
}
}

arsort($locales, SORT_NUMERIC);

return array_map('floatval', $locales);
}

}

0 comments on commit 17bf19f

Please sign in to comment.