1

在我的 Symfony 1.4 项目中,直到现在,以下是我在操作中使用 i18n setflash 消息的方式:

$message = $this->getContext()->getI18N()->__('message’);
$this->getUser()->setFlash('notice', $message);

但我刚刚找到了一种最简单的方法。这是我在项目配置类中使用的代码:

public function setup()
{
  $this->loadHelpers(array('I18N')); 
} 

然后是我在操作中使用的代码:

$this->getUser()->setFlash('error', __('message'));

对我来说,这很方便,因为我不必到处使用 getContext->getI18N 但我想知道使用这种方法是否有缺点?

4

1 回答 1

2

I always use this method in many projects and I didn't see any drawbacks.

Helpers are here to give us a library of useful functions. Some helper's functions return html so (impov) they are dedicated to the template, but in case of I18N or Date, it can be useful to have them in an action.

And if you don't want to load your helper in a global view, you can use:

sfApplicationConfiguration::getActive()->loadHelpers(array('I18N'));
于 2012-04-25T12:34:32.250 回答