我遇到了Symfony2翻译的问题。
我有一个DateTimeFormatter类(这里简化):
<?php
namespace Project\CommonBundle\Util;
use DateTime;
use Symfony\Component\Translation\TranslatorInterface;
class DateTimeFormatter
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function format(DateTime $datetime, array $options = array())
{
$delta = time() - $datetime->getTimestamp();
return $this->translator->trans('datetime_formatter.seconds_ago', array('%seconds%', $delta);
}
}
使用翻译文件:
datetime_formatter:
seconds_ago: %seconds% seconds ago
我将其配置为服务:
parameters:
datetime_formatter.class: Skepin\CommonBundle\Util\DateTimeFormatter
services:
common.util.datetime_formatter:
class: %datetime_formatter.class%
arguments: [@translator]
问题是:当我使用它时,显示的文本是%seconds% seconds ago
,参数不会被替换。我错过了什么?
谢谢!
答案 0 :(得分:1)
问题已解决:
return $this->translator->trans('datetime_formatter.seconds_ago', array('%seconds%' => $delta);
而不是:
return $this->translator->trans('datetime_formatter.seconds_ago', array('%seconds%', $delta);
愚蠢的错误......