如何在Symfony中翻译(和复制)CSRF消息?

时间:2016-01-18 05:21:11

标签: symfony csrf csrf-protection

我的部分用户在我的网络应用程序中遇到CSRF错误。他们告诉我他们不知道该怎么做。

为了获得更好的用户体验,我想翻译一下这条消息。谁能告诉我怎么做到这一点?

另外,我如何在我的开发环境中实际复制CSRF错误?

我正在使用Symfony 2.8和3.0。

3 个答案:

答案 0 :(得分:3)

请务必在验证工具validator.<lang>.<type>中添加您的翻译(不在消息中:messages),例如:

validator.en.yml

<trans-unit id="1">
    <source>The CSRF token is invalid. Please try to resubmit the form.</source>
    <target>The CSRF token is invalid. Please try to resubmit the form.</target>
</trans-unit>

编辑 - 每次更改翻译文件后刷新缓存

编辑 - 测试/复制行为:

您可以使用像firebug这样的工具进行编辑并更改_token表单隐藏元素并提交表单或暂时从表单中删除该字段。

希望这个帮助

答案 1 :(得分:1)

您可以将消息替换为翻译文件中添加的以下行

    <trans-unit id="1">
        <source>The CSRF token is invalid. Please try to resubmit the form.</source>
        <target>The CSRF token is invalid. Please try to resubmit the form.</target>
    </trans-unit>

在目标代码中,您可以更改自定义消息。如果对此有任何疑问,请告诉我

答案 2 :(得分:0)

如果要转换为特定格式,请在格式选项数组-csrf_message中传递转换后的值。 (至少在3.4 symfony上)。就像在创建控制器时一样

$options['csrf_message'] = 'Translated value'
$this->createForm(CheckoutType::class, $data, $options);

复制选项之一是修改找到前端控制器中的$ _POST数组。就像在app.php中一样

$_POST['checkout']['_token'] = '1';