12

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?

tirenweb
  • 30,963
  • 73
  • 183
  • 303

4 Answers4

4

Since you defined datetime TwigFilter, you don't have to call {{ event.date|date('F') }}

Instead, you should call this {{ event.date|datetime('%B') }}

Vitalii Zurian
  • 17,858
  • 4
  • 64
  • 81
3

You can use SonataIntlBundle to handle localized date representation. Intl library needs to be installed though.

Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
2

You probably haven't installed the PHP intl-library in your PHP. http://php.net/manual/de/book.intl.php

Chris
  • 8,031
  • 10
  • 41
  • 67
2

If you have installed the IntlExtension you can use format_datetime with custom pattern.

{{ event.date|format_datetime(pattern="MMMM") }}

docs:

Julien
  • 703
  • 1
  • 10
  • 17