0
votes

I am developing a Vue.js application and want to handle the login via JSON login. This works already but I'd like to translate the error messages, e.g. "Invalid credentials."

My translation.yaml config file.

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

The Symfony Security component already has some German (de) translations in its translation directory:

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>

So I copied this file into my translations directory but still the english messages are being displayed. Also I always cleared my cache (thanks to Dylan's answer)

3
You may need to clear your cache (even in the dev environment). - Dylan Kas
I did that already, but it's still not working. - Felix G

3 Answers

0
votes

I think you might be displaying the message key which would always be in english. I guess you have to access translations and translate the message key.

In twig it looks like this:

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

In Vue.js I guess you'd have to expose the translations in JavaScript or JSON.

According to this documentation you can also throw a CustomUserMessageAuthenticationException at any point in your LoginAuthenticator and customize the messages the way you want, which would make it possible to translate security errors on the back-end.

0
votes

As I see, you should change the source-language="en" to source-language="de".

0
votes

Translate according to this documentation:

Create translation file translations/security.de.yaml and translate messages according to vendor/symfony/security-core/Resources/translations/security.de.xlf

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