Refactor flash message handling to use translatable messages

Replaced raw translations with Symfony's TranslatableMessage for flash messages in the controller. Updated Twig templates to use the `|trans` filter for consistency in rendering translations. This ensures better handling of multilingual content across the application.
This commit is contained in:
Julien Fastré 2025-04-01 14:31:27 +02:00
parent 68b61b7d8a
commit 9124fa68e8
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
4 changed files with 31 additions and 17 deletions

View File

@ -0,0 +1,6 @@
kind: DX
body: Allow TranslatableMessage in flash messages
time: 2025-04-01T14:47:28.814268801+02:00
custom:
Issue: ""
SchemaChange: No schema change

View File

@ -20,19 +20,26 @@ The four following levels are defined :
+-----------+----------------------------------------------------------------------------------------------+
|Key |Intent |
+===========+==============================================================================================+
|alert |A message not linked with the user action, but which should require an action or a |
| |correction. |
+-----------+----------------------------------------------------------------------------------------------+
|success |The user action succeeds. |
+-----------+----------------------------------------------------------------------------------------------+
|notice |A simple message to give information to the user. The message may be linked or not linked with|
| |the user action. |
+-----------+----------------------------------------------------------------------------------------------+
|warning |A message linked with an action, the user should correct. |
+-----------+----------------------------------------------------------------------------------------------+
|error |The user's action failed: he must correct something to process the action. |
+-----------+----------------------------------------------------------------------------------------------+
We can use :code:`TranslatableMessage` (and other :code:`TranslatableMessageInterface` instances) into the controller:
.. code-block:: php
// in a controller action:
if (($session = $request->getSession()) instanceof Session) {
$session->getFlashBag()->add(
'success',
new TranslatableMessage('saved_export.Saved export is saved!')
);
}
.. seealso::
`Flash Messages on Symfony documentation <http://symfony.com/doc/current/book/controller.html#flash-messages>`_

View File

@ -32,6 +32,7 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Translation\TranslatorInterface;
final readonly class SavedExportController
@ -64,7 +65,7 @@ final readonly class SavedExportController
$session = $request->getSession();
if ($session instanceof Session) {
$session->getFlashBag()->add('success', $this->translator->trans('saved_export.Export is deleted'));
$session->getFlashBag()->add('success', new TranslatableMessage('saved_export.Export is deleted'));
}
return new RedirectResponse(
@ -111,7 +112,7 @@ final readonly class SavedExportController
$this->entityManager->flush();
if (($session = $request->getSession()) instanceof Session) {
$session->getFlashBag()->add('success', $this->translator->trans('saved_export.Saved export is saved!'));
$session->getFlashBag()->add('success', new TranslatableMessage('saved_export.Saved export is saved!'));
}
return new RedirectResponse(
@ -144,7 +145,7 @@ final readonly class SavedExportController
$this->entityManager->flush();
if (($session = $request->getSession()) instanceof Session) {
$session->getFlashBag()->add('success', $this->translator->trans('saved_export.Saved export is saved!'));
$session->getFlashBag()->add('success', new TranslatableMessage('saved_export.Saved export is saved!'));
}
return new RedirectResponse(
@ -157,8 +158,8 @@ final readonly class SavedExportController
'@ChillMain/SavedExport/edit.html.twig',
[
'form' => $form->createView(),
]
)
],
),
);
}
@ -194,8 +195,8 @@ final readonly class SavedExportController
[
'grouped_exports' => $exportsGrouped,
'total' => \count($exports),
]
)
],
),
);
}
}

View File

@ -10,19 +10,19 @@
{% for flashMessage in app.session.flashbag.get('success') %}
<div class="col-8 alert alert-success flash_message">
<span>{{ flashMessage|raw }}</span>
<span>{{ flashMessage|trans }}</span>
</div>
{% endfor %}
{% for flashMessage in app.session.flashbag.get('error') %}
<div class="col-8 alert alert-danger flash_message">
<span>{{ flashMessage|raw }}</span>
<span>{{ flashMessage|trans }}</span>
</div>
{% endfor %}
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="col-8 alert alert-warning flash_message">
<span>{{ flashMessage|raw }}</span>
<span>{{ flashMessage|trans }}</span>
</div>
{% endfor %}