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

@ -15,24 +15,31 @@ Messages to users, flashbags and buttons
Flashbags Flashbags
========== ==========
The four following levels are defined : The four following levels are defined :
+-----------+----------------------------------------------------------------------------------------------+ +-----------+----------------------------------------------------------------------------------------------+
|Key |Intent | |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. | |success |The user action succeeds. |
+-----------+----------------------------------------------------------------------------------------------+ +-----------+----------------------------------------------------------------------------------------------+
|notice |A simple message to give information to the user. The message may be linked or not linked with| |notice |A simple message to give information to the user. The message may be linked or not linked with|
| |the user action. | | |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. | |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:: .. seealso::
`Flash Messages on Symfony documentation <http://symfony.com/doc/current/book/controller.html#flash-messages>`_ `Flash Messages on Symfony documentation <http://symfony.com/doc/current/book/controller.html#flash-messages>`_
@ -66,7 +73,7 @@ To add the action on button, use them as class along with ``sc-button`` :
| | | - Submitting this form will remove the entity | | | | - Submitting this form will remove the entity |
+-----------+----------------+------------------------------------------------------------------------------+ +-----------+----------------+------------------------------------------------------------------------------+
| Edit | ``bt-edit`` or | Link to a form to edit an entity | | Edit | ``bt-edit`` or | Link to a form to edit an entity |
| | ``bt-update`` | | | | ``bt-update`` | |
+-----------+----------------+------------------------------------------------------------------------------+ +-----------+----------------+------------------------------------------------------------------------------+
| Save | ``bt-save`` | Submitting this form will save change on the entity | | Save | ``bt-save`` | Submitting this form will save change on the entity |
+-----------+----------------+------------------------------------------------------------------------------+ +-----------+----------------+------------------------------------------------------------------------------+

View File

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

View File

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