Symfony 2 setlocale(LC_ALL,'de_DE') [英] 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')}}
但输出为"2013年6月28日星期五".
But the output is "Friday 28.06.2013".
我应该在哪里使用以德语显示日期的setlocale函数?
Where should I use the setlocale function that displays the date in German?
推荐答案
您需要启用 intl函数在php中),那么您只需使用:
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 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';
}
}
然后在services.yml中注册
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 }
在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屋!