diff --git a/src/Utils/LocaleHelper.php b/src/Utils/LocaleHelper.php index b0b6aaae2..4fd668525 100644 --- a/src/Utils/LocaleHelper.php +++ b/src/Utils/LocaleHelper.php @@ -34,10 +34,14 @@ class LocaleHelper /** @var ContentRepository */ private $contentRepository; - public function __construct(string $locales, ContentRepository $contentRepository, UrlGeneratorInterface $urlGenerator, Config $config) + /** @var string */ + private $defaultLocale; + + public function __construct(string $locales, ContentRepository $contentRepository, UrlGeneratorInterface $urlGenerator, Config $config, string $defaultLocale) { $this->localeCodes = new Collection(explode('|', $locales)); $this->urlGenerator = $urlGenerator; + $this->defaultLocale = $defaultLocale; $this->flagCodes = $this->getFlagCodes(); $this->codetoCountry = $this->getCodetoCountry(); @@ -52,9 +56,14 @@ public function getLocales(Environment $twig, ?Collection $localeCodes = null, b if ($all) { $localeCodes = $this->localeCodes; } else { - $localeCodes = $this->getContentLocales(); + $localeCodes = new Collection($this->getContentLocales()); } } + + if (! $this->localeCodes->has($this->defaultLocale)) { + $this->localeCodes->add($this->defaultLocale); + } + // Get the route and route params, to set the new localized link $globals = $twig->getGlobals(); @@ -88,7 +97,7 @@ public function getLocales(Environment $twig, ?Collection $localeCodes = null, b } $locale->put('link', $this->getLink($route, $routeParams, $locale)); - $locale->put('current', $currentLocale === $localeCode); + $locale->put('current', $this->isCurrentLocale($localeCode, $currentLocale, $localeCodes)); $locales->push($locale); } @@ -96,6 +105,15 @@ public function getLocales(Environment $twig, ?Collection $localeCodes = null, b return $locales; } + private function isCurrentLocale(string $localeCode, string $currentLocale, Collection $localeCodes): bool + { + if (! $localeCodes->has($currentLocale)) { + return $localeCode === $this->defaultLocale; + } + + return $currentLocale === $localeCode; + } + private function getContentLocales(): array { $contentTypes = $this->config->get('contenttypes');