Search code examples
symfonysymfony-translator

How to update the invalid CSRF token message in Symfony 7


Using Symfony 7 with symfony/translation installed, I'm trying to update the message that gets displayed when my login form's CSRF token is invalid. Currently it reads "Invalid CSRF token." which my users will not have any clue about.

I have my /config/packages/translation.yaml file configured like:

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

Then in /translations/validators.en.yaml I have:

csrf_token:
  invalid: "Wakka wakka"

I haven't been able to find any documentation on what exacly the key(s) in the .yaml file should be - what I've got is just a ChatGPT suggestion.

I've cleared my cache, but I'm not seeing the desired message. What have I misconfigured?

Update
As @Jakumi suggested, I updated my yaml file to

"Invalid CSRF token." : "wakka wakka"

with no luck.

I also tried adding a security.en.xlf file in that directory with my translation, and also renaming my yaml file to security.en.yaml. Still no luck.


Solution

  • Sometimes the answer is astonishingly straight-forward: The translation key is the string "Invalid CSRF token." (see Security component translations), in symfony 7 it's this string and in the security "domain". In other versions of symfony it might be somewhere else.

    Generally, it's advisable to just search for the string in the codebase and if it is translated at all the translation must be in some translation files.

    Then, to translate it, follow the instructions. Essentially it just has to be valid yaml, if you're using yaml. As far as I can tell this should be (YAML):

    "Invalid CSRF token.": "Whatever the translation is."
    

    Some quotes can be omitted, but I don't want to go into detail.

    To check that your translation in works in general, open up a template and add {{ 'Invalid CSRF token.'|trans({}, 'security') (the string and the domain might differ!)