php Symfony2:如何设置 twig |date("d F, Y") 过滤器以在瑞典语中输出月份?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17364206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2: How can I set twig |date("d F, Y") filter to output months in Swedish?
提问by tirithen
I'm having problems with the |date("d F, Y") filter in my twig templates.
我的树枝模板中的 |date("d F, Y") 过滤器有问题。
I want the months to be outputted in Swedish. I have tried setting "locale: sv" in my parameters.yml files but I get no effect.
我希望以瑞典语输出月份。我曾尝试在我的 parameters.yml 文件中设置“locale: sv”,但没有任何效果。
It was working before I upgraded to from Symfony 2.1 to 2.3 so I think that might have something to do with it.
在我从 Symfony 2.1 升级到 2.3 之前它正在工作,所以我认为这可能与它有关。
Any thoughts on how to fix this?
关于如何解决这个问题的任何想法?
回答by Nicolai Fr?hlich
The Twig Intl Extension
Twig 国际扩展
You can use the Twig Intl Extensionfound in fabpot's official Twig extension repository.
It provides a localized date filter which can be used like this:
它提供了一个本地化的日期过滤器,可以像这样使用:
{{ date | localizeddate('full', 'none', app.request.locale ) }}
use app.request.locale
as third parameter for current locale or just 'sv'
.
使用app.request.locale
作为当前区域设置或只是第三个参数'sv'
。
Integration into your project
集成到您的项目中
add the official extensions to your composer.json
using:
将官方扩展添加到您的composer.json
使用中:
composer require twig/extensions:1.0.*@dev
composer update twig/extensions
config.yml
配置文件
#enable intl extensions
services:
twig.extension.intl:
class: Twig_Extensions_Extension_Intl
tags:
- { name: twig.extension }
quick tip:
小建议:
another handy extension is the Text extension providing truncate,...etc filters
另一个方便的扩展是提供truncate等过滤器的 Text 扩展
services:
twig.extension.text:
class: Twig_Extensions_Extension_Text
tags:
- { name: twig.extension }
回答by Seb
I will make an addition to solution posted by @nifr.
我将对@nifr 发布的解决方案进行补充。
In order to use your date format install the Twig Intl Extension and than you can use:
为了使用您的日期格式,请安装 Twig Intl Extension,然后您可以使用:
{{ date|localizeddate('none', 'none', app.request.locale, null, 'dd MMMM, yyyy') }}
The last argument in my example is a date format - here is a documentation: http://userguide.icu-project.org/formatparse/datetime
我的示例中的最后一个参数是日期格式 - 这是一个文档:http: //userguide.icu-project.org/formatparse/datetime
Here is the Twig Intl Extension documentation: https://twig-extensions.readthedocs.io/en/latest/intl.html
这是 Twig 国际扩展文档:https: //twig-extensions.readthedocs.io/en/latest/intl.html
回答by Alexey B.
|date
filter use DateTime::format
function which doesnt support locales. See this questionand write your own twig extension.
|date
过滤器使用DateTime::format
不支持语言环境的功能。看到这个问题并编写你自己的树枝扩展。
回答by Liiva
I didn't really like the Twig Intl extension, it felt a bit bloated for my use case thus I went with a different approach. In our application we extended the DateTime object and overloaded the format
function to translate the date using PHP's strftime
function.
我不太喜欢 Twig Intl 扩展,它对我的用例来说有点臃肿,因此我采用了不同的方法。在我们的应用程序中,我们扩展了 DateTime 对象并重载了该format
函数以使用 PHP 的strftime
函数来转换日期。
A few things should be considered here before using this approach:
在使用这种方法之前,这里应该考虑一些事情:
- Our application uses a single language (Dutch in our case)
- We use our extended DateTime object everywhere in the application
- Extending the DateTime class willcause other issues, for example in Doctrine you will have to implement a custom Type to handle your extended DateTime and not all libraries use the
DateTimeInterface
correctly but expect\DateTime
objects
- 我们的应用程序使用一种语言(在我们的例子中是荷兰语)
- 我们在应用程序的任何地方都使用我们扩展的 DateTime 对象
- 扩展 DateTime 类会导致其他问题,例如在 Doctrine 中,您必须实现自定义类型来处理扩展的 DateTime 并且并非所有库都
DateTimeInterface
正确使用但期望\DateTime
对象
Here's the DateTime class:
这是 DateTime 类:
YourNameSpace;
class DateTime extends \DateTime {
public static function createFromFormat($format, $time, $timezone = null) {
$dateTime = parent::createFromFormat($format, $time, $timezone);
// we want to return a <YourNameSpace>\DateTime instead of a 'normal' DateTime, thus we have to instantiate one
// note that this returns `null` instead of `false` so you can use nullable return types `?DateTime` and the `??` operator
return $dateTime && $dateTime->format($format) == $time ? (new DateTime())->setTimestamp($dateTime->getTimestamp()) : null;
}
const FORMAT_LOCALE_MAP = [
'F' => '%B', // full textual representation of a month, such as January or March
'M' => '%b', // short textual representation of a month, three letters
'l' => '%A', // full textual representation of the day of the week
'D' => '%a' // short textual representation of a day, three letters
// add any other if your application needs it
];
public function format($format): string {
$formattedDate = parent::format($format);
// localize string
foreach(self::FORMAT_LOCALE_MAP as $dateFormat => $strftimeFormat) {
if(strpos($format, $dateFormat) !== false) {
$formattedDate = str_replace(parent::format($dateFormat), strftime($strftimeFormat, $this->getTimestamp()), $formattedDate);
}
}
return $formattedDate;
}
}
Then in your front controller (i.e. public/index.php
) set your locale:
然后在您的前端控制器(即public/index.php
)中设置您的语言环境:
setlocale(LC_ALL, 'nl_NL');
Now in your Twig template, anywhere a DateTime is formatted:
现在在你的 Twig 模板中,任何日期时间被格式化的地方:
// start is an instance of your extended DateTime object
{{ start.format('D d M')|capitalize }}
// Do 06 dec