Skip to content

Commit

Permalink
pkp#7512 Give preference to the server preferred locale if there's no…
Browse files Browse the repository at this point in the history
… match with the Accept-Language header
  • Loading branch information
jonasraoni committed Jul 31, 2022
1 parent 0f06604 commit 5bf1db1
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions classes/i18n/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,10 @@ public function getLocale(): string
return $this->locale;
}
$request = $this->_getRequest();
$supportedLocales = array_values($this->_getSupportedLocales());
$locale = $request->getUserVar('setLocale')
?: (SessionManager::hasSession() ? SessionManager::getManager()->getUserSession()->getSessionVar('currentLocale') : null)
?: $request->getCookieVar('currentLocale')
?: $request->getPreferredLanguage(count($supportedLocales) ? $supportedLocales : [LocaleInterface::DEFAULT_LOCALE]);
?: $this->_getPreferredLocale();
$this->setLocale($locale);
return $this->locale;
}
Expand Down Expand Up @@ -445,4 +444,19 @@ private function _getSupportedLocales(): array
?? array_map(fn (LocaleMetadata $locale) => $locale->locale, $this->getLocales());
return $this->supportedLocales = array_combine($locales, $locales);
}

/**
* Retrieve the preferred user locale from our supported locales using the Accept-Language header
* If there's no match, it falls back to the server's primary locale
*/
private function _getPreferredLocale(): ?string
{
$serverPreference = $this->getPrimaryLocale() ?: LocaleInterface::DEFAULT_LOCALE;
$supportedLocales = array_values($this->_getSupportedLocales());
// Move the server preference to the top, in case the user preference doesn't match with the supported locales, the server one will be picked
if (is_int($index = array_search($serverPreference, $supportedLocales))) {
array_splice($supportedLocales, $index, 1);
}
return $this->_getRequest()->getPreferredLanguage([$serverPreference, ...$supportedLocales]);
}
}

0 comments on commit 5bf1db1

Please sign in to comment.