Skip to content

Commit

Permalink
feat(l10n): Redirect routes without locale using a LocaleListener
Browse files Browse the repository at this point in the history
That way we don't need extra fallback routes for routes without locale
and can process both routes with and without locale in one function.
  • Loading branch information
doobry-systemli committed Mar 29, 2024
1 parent 2d61057 commit a3e17c9
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 102 deletions.
5 changes: 5 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ services:
tags:
- { name: kernel.event_subscriber }

App\EventListener\LocaleListener:
arguments:
$defaultLocale: "%locale%"
$supportedLocales: "%supported_locales%"

App\Handler\:
resource: '../src/Handler/*'
public: true
Expand Down
4 changes: 2 additions & 2 deletions features/login.feature
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ Feature: Login
@logout
Scenario: Logout
When I am authenticated as "[email protected]"
And I am on "/logout"
And I am on "/en/logout"

When I am on "/admin/dashboard"
Then I should be on "/en/login"

@logout
Scenario: Logout
When I am authenticated as "[email protected]"
And I am on "/logout"
And I am on "/en/logout"

Then I should see text matching "You are now logged out."

Expand Down
2 changes: 1 addition & 1 deletion features/registration.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Feature: registration
And I should see text matching "The following recovery token got created for you"
And I should see text matching regex "/^[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}+$/" in element with selector ".recovery-token"

When I am on "/logout"
When I am on "/en/logout"
Then I should be on "/en/"

