From 09149ddc762d418c0f2391e3ed56b5d0568f0f30 Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 24 Aug 2020 15:56:32 +0200 Subject: [PATCH 1/2] `localdate` uses prefers currentlocale over defaultlocale --- src/Twig/LocaleExtension.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Twig/LocaleExtension.php b/src/Twig/LocaleExtension.php index 311201436..b7c8196a9 100644 --- a/src/Twig/LocaleExtension.php +++ b/src/Twig/LocaleExtension.php @@ -44,11 +44,12 @@ public function getFilters(): array $safe = [ 'is_safe' => ['html'], ]; + $env = ['needs_environment' => true]; return [ new TwigFilter('localedatetime', [$this, 'localedatetime'], $safe), new TwigFilter('localedate', [$this, 'localedatetime'], $safe), - new TwigFilter('localdate', [$this, 'localdate'], $safe), + new TwigFilter('localdate', [$this, 'localdate'], $safe + $env), ]; } @@ -157,7 +158,7 @@ public function localedatetime($dateTime, string $format = '%B %e, %Y %H:%M', ?s return strftime($format, $timestamp); } - public function localdate($dateTime, ?string $format = null, ?string $locale = null): string + public function localdate(Environment $twig, $dateTime, ?string $format = null, ?string $locale = null): string { if ($dateTime instanceof \Datetime) { $dateTime = Carbon::createFromTimestamp($dateTime->getTimestamp(), $dateTime->getTimezone()); @@ -172,7 +173,8 @@ public function localdate($dateTime, ?string $format = null, ?string $locale = n } if ($locale === null) { - $locale = $this->defaultLocale; + $current = $this->getHtmlLang($twig); + $locale = ! empty($current) ? $current : $this->defaultLocale; } $dateTime->locale($locale); From 9a836af0976255832917ec5f30d48776035119fb Mon Sep 17 00:00:00 2001 From: Ivo Valchev Date: Mon, 24 Aug 2020 16:23:43 +0200 Subject: [PATCH 2/2] Use class twig. --- src/Twig/LocaleExtension.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Twig/LocaleExtension.php b/src/Twig/LocaleExtension.php index b7c8196a9..56b6fb067 100644 --- a/src/Twig/LocaleExtension.php +++ b/src/Twig/LocaleExtension.php @@ -28,12 +28,16 @@ class LocaleExtension extends AbstractExtension /** @var string */ private $defaultLocale; - public function __construct(TranslatorInterface $translator, LocaleHelper $localeHelper, Config $config, string $defaultLocale) + /** @var Environment */ + private $twig; + + public function __construct(TranslatorInterface $translator, LocaleHelper $localeHelper, Config $config, Environment $twig, string $defaultLocale) { $this->translator = $translator; $this->localeHelper = $localeHelper; $this->config = $config; $this->defaultLocale = $defaultLocale; + $this->twig = $twig; } /** @@ -44,12 +48,11 @@ public function getFilters(): array $safe = [ 'is_safe' => ['html'], ]; - $env = ['needs_environment' => true]; return [ new TwigFilter('localedatetime', [$this, 'localedatetime'], $safe), new TwigFilter('localedate', [$this, 'localedatetime'], $safe), - new TwigFilter('localdate', [$this, 'localdate'], $safe + $env), + new TwigFilter('localdate', [$this, 'localdate'], $safe), ]; } @@ -158,7 +161,7 @@ public function localedatetime($dateTime, string $format = '%B %e, %Y %H:%M', ?s return strftime($format, $timestamp); } - public function localdate(Environment $twig, $dateTime, ?string $format = null, ?string $locale = null): string + public function localdate($dateTime, ?string $format = null, ?string $locale = null): string { if ($dateTime instanceof \Datetime) { $dateTime = Carbon::createFromTimestamp($dateTime->getTimestamp(), $dateTime->getTimezone()); @@ -173,7 +176,7 @@ public function localdate(Environment $twig, $dateTime, ?string $format = null, } if ($locale === null) { - $current = $this->getHtmlLang($twig); + $current = $this->getHtmlLang($this->twig); $locale = ! empty($current) ? $current : $this->defaultLocale; }