Symfony 2 setlocale (LC_ALL, 'de_DE')

2022-01-22 00:00:00 php symfony twig setlocale

我想用德语在 TWIG 中显示日期.

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

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

但输出是Friday 28.06.2013"​​.

But the output is "Friday 28.06.2013".

我应该在哪里使用以德语显示日期的 setlocale 函数?

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

推荐答案

您需要启用 twig intl extension(它需要启用 intl 函数a> 在 php 中)a 然后你只需使用:

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) }}

<小时>

已编辑:
如果您只想本地化日期名称,可以创建自己的 Twig 扩展:


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 AcmeBundleDemoBundleTwig;

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';
    }
}

然后在services.yml中注册

Then register it in services.yml

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

parameters:
    acme_demo.date_extension.class: AcmeBundleDemoBundleTwigDateExtension

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

在您的 Twig 模板中,您可以只使用:

In your Twig template you can use just:

{{ event.beginAt|intl_day }} {{ event.beginAt|date('d.m.Y') }}

相关文章