我想显示日期格式,例如:10月09日, 周三
(表示 10/9,星期三(我的数据是 UNIX 时间,如 [message.time]:1380813820000
所以这就是我在 twig:
中所做的 {{ (message.time/1000)|date("m月d日, 周w") }}
但它告诉我:10月09日,周3
,因为日期"w"是数字,而不是中文文本.
那么我可以通过 Twig 做任何事情来转换文本格式吗?
谢谢
根本问题是,Twig 使用 DateTime::format
方法,该方法不支持语言环境或(据我所知(任何其他类型的功能来翻译工作日的名称。
有三种解决方案:
- 使用
strftime
,它支持区域设置(因此支持本地化的工作日名称(。 - 如果你可以使用PHP的
intl
扩展,那么你可以使用Twig扩展,它带有Twig的intl
扩展。 - 你自己翻译工作日。
另外,要在 Twig 模板中使用您喜欢的解决方案,您必须扩展 Twig 的功能。
使用 strftime 和 setlocale
以下(相当大的(代码实现了strftime
解决方案:
<?php
// inspired by phpdude:
// https://github.com/fabpot/twig/issues/378#issuecomment-4698225
class DateTimeHelper_Twig extends Twig_Extension
{
public function getFilters()
{
return array(
'datetime' => new Twig_Filter_Method($this, 'datetime',
array('needs_environment' => true)),
);
}
// This uses `strftime` which makes use of the locale. The format is not
// compatible with the one of date() or DateTime::format().
public function datetime(Twig_Environment $env, $date,
$format = "%B %e, %Y %H:%M", $timezone = null)
{
$date = twig_date_converter($env, $date, $timezone);
return strftime($format, $date->getTimestamp());
}
public function getName()
{
return 'DateTimeHelper';
}
}
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
// Call the setlocale before you use the `datetime` in your templates.
// This only needs to be done once per request.
// If you already have a locale configured in your environment,
// you can replace this with setlocale(LC_TIME, ""); - that way the
// locale of your environment is used.
setlocale(LC_TIME, "zh_CN.UTF-8");
// Add the extension to Twig like that:
$twig->addExtension(new DateTimeHelper_Twig());
$message = array('time' => time() * 1000);
// use the `datetime` filter with %a which gets replaced by the short weekday name of
// the current locale.
echo $twig->render('{{ (message.time/1000)|datetime("%m月%d日, 周%a") }}',
array('message' => $message)), PHP_EOL;
这段代码在我的系统上显示10月09日, 周三
(在我安装了 debian 软件包之后locales-all
;-((。
当然,区域设置附带了您可能需要注意的限制列表:
- 您必须使用正确的语言环境(可能使用 UTF-8(,并且您需要的语言环境必须安装在使用您的代码的所有系统上。
- 此外,此解决方案并非完全独立于平台(在Windows上
setlocale
工作方式不同/给出不同的结果(。查看 PHP 手册setlocale
- 很容易把事情搞砸。
使用 intl 和 Twig 扩展
如果你可以使用intl
扩展和"Twig-extensions"包,你必须使用localizeddate
而不是date
:
// add the extension like that
$twig->addExtension(new Twig_Extensions_Extension_Intl());
$message = array('time' => time() * 1000);
echo $twig->render('{{ (message.time/1000)|localizeddate("none", "none", "zh", null, "MM月dd日, eee") }}', array('message' => $message)), PHP_EOL;
该代码还显示了10月09日, 周三
- 它甚至会自动添加周-thingie。
当然,这里的日期格式也不同 - 查看ICU用户指南。
如果 setlocale 函数无法在您的系统上工作,您可以尝试此代码。
<?php
require_once dirname(__FILE__).'/vendor/autoload.php';
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
$twig->addFilter(new Twig_SimpleFilter('format_date', function($value) {
$weekdays = array('日','一','二','三','四','五','六');
return sprintf("%s, 周%s", date("m月d日"), $weekdays[date("w")]);
}));
echo $twig->render('{{ time_at | format_date }}', array(
'time_at' => 1380813820000/1000
));
我找到了一个快速过滤器replace
,这是代码片段:
<div class="date"><span>{{ (create_time/1000)|date("m月d日,周D")|replace({'Mon':'一','Tue':'二','Wed':'三','Thu':'四','Fri':'五','Sat':'六','Sun':'日'}) }}</span></div>