I'm just trying to show the name of the months based on the current locale.
{{ event.date|date('F') }}
but the months are always shown in english...
I have tried this code below I found here, but the result is the same...
class Helper_Twig extends Twig_Extension
{
public function getFilters()
{
return array(
'datetime' => new Twig_Filter_Method($this, 'datetime')
);
}
public function datetime($d, $format = "%B %e")
{
if ($d instanceof \DateTime) {
$d = $d->getTimestamp();
}
return strftime($format, $d);
}
public function getName()
{
return 'Helper';
}
}
NOTE: In the controller I'm checking the current locale using $request->getLocale
and it corresponds to the locale parameter I'm switching in parameters.yml.
What is the problem?
Since you defined datetime
TwigFilter, you don't have to call {{ event.date|date('F') }}
Instead, you should call this {{ event.date|datetime('%B') }}
If you have installed the IntlExtension
you can use format_datetime
with custom pattern.
{{ event.date|format_datetime(pattern="MMMM") }}
docs:
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