To format a date in twig you usually use something like:
{{ meeting.date|date("m/d/Y") }}
Now, I have to localize this date (US m/d/y, NL d/m/y). What would be the best practice to do this in the twig? I do use Symfony 2, a workaround would be to do the translation in the controller but i would like to do this in the twig.
What about the Intl Twig extension?
Usage in a twig template:
{{ my_date | localizeddate('full', 'none', locale) }}
I didn't want to install a whole extensions just for this stuff and need to do a few things automatically: It's also possible to write a helperclass (or expand an existing helper) in Bundle/Twig/Extensions for example like this:
public function foo(\Datetime $datetime, $lang = 'de_DE', $pattern = 'd. MMMM Y')
{
$formatter = new \IntlDateFormatter($lang, \IntlDateFormatter::LONG, \IntlDateFormatter::LONG);
$formatter->setPattern($pattern);
return $formatter->format($datetime);
}
twig-Template:
{{ yourDateTimeObject|foo('en_US', 'd. MMMM Y') }}
The result is "12. February 2014" (or "12. Februar 2014" in de_DE and so on)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With