2

I would like to display a date in TWIG in German.

{{ event.beginnAt |date('l d.m.Y')}}

But the output is "Friday 28.06.2013".

Where should I use the setlocale function that displays the date in German?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Vico
  • 1,269
  • 2
  • 12
  • 16
  • did you check your parameters.yml ? – cptnk Jun 27 '13 at 14:53
  • possible duplicate of [How to render a DateTime object in a Twig template](http://stackoverflow.com/questions/8318914/how-to-render-a-datetime-object-in-a-twig-template) – Nic Wortel Feb 09 '15 at 13:40

3 Answers3

4

You need to enable twig intl extension (it requires enabled intl functions in php) a then you just use:

{{ event.beginAt | localizeddate('full', 'none', locale) }}

Edited:
If you want to just localized name of day, you can create your own Twig extension:

src/Acme/Bundle/DemoBundle/Twig/DateExtension.php

namespace Acme\Bundle\DemoBundle\Twig;

class DateExtension extends \Twig_Extension
{

    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('intl_day', array($this, 'intlDay')),
        );
    }

    public function intlDay($date, $locale = "de_DE")
    {

        $fmt = new \IntlDateFormatter( $locale, \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, 'Europe/Berlin', \IntlDateFormatter::GREGORIAN, 'EEEE');
        return $fmt->format($date);
    }

    public function getName()
    {
        return 'date_extension';
    }
}

Then register it in services.yml

src/Acme/Bundle/DemoBundle/Resources/config/services.yml

parameters:
    acme_demo.date_extension.class: Acme\Bundle\DemoBundle\Twig\DateExtension

services:
    acme_demo.twig.date_extension:
        class: %acme_demo.date_extension.class%
        tags:
            - { name: twig.extension }

In your Twig template you can use just:

{{ event.beginAt|intl_day }} {{ event.beginAt|date('d.m.Y') }}
jkucharovic
  • 4,214
  • 1
  • 31
  • 46
  • Thank u very mutch! But now i have an other problem. event.beginnAt | localizeddate('full', 'none', 'de') gives out Freitag, 28. Juni 2013. But i only want Freitag, 28.06.2013 – Vico Jun 28 '13 at 08:50
  • The filter "intl_day" does not exist in templatefile. – Vico Jun 28 '13 at 09:40
  • Okay. I dont register the yml in PageExtension. Now it work. But i get an other Error: "Method "beginAt" for object "Event\PageBundle\Entity\Event" – Vico Jun 28 '13 at 09:46
  • It's typo – I've written `beginAt`, but you use `beginnAt` – jkucharovic Jun 28 '13 at 10:12
0

In order to achieve that I used the SonataIntlBundle it exposes some twig function that are formatted with intl.

Example:

 {{ date_time_object | format_datetime(null, 'fr', 'Europe/Paris',
constant('IntlDateFormatter::LONG'), constant('IntlDateFormatter::SHORT')) }}

will output

 '1 février 2011 19:55'

I used a simpler approach since I forced the local of the php execution context and, after that, just replaced the date function by format_datetime. Remember to see the supported patterns as they differ from the strftime. You have a link on the SonataIntlBundle for that.

saamorim
  • 3,855
  • 17
  • 22
0

You can use setlocale() and strftime(), using something Symfony-specific might be overhead.

Jarek Jakubowski
  • 948
  • 10
  • 22