Skip to content

Commit

Permalink
[FEATURE] Allow language fallbacks #8
Browse files Browse the repository at this point in the history
  • Loading branch information
rafu1987 committed Mar 21, 2020
1 parent c7c8ed4 commit 698baee
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Classes/Middleware/RedirectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ protected function getRedirectResponseByBrowserLanguage(ServerRequestInterface $
/** @var SiteLanguage[] $siteLanguages */
$siteLanguages = $site->getLanguages();

$siteLanguagesFallbacks = [];
if (is_array($site->getConfiguration()['SiteLanguageRedirectionFallbacks']) && !empty($site->getConfiguration()['SiteLanguageRedirectionFallbacks'])) {
$siteLanguagesFallbacks = $site->getConfiguration()['SiteLanguageRedirectionFallbacks'];
}

$acceptLanguages = $request->getHeader('accept-language');
if (!empty($acceptLanguages)) {
$acceptLanguages = array_unique(
Expand Down Expand Up @@ -133,7 +138,7 @@ protected function getRedirectResponseByBrowserLanguage(ServerRequestInterface $
/** @var array $matchingSiteLanguageCodes Array in the form of: ['de-at', 'de', 'en'] */
$matchingSiteLanguageCodes = array_filter(
$acceptLanguagesWithFallbacks,
function ($language) use ($siteLanguages) {
function ($language) use ($siteLanguages, $siteLanguagesFallbacks) {
return in_array(
$language,
array_merge(
Expand All @@ -144,7 +149,10 @@ function ($language) use ($siteLanguages) {
array_map(function ($siteLanguage) {
/** @var SiteLanguage $siteLanguage */
return $siteLanguage->getTwoLetterIsoCode();
}, $siteLanguages)
}, $siteLanguages),
array_map(function ($siteLanguagesFallback) {
return $siteLanguagesFallback;
}, array_keys($siteLanguagesFallbacks))
)
);
}
Expand All @@ -153,10 +161,10 @@ function ($language) use ($siteLanguages) {
/** @var SiteLanguage[] $matchingSiteLanguages */
$matchingSiteLanguages = array_map(function ($item) {
return array_shift($item);
}, array_map(function ($matchingSiteLanguageCode) use ($siteLanguages) {
return array_filter($siteLanguages, function ($siteLanguage) use ($matchingSiteLanguageCode) {
}, array_map(function ($matchingSiteLanguageCode) use ($siteLanguages, $siteLanguagesFallbacks) {
return array_filter($siteLanguages, function ($siteLanguage) use ($matchingSiteLanguageCode, $siteLanguagesFallbacks) {
/** @var SiteLanguage $siteLanguage */
return strtolower($siteLanguage->getHreflang()) === $matchingSiteLanguageCode || $siteLanguage->getTwoLetterIsoCode() === $matchingSiteLanguageCode;
return strtolower($siteLanguage->getHreflang()) === $matchingSiteLanguageCode || $siteLanguage->getTwoLetterIsoCode() === $matchingSiteLanguageCode || strtolower($siteLanguage->getHreflang()) === $siteLanguagesFallbacks[$matchingSiteLanguageCode] || $siteLanguage->getTwoLetterIsoCode() === $siteLanguagesFallbacks[$matchingSiteLanguageCode];
});
}, $matchingSiteLanguageCodes));

Expand Down

0 comments on commit 698baee

Please sign in to comment.