Ajout de la traduction (#15)

Co-authored-by: hugo.pradier2 <hugo.pradier2@etu.uca.fr>
Co-authored-by: clfreville2 <clement.freville2@etu.uca.fr>
Reviewed-on: https://codefirst.iut.uca.fr/git/clement.freville2/herbarium/pulls/15
Reviewed-by: Clément FRÉVILLE <clement.freville2@etu.uca.fr>
Co-authored-by: Hugo PRADIER <hugo.pradier2@etu.uca.fr>
Co-committed-by: Hugo PRADIER <hugo.pradier2@etu.uca.fr>
This commit is contained in:
Hugo PRADIER
2024-06-13 08:46:30 +02:00
committed by Clément FRÉVILLE
parent ccb2b541ea
commit 8a1edeb938
31 changed files with 1919 additions and 94 deletions

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Translation\LocaleSwitcher;
class SettingController extends AbstractController
{
#[Route('/setting/{locale}', name: 'app_setting_locale')]
public function index(LocaleSwitcher $localeSwitcher, string $locale, Request $request): Response
{
$localeSwitcher->setLocale($locale);
$request->getSession()->set('_locale', $locale);
return $this->redirectToRoute('app_posts');
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Translation\LocaleSwitcher;
final readonly class LocaleListener
{
public function __construct(private LocaleSwitcher $localeSwitcher)
{
}
#[AsEventListener(event: KernelEvents::REQUEST)]
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
if ($request->attributes->getBoolean('_stateless')) {
return;
}
$locale = $request->getSession()->get('_locale');
if ($locale !== null) {
$this->localeSwitcher->setLocale($locale);
}
}
}