symfonytranslationsymfony4flash-message

Translation of flash messages with parameters in Symfony 4


I have a problem with flash messages in Symfony 4 and translation. Translation of simple flash messages is working fine:

$this->addFlash('success', 'flashmessage.project_deleted');

But now I want to add some parameters to the flash messages and I have no idea how to handle it. I tried a lot, but nothing is working. I want to show in the flash messages the title of projects after f.e. removing. For example:

$this->addFlash('success', sprintf('flashmessage.project_deleted: %s', $project->getTitle()));

But the translation is not recognized, because the parameter is replaces before translation happens (I think so). And it should also be possible to have parameters in the middle of a string and not only at the end or at the beginning and ideally more than one parameter.

I'm using this in my Controller which extends AbstractController.

Does anybody has a solution for this?


Solution

  • Usually you would pass in the parameters to the translation, so your code snippet should probably look your first example and then in twig you would have something like this:

    {% for message in app.flashes('success') %}
        <div class="alert alert-success">
           {{ message|trans({ 'title': project.title }) }}
        </div>
    {% endfor %}
    

    The translation then should contain the parameter that is replaced:

    flashmessage:
         project_created: 'The project "%title%" was created successfully.'
         project_deleted: 'You successfully deleted the project "%title%".'
         ...
    

    Obviously the downside is that you have to dynamically pass in the variables which does not make much sense for flash messages, as not all of them will require these parameters. Also, as you already mentioned, when you deleted the project you will probably not have it available anymore in the template.

    Instead I would recommend translating the message before passing it into the flash bag:

    $this->addFlash(
        'success',
        $this->translator->translate(
            'flashmessage.project_deleted',
            [
                'title' => $project->getTitle(),
            ]
        )
    );
    

    This will require that you pass in the translator to your controller. You could either create your own base controller similar to Symfony's AbstractController for this and create something like a $this->trans()-method to make it easier to translate things inside your controller. Also, you will still have to make sure that $project->getTitle() will still return a value, so you probably want to call this before you actually delete the entry or have the data in memory.

    When you do it this way, then you should not translate the flash messages in the template itself because they are already translated. This will still work because when Symfony tries to translate the already translated message, e.g. You successfully deleted the project "foo". then it will not find a translation and just print the original text instead, but you will get warnings in your logs about missing translations. The solution is to remove the |trans in your template (see first snippet).