When I am on "/en/login"
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/DeleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(private readonly DeleteHandler $deleteHandler, priva
* @param $aliasId
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/alias/delete/{aliasId}', name: 'alias_delete')]
#[Route(path: '/{_locale}/alias/delete/{aliasId}', name: 'alias_delete')]
public function deleteAlias(Request $request, $aliasId): Response
{
$user = $this->getUser();
Expand Down Expand Up @@ -67,7 +67,7 @@ public function deleteAlias(Request $request, $aliasId): Response
* @param Request $request
* @return RedirectResponse|Response
*/
#[Route(path: '/{_locale<%locales%>}/user/delete', name: 'user_delete')]
#[Route(path: '/{_locale}/user/delete', name: 'user_delete')]
public function deleteUser(Request $request): RedirectResponse|Response
{
$user = $this->getUser();
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/InitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function __construct(private readonly EntityManagerInterface $manager, pr
* @return Response
* @throws ValidationException
*/
#[Route(path: '/{_locale<%locales%>}/init', name: 'init')]
#[Route(path: '/init', name: 'init_no_locale')]
#[Route(path: '/{_locale}/init', name: 'init')]
public function index(Request $request): Response
{
// redirect if already configured
Expand Down Expand Up @@ -63,7 +64,7 @@ public function index(Request $request): Response
* @param Request $request
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/init/user', name: 'init_user')]
#[Route(path: '/{_locale}/init/user', name: 'init_user')]
public function user(Request $request): Response
{
// redirect if already configured
Expand Down
25 changes: 6 additions & 19 deletions src/Controller/RecoveryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,13 @@ public function __construct(
{
}

/**
* @param Request $request
* @return RedirectResponse
*/
#[Route(path: '/recovery', name: 'recovery_no_locale')]
public function recoveryNoLocale(Request $request): RedirectResponse
{
$supportedLocales = (array)$this->getParameter('supported_locales');
$preferredLanguage = $request->getPreferredLanguage($supportedLocales);
$locale = $preferredLanguage ?: $request->getLocale();

return $this->redirectToRoute('recovery', ['_locale' => $locale]);
}

/**
* @param Request $request
* @return Response
* @throws Exception
*/
#[Route(path: '/{_locale<%locales%>}/recovery', name: 'recovery')]
#[Route(path: '/recovery', name: 'recovery_no_locale')]
#[Route(path: '/{_locale}/recovery', name: 'recovery')]
public function recoveryProcess(Request $request): Response
{
$recoveryProcess = new RecoveryProcess();
Expand Down Expand Up @@ -123,7 +110,7 @@ public function recoveryProcess(Request $request): Response
* @return Response
* @throws Exception
*/
#[Route(path: '/{_locale<%locales%>}/recovery/reset_password', name: 'recovery_reset_password')]
#[Route(path: '/{_locale}/recovery/reset_password', name: 'recovery_reset_password')]
public function recoveryResetPassword(Request $request): Response
{
$recoveryResetPassword = new RecoveryResetPassword();
Expand Down Expand Up @@ -204,7 +191,7 @@ public function recoveryResetPassword(Request $request): Response
* @return Response
* @throws Exception
*/
#[Route(path: '/{_locale<%locales%>}/user/recovery_token', name: 'user_recovery_token')]
#[Route(path: '/{_locale}/user/recovery_token', name: 'user_recovery_token')]
public function recoveryToken(Request $request): Response
{
if (null === $user = $this->getUser()) {
Expand Down Expand Up @@ -273,7 +260,7 @@ public function recoveryToken(Request $request): Response
* @param Request $request
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/recovery/recovery_token/ack', name: 'recovery_recovery_token_ack')]
#[Route(path: '/{_locale}/recovery/recovery_token/ack', name: 'recovery_recovery_token_ack')]
public function recoveryRecoveryTokenAck(Request $request): Response
{
$recoveryTokenAck = new RecoveryTokenAck();
Expand Down Expand Up @@ -311,7 +298,7 @@ public function recoveryRecoveryTokenAck(Request $request): Response
* @param Request $request
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/user/recovery_token/ack', name: 'user_recovery_token_ack')]
#[Route(path: '/{_locale}/user/recovery_token/ack', name: 'user_recovery_token_ack')]
public function recoveryTokenAck(Request $request): Response
{
$recoveryTokenAck = new RecoveryTokenAck();
Expand Down
10 changes: 6 additions & 4 deletions src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(private readonly RegistrationHandler $registrationHa
* @param Request $request
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/register/recovery_token', name: 'register_recovery_token')]
#[Route(path: '/{_locale}/register/recovery_token', name: 'register_recovery_token')]
public function registerRecoveryTokenAck(Request $request): Response
{
$recoveryTokenAck = new RecoveryTokenAck();
Expand Down Expand Up @@ -58,7 +58,7 @@ public function registerRecoveryTokenAck(Request $request): Response
* @param Request $request
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/register/welcome', name: 'register_welcome')]
#[Route(path: '/{_locale}/register/welcome', name: 'register_welcome')]
public function welcome(Request $request): Response
{
$request->getSession()->getFlashBag()->add('success', 'flashes.registration-successful');
Expand All @@ -72,8 +72,10 @@ public function welcome(Request $request): Response
* @return Response
* @throws Exception
*/
#[Route(path: '/{_locale<%locales%>}/register', name: 'register')]
#[Route(path: '/{_locale<%locales%>}/register/{voucher}', name: 'register_voucher')]
#[Route(path: '/register', name: 'register_no_locale')]
#[Route(path: '/{_locale}/register', name: 'register')]
#[Route(path: '/register/{voucher}', name: 'register_voucher_no_locale')]
#[Route(path: '/{_locale}/register/{voucher}', name: 'register_voucher')]
public function register(Request $request, string $voucher = null): Response
{
if (!$this->registrationHandler->isRegistrationOpen()) {
Expand Down
20 changes: 4 additions & 16 deletions src/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,12 @@

class SecurityController extends AbstractController
{
/**
* @param Request $request
* @return RedirectResponse
*/
#[Route(path: '/login', name: 'login_no_locale')]
public function indexNoLocale(Request $request): RedirectResponse
{
$supportedLocales = (array)$this->getParameter('supported_locales');
$preferredLanguage = $request->getPreferredLanguage($supportedLocales);
$locale = $preferredLanguage ?: $request->getLocale();

return $this->redirectToRoute('login', ['_locale' => $locale]);
}

/**
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/login', name: 'login')]
#[Route(path: '/login', name: 'login_no_locale')]
#[Route(path: '/{_locale}/login', name: 'login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
Expand All @@ -46,7 +33,8 @@ public function login(AuthenticationUtils $authenticationUtils): Response
/**
* @return void
*/
#[Route(path: '/logout', name: 'logout', methods: ['GET'])]
#[Route(path: '/logout', name: 'logout_no_locale', methods: ['GET'])]
#[Route(path: '/{_locale}/logout', name: 'logout', methods: ['GET'])]
public function logout(): void
{

Expand Down
42 changes: 8 additions & 34 deletions src/Controller/StartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,11 @@ public function __construct(
{
}

/**
* @param Request $request
* @return RedirectResponse
*/
#[Route(path: '/', name: 'index_no_locale')]
public function indexNoLocale(Request $request): RedirectResponse
{
$supportedLocales = (array)$this->getParameter('supported_locales');
$preferredLanguage = $request->getPreferredLanguage($supportedLocales);
$locale = $preferredLanguage ?: $request->getLocale();

return $this->redirectToRoute('index', ['_locale' => $locale]);
}

/**
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/', name: 'index')]
#[Route(path: '/', name: 'index_no_locale')]
#[Route(path: '/{_locale}/', name: 'index')]
public function index(): Response
{
// forward to start if logged in
Expand All @@ -82,24 +69,11 @@ public function index(): Response
return $this->render('Start/index_anonymous.html.twig');
}

/**
* @param Request $request
* @return RedirectResponse
*/
#[Route(path: '/start', name: 'start_no_locale')]
public function startNoLocale(Request $request): RedirectResponse
{
$supportedLocales = (array)$this->getParameter('supported_locales');
$preferredLanguage = $request->getPreferredLanguage($supportedLocales);
$locale = $preferredLanguage ?: $request->getLocale();

return $this->redirectToRoute('start', ['_locale' => $locale]);
}

/**
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/start', name: 'start')]
#[Route(path: '/start', name: 'start_no_locale')]
#[Route(path: '/{_locale}/start', name: 'start')]
public function start(): Response
{
if ($this->isGranted(Roles::SPAM)) {
Expand All @@ -122,7 +96,7 @@ public function start(): Response
* @param Request $request
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/voucher', name: 'vouchers', requirements: ['_locale' => '%locales%'])]
#[Route(path: '/{_locale}/voucher', name: 'vouchers')]
public function voucher(Request $request): Response
{
/** @var User $user */
Expand Down Expand Up @@ -162,7 +136,7 @@ public function voucher(Request $request): Response
* @param Request $request
* @return Response
*/
#[Route(path: '/{_locale<%locales%>}/alias', name: 'aliases')]
#[Route(path: '/{_locale}/alias', name: 'aliases')]
public function alias(Request $request): Response
{
/** @var User $user */
Expand Down Expand Up @@ -224,7 +198,7 @@ public function alias(Request $request): Response
* @return Response
* @throws Exception
*/
#[Route(path: '/{_locale<%locales%>}/account', name: 'account')]
#[Route(path: '/{_locale}/account', name: 'account')]
public function account(Request $request): Response
{
/** @var User $user */
Expand Down Expand Up @@ -264,7 +238,7 @@ public function account(Request $request): Response
* @param Request $request
* @return Response|null
*/
#[Route(path: '/{_locale<%locales%>}/openpgp', name: 'openpgp')]
#[Route(path: '/{_locale}/openpgp', name: 'openpgp')]
public function openPgp(Request $request): ?Response
{
/** @var User $user */
Expand Down
Loading

0 comments on commit a3e17c9

Please sign in to comment.