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

问题描述 投票:2回答:3

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

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

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

我正在使用Symfony 2.8和3.0。

symfony csrf csrf-protection
3个回答
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
投票

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

    <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>

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


0
投票

如果要翻译特定表单,请在表单选项数组中传递已翻译的值 - csrf_message。 (至少在3.4 symfony上)。在创建时喜欢控制器

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

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

$_POST['checkout']['_token'] = '1';
© www.soinside.com 2019 - 2024. All rights reserved.