tro如何翻译Symfony 4安全错误消息:"Invalid credentials"



我正在开发一个 Vue.js 应用程序,并希望通过 JSON 登录来处理登录。这已经有效了,但我想翻译错误消息,例如"凭据无效"。

我的translation.yaml配置文件。

framework:
    default_locale: de
    translator:
        default_path: '%kernel.project_dir%/translations'
        fallbacks:
            - en

Symfony Security组件在其翻译目录中已经有一些德语(de(翻译:

https://github.com/symfony/security-core/blob/master/Resources/translations/security.de.xlf

<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
        ...
            <trans-unit id="4">
                <source>Invalid credentials.</source>
                <target>Fehlerhafte Zugangsdaten.</target>
            </trans-unit>
        ...
        </body>
    </file>
</xliff>

所以我将此文件复制到我的translations目录中,但仍然显示英文消息。我也总是清除我的缓存(感谢迪伦的回答(

我想您可能会显示始终为英文的消息键。我想您必须访问翻译并翻译消息密钥。

在树枝中,它看起来像这样:

 <span class="form-error-message">{{ error.messageKey|trans(error.messageData, 'security') }}</span> 

在 Vue 中.js我想你必须公开 JavaScript 或 JSON 中的翻译。

根据本文档,您还可以在 LoginAuthenticator 中的任何位置抛出CustomUserMessageAuthenticationException,并按照您想要的方式自定义消息,这将使翻译后端的安全错误成为可能。

如我所见,您应该将source-language="en"更改为 source-language="de" .

根据此文档进行翻译:

创建翻译文件translations/security.de.yaml并根据vendor/symfony/security-core/Resources/translations/security.de.xlf翻译消息

'Invalid credentials.': 'The password you entered was invalid!'

最新更新