From 2955454edb601415b87966bae1ed0c4fda712914 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 11 Apr 2024 16:44:20 +0200 Subject: [PATCH] Refactor how we define the available locales --- config/packages/framework.yaml | 4 +++ config/packages/security.yaml | 2 +- config/routes.yaml | 4 --- config/services.yaml | 4 +-- .../RedirectToPreferredLocaleSubscriber.php | 27 +++++++------------ src/Twig/AppExtension.php | 16 ++++------- 6 files changed, 21 insertions(+), 36 deletions(-) diff --git a/config/packages/framework.yaml b/config/packages/framework.yaml index f3f77c688..9c09e4ca1 100644 --- a/config/packages/framework.yaml +++ b/config/packages/framework.yaml @@ -6,6 +6,10 @@ framework: http_method_override: false handle_all_throwables: true + # this defines the codes of the locales (languages) available in the application + # https://symfony.com/doc/current/reference/configuration/framework.html#enabled-locales + enabled_locales: ['ar', 'bg', 'bn', 'bs', 'ca', 'cs', 'de', 'en', 'es', 'eu', 'fr', 'hr', 'id', 'it', 'ja', 'lt', 'ne', 'nl', 'pl', 'pt_BR', 'ro', 'ru', 'sl', 'sq', 'sr_Cyrl', 'sr_Latn', 'tr', 'uk', 'vi', 'zh_CN'] + # Enables session support. Note that the session will ONLY be started if you read or write from it. # Remove or comment this section to explicitly disable session support. session: diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 6a4271bd0..47dd868fd 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -65,7 +65,7 @@ security: access_control: # this is a catch-all for the admin area # additional security lives in the controllers - - { path: '^/(%app_locales%)/admin', roles: ROLE_ADMIN } + - { path: '^/{_locale}/admin', roles: ROLE_ADMIN } # The ROLE_ADMIN role inherits from the ROLE_USER role role_hierarchy: diff --git a/config/routes.yaml b/config/routes.yaml index cff373ec0..e1a588cf7 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -5,8 +5,6 @@ homepage: path: /{_locale} controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction - requirements: - _locale: '%app_locales%' defaults: template: default/homepage.html.twig _locale: '%locale%' @@ -17,7 +15,5 @@ controllers: namespace: App\Controller type: attribute prefix: /{_locale} - requirements: - _locale: '%app_locales%' defaults: _locale: '%locale%' diff --git a/config/services.yaml b/config/services.yaml index 0153ceb79..179bb3182 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -5,8 +5,6 @@ # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration parameters: locale: 'en' - # This parameter defines the codes of the locales (languages) enabled in the application - app_locales: ar|en|fr|de|es|cs|nl|ru|uk|ro|pt_BR|pl|it|ja|id|ca|sl|sq|hr|zh_CN|bg|tr|lt|bs|sr_Cyrl|sr_Latn|eu|ne|bn|vi app.notifications.email_sender: anonymous@example.com services: @@ -18,7 +16,7 @@ services: # this allows to define the scalar arguments once and apply them to any services # defined/created in this file; if some argument is used rarely, instead of defining # it here you can use the #[Autowire] attribute to inject it manually in the service constructor - string $locales: '%app_locales%' + array $enabledLocales: '%kernel.enabled_locales%' string $defaultLocale: '%locale%' # makes classes in src/ available to be used as services diff --git a/src/EventSubscriber/RedirectToPreferredLocaleSubscriber.php b/src/EventSubscriber/RedirectToPreferredLocaleSubscriber.php index 5e6894794..b367a7b98 100644 --- a/src/EventSubscriber/RedirectToPreferredLocaleSubscriber.php +++ b/src/EventSubscriber/RedirectToPreferredLocaleSubscriber.php @@ -28,34 +28,27 @@ */ final class RedirectToPreferredLocaleSubscriber implements EventSubscriberInterface { - /** - * @var string[] - */ - private array $locales; - private readonly string $defaultLocale; - public function __construct( private readonly UrlGeneratorInterface $urlGenerator, - string $locales, - ?string $defaultLocale = null + /** @var string[] */ + private array $enabledLocales, + private ?string $defaultLocale = null ) { - $this->locales = explode('|', trim($locales)); - - if (empty($this->locales)) { + if (empty($this->enabledLocales)) { throw new \UnexpectedValueException('The list of supported locales must not be empty.'); } - $this->defaultLocale = $defaultLocale ?: $this->locales[0]; + $this->defaultLocale = $defaultLocale ?: $this->enabledLocales[0]; - if (!\in_array($this->defaultLocale, $this->locales, true)) { - throw new \UnexpectedValueException(sprintf('The default locale ("%s") must be one of "%s".', $this->defaultLocale, $locales)); + if (!\in_array($this->defaultLocale, $this->enabledLocales, true)) { + throw new \UnexpectedValueException(sprintf('The default locale ("%s") must be one of "%s".', $this->defaultLocale, implode(', ', $this->enabledLocales))); } // Add the default locale at the first position of the array, // because Symfony\HttpFoundation\Request::getPreferredLanguage // returns the first element when no an appropriate language is found - array_unshift($this->locales, $this->defaultLocale); - $this->locales = array_unique($this->locales); + array_unshift($this->enabledLocales, $this->defaultLocale); + $this->enabledLocales = array_unique($this->enabledLocales); } public static function getSubscribedEvents(): array @@ -81,7 +74,7 @@ public function onKernelRequest(RequestEvent $event): void return; } - $preferredLanguage = $request->getPreferredLanguage($this->locales); + $preferredLanguage = $request->getPreferredLanguage($this->enabledLocales); if ($preferredLanguage !== $this->defaultLocale) { $response = new RedirectResponse($this->urlGenerator->generate('homepage', ['_locale' => $preferredLanguage])); diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index f37967721..e6c44439e 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -24,11 +24,6 @@ */ final class AppExtension extends AbstractExtension { - /** - * @var string[] - */ - private readonly array $localeCodes; - /** * @var list|null */ @@ -36,11 +31,10 @@ final class AppExtension extends AbstractExtension // The $locales argument is injected thanks to the service container. // See https://symfony.com/doc/current/service_container.html#binding-arguments-by-name-or-type - public function __construct(string $locales) - { - $localeCodes = explode('|', $locales); - sort($localeCodes); - $this->localeCodes = $localeCodes; + public function __construct( + /** @var string[] */ + private array $enabledLocales, + ) { } public function getFunctions(): array @@ -65,7 +59,7 @@ public function getLocales(): array $this->locales = []; - foreach ($this->localeCodes as $localeCode) { + foreach ($this->enabledLocales as $localeCode) { $this->locales[] = ['code' => $localeCode, 'name' => Locales::getName($localeCode, $localeCode)]; }