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:
20
src/Controller/SettingController.php
Normal file
20
src/Controller/SettingController.php
Normal 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');
|
||||
}
|
||||
}
|
28
src/EventListener/LocaleListener.php
Normal file
28
src/EventListener/LocaleListener.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user