2012-02-01 95 views
0

我遇到了Symfony2翻譯器的問題。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

問題解決:代替

return $this->translator->trans('datetime_formatter.seconds_ago', array('%seconds%' => $delta); 

return $this->translator->trans('datetime_formatter.seconds_ago', array('%seconds%', $delta); 

愚蠢的錯誤...