Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better language detection and validation #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions Router/DefaultLocaleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function resolveLocale(Request $request, array $availableLocales)
if ($request->query->has('hl')) {
$hostLanguage = $request->query->get('hl');

if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) {
if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage) && in_array($hostLanguage,$availableLocales)) {
return $hostLanguage;
}
}
Expand All @@ -56,19 +56,23 @@ public function resolveLocale(Request $request, array $availableLocales)
if ($request->cookies->has('hl')) {
$hostLanguage = $request->cookies->get('hl');

if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) {
if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage) && in_array($hostLanguage,$availableLocales)) {
return $hostLanguage;
}
}

// use accept header for locale matching if sent
if ($languages = $request->getLanguages()) {
foreach ($languages as $lang) {
if (in_array($lang, $availableLocales, true)) {
return $lang;
}
}
}
if ($languages = $request->getLanguages()) {
foreach ($languages as $lang) {
if (in_array($lang, $availableLocales, true)) {
return $lang;
}else if (preg_match('#^([a-z]{2})_[a-z]{2}?$#i', $lang, $langParts)) {
if (in_array($langParts[1], $availableLocales, true)) {
return $langParts[1];
}
}
}
}

return null;
}
Expand Down