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 Nov 14, 2024
1 parent f910b19 commit 0e07b93
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 @@ -132,11 +132,10 @@ public function getLocale(): string
return $this->locale;
}
$request = $this->_getRequest();
$supportedLocales = array_values($this->_getSupportedLocales());
$locale = $request->getUserVar('setLocale')
?: $request->getSession()->get('currentLocale')
?: $request->getCookieVar('currentLocale')
?: $request->getPreferredLanguage(count($supportedLocales) ? $supportedLocales : [LocaleInterface::DEFAULT_LOCALE]);
?: $this->_getPreferredLocale();
$this->setLocale($locale);
return $this->locale;
}
Expand Down Expand Up @@ -584,4 +583,19 @@ private function _getSubmissionLocaleNames(): array
->toArray());
})();
}

/**
* 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 0e07b93

Please sign in to comment.