[2.1] [Twig-Bridge] Added localeDate filter to Twig-Bridge #1824

Closed
wants to merge 3 commits into
from

Projects

None yet

3 participants

@thaberkern

Added a filter for generated localized dates based on the Locale-Setting of the user

@stof stof and 1 other commented on an outdated diff Jul 27, 2011
...ymfony/Bridge/Twig/Extension/TranslationExtension.php
+ 'short' => \IntlDateFormatter::SHORT,
+ 'medium' => \IntlDateFormatter::MEDIUM,
+ 'long' => \IntlDateFormatter::LONG,
+ 'full' => \IntlDateFormatter::FULL,
+ );
+
+ $formatter = \IntlDateFormatter::create(
+ $locale != null ? $locale : $this->translator->getLocale(),
+ $formatValues[$dateFormat],
+ $formatValues[$timeFormat]
+ );
+
+ if ($date instanceof \DateTime) {
+ return $formatter->format($date);
+ }
+ else {
@stof
stof Jul 27, 2011 Member

the else is useless as the if returns. Simply put the final return statement without wrapping it

Timo Haberkern small code change db13c29
@stof stof and 1 other commented on an outdated diff Jul 27, 2011
...ymfony/Bridge/Twig/Extension/TranslationExtension.php
@@ -73,6 +74,30 @@ class TranslationExtension extends \Twig_Extension
return $this->translator->transChoice($message, $count, array_merge(array('%count%' => $count), $arguments), $domain);
}
+ public function localedate($date, $dateFormat = 'medium', $timeFormat = 'medium', $locale = null)
+ {
+ $formatValues = array(
+ 'none' => \IntlDateFormatter::NONE,
+ 'short' => \IntlDateFormatter::SHORT,
+ 'medium' => \IntlDateFormatter::MEDIUM,
+ 'long' => \IntlDateFormatter::LONG,
+ 'full' => \IntlDateFormatter::FULL,
+ );
+
+ $formatter = \IntlDateFormatter::create(
+ $locale != null ? $locale : $this->translator->getLocale(),
@stof
stof Jul 27, 2011 Member

This will fail for people that use the stub intl implementation instead of the real intl as they cannot change the locale

@thaberkern
thaberkern Jul 27, 2011

@stof Any idea how we could work around this?

@thaberkern

Handling the locale-exception if the StubIntlDateFormatter is used

@stof
Member
stof commented Sep 4, 2011

I see a possible improvement here: by simply using \Locale::getDefaultLocale instead of $translator->getLocale() for the case where the locale is not given, we could make this filter totally decoupled from Symfony as it would remove the need of handling the StubIntlFormatter case (which is needed currently because the translator locale will not be en when using it whereas \Locale::getDefaultLocale() will. People passing explicitly another locale when they use the stub implementation should be nothified they did something wrong IMO)

@fabpot what do you think about creating an IntlExtension in the official Twig-extension repo containing this filter (and a few others which could be inspired from what is done in SonataIntlBundle but decoupled from Symfony) ? This would allow every Twig user to be able to use the intl-based filters when they have intl (the fact that it has a dependency to intl is the reason why I propose it for the extension repo as this dependency has already been rejected for the core repo)

@fabpot
Member
fabpot commented Sep 14, 2011

That's definitely something that needs to be part of the Twig-extension repo.

@stof stof referenced this pull request in twigphp/Twig-extensions Oct 16, 2011
Merged

Added an intl extension. #30

@stof
Member
stof commented Oct 16, 2011

I used your work to create a PR on the Twig-extensions repo as suggested previously.

I'm closing this PR as @fabpot agreed that the extensions repo was the good place for it.

@stof stof closed this Oct 16, 2011
@fabpot fabpot added a commit to twigphp/Twig-extensions that referenced this pull request Nov 7, 2011
@fabpot fabpot merged branch stof/intl (PR #30)
Commits
-------

8b78e86 Added an intl extension.

Discussion
----------

Added an intl extension.

This implementation is based on @thaberkern's work in symfony/symfony#1824

---------------------------------------------------------------------------

by stfalcon at 2011/10/23 07:09:48 -0700

it works. thanks for extension.

---------------------------------------------------------------------------

by webornot at 2011/10/25 16:33:04 -0700

thanks :)
bdd1cbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment