mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
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:
parent
68b61b7d8a
commit
9124fa68e8
6
.changes/unreleased/DX-20250401-144728.yaml
Normal file
6
.changes/unreleased/DX-20250401-144728.yaml
Normal 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
|
@ -20,19 +20,26 @@ 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>`_
|
||||||
|
@ -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),
|
||||||
]
|
],
|
||||||
)
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 %}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user