From 9124fa68e8a5deeae0b6b57eaadf9ef0ba8fb878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 1 Apr 2025 14:31:27 +0200 Subject: [PATCH] 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. --- .changes/unreleased/DX-20250401-144728.yaml | 6 ++++++ docs/source/development/messages-to-users.rst | 21 ++++++++++++------- .../Controller/SavedExportController.php | 15 ++++++------- .../views/layoutWithVerticalMenu.html.twig | 6 +++--- 4 files changed, 31 insertions(+), 17 deletions(-) create mode 100644 .changes/unreleased/DX-20250401-144728.yaml diff --git a/.changes/unreleased/DX-20250401-144728.yaml b/.changes/unreleased/DX-20250401-144728.yaml new file mode 100644 index 000000000..a08ba1760 --- /dev/null +++ b/.changes/unreleased/DX-20250401-144728.yaml @@ -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 diff --git a/docs/source/development/messages-to-users.rst b/docs/source/development/messages-to-users.rst index a935ee855..244638d0b 100644 --- a/docs/source/development/messages-to-users.rst +++ b/docs/source/development/messages-to-users.rst @@ -15,24 +15,31 @@ Messages to users, flashbags and buttons Flashbags ========== -The four following levels are defined : +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 `_ @@ -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 | +-----------+----------------+------------------------------------------------------------------------------+ | 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 | +-----------+----------------+------------------------------------------------------------------------------+ diff --git a/src/Bundle/ChillMainBundle/Controller/SavedExportController.php b/src/Bundle/ChillMainBundle/Controller/SavedExportController.php index b526ef798..753cb782d 100644 --- a/src/Bundle/ChillMainBundle/Controller/SavedExportController.php +++ b/src/Bundle/ChillMainBundle/Controller/SavedExportController.php @@ -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), - ] - ) + ], + ), ); } } diff --git a/src/Bundle/ChillMainBundle/Resources/views/layoutWithVerticalMenu.html.twig b/src/Bundle/ChillMainBundle/Resources/views/layoutWithVerticalMenu.html.twig index c192eaaac..cacd082c9 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/layoutWithVerticalMenu.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/layoutWithVerticalMenu.html.twig @@ -10,19 +10,19 @@ {% for flashMessage in app.session.flashbag.get('success') %}
- {{ flashMessage|raw }} + {{ flashMessage|trans }}
{% endfor %} {% for flashMessage in app.session.flashbag.get('error') %}
- {{ flashMessage|raw }} + {{ flashMessage|trans }}
{% endfor %} {% for flashMessage in app.session.flashbag.get('notice') %}
- {{ flashMessage|raw }} + {{ flashMessage|trans }}
{% endfor %}