【问题标题】:Symfony2 : how to translate @Assert\Callback messageSymfony2:如何翻译@Assert\Callback 消息
【发布时间】:2013-04-13 02:19:03
【问题描述】:

我的实体中有一个回调方法

@Assert\Callback(methods={"isStudentsOK"}) 
....
public function isStudentsOK(ExecutionContext $context)
{
  if ( $this->students->isEmpty())
    $context->addViolationAtSubPath('students', 'Missing students informations', array(), null);
} 

我想翻译我模板中的信息。
{{ form_errors(form.student)|trans }} 不起作用... 我有这个

<span class="help-inline">Missing students informations</span>

这是一个错误吗?我该怎么办?

编辑

我自己找到了回复:

我已经安装了一个 BootstrapBundle,{% block form_errors %} 显示在一个 span 内

我已经覆盖了form_div_layout.html.twig 模板。

【问题讨论】:

  • 你应该写一个答案并将其标记为答案,而不是让这个问题保持打开状态。

标签: symfony twig translate


【解决方案1】:

您的代码使用了 |trans 过滤器,所以我假设您使用的是 Twig i18n 扩展,而该扩展又使用 PHP 的 gettext 函数。

如果您的 PHP 代码生成了消息,您不妨在那里进行翻译:

@Assert\Callback(methods={"isStudentsOK"}) 
....
public function isStudentsOK(ExecutionContext $context)
{
  if ( $this->students->isEmpty())
    $context->addViolationAtSubPath('students', _('Missing students informations'), array(), null);
} 

请注意错误消息中的下划线功能。这是“Trans”过滤器的 PHP 等效项。

【讨论】:

    猜你喜欢
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2012-05-10
    相关资源
    最近更新 更多