Symfony 2 setlocale (LC_ALL, 'de_DE') [英] Symfony 2 setlocale (LC_ALL, 'de_DE')

查看:18
本文介绍了Symfony 2 setlocale (LC_ALL, 'de_DE')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用德语在 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') }}

这篇关于Symfony 2 setlocale (LC_ALL, 'de_DE')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