Skip to content

Commit

Permalink
Merge pull request #1439 from bolt/bugfix/htmllang-default-locale
Browse files Browse the repository at this point in the history
Fix htmllang() when no locales defined
  • Loading branch information
bobdenotter committed Jun 5, 2020
2 parents cae744e + cd7094f commit b38d9ba
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/Utils/LocaleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -88,14 +97,23 @@ 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);
}

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');
Expand Down

0 comments on commit b38d9ba

Please sign in to comment.