From 0dbbbb677310f0a26739112a6d980c6d6db75aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20H=C3=A9lias?= Date: Tue, 27 Aug 2024 11:10:28 +0200 Subject: [PATCH] Current locale must be one of the kernel.enabled_locales --- src/Resources/config/services.yml | 1 + src/Translation/Translator.php | 5 +++-- tests/Translation/TranslatorTest.php | 23 ++++++++++------------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 6ee79dc..cedb0ad 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -5,6 +5,7 @@ services: - '@translator' - '@request_stack' - '%kernel.default_locale%' + - '%kernel.enabled_locales%' locastic_api_platform_translation.listener.assign_locale: class: Locastic\ApiPlatformTranslationBundle\EventListener\AssignLocaleListener diff --git a/src/Translation/Translator.php b/src/Translation/Translator.php index cc5e408..6ff02fa 100644 --- a/src/Translation/Translator.php +++ b/src/Translation/Translator.php @@ -15,7 +15,8 @@ class Translator implements TranslatorInterface public function __construct( private TranslatorInterface $translator, private RequestStack $requestStack, - private string $defaultLocale + private string $defaultLocale, + private array $enabledLocales ) { } @@ -45,7 +46,7 @@ public function loadCurrentLocale(): string return $localeCode; } - $preferredLanguage = $request->getPreferredLanguage(); + $preferredLanguage = $request->getPreferredLanguage($this->enabledLocales); return empty($preferredLanguage) ? $this->defaultLocale : $preferredLanguage; } diff --git a/tests/Translation/TranslatorTest.php b/tests/Translation/TranslatorTest.php index 62d68cd..e4b6ad6 100644 --- a/tests/Translation/TranslatorTest.php +++ b/tests/Translation/TranslatorTest.php @@ -20,6 +20,7 @@ class TranslatorTest extends TestCase private TranslatorInterface|\PHPUnit\Framework\MockObject\MockObject $translator; private \PHPUnit\Framework\MockObject\MockObject|RequestStack $requestStack; private string $defaultLocale; + private array $enabledLocales; /** * {@inheritdoc} @@ -27,6 +28,7 @@ class TranslatorTest extends TestCase protected function setUp(): void { $this->defaultLocale = 'en'; + $this->enabledLocales = ['en', 'hr', 'fr', 'it']; $this->translator = $this->createMock(TranslatorInterface::class); $this->requestStack = $this->createMock(RequestStack::class); } @@ -42,7 +44,7 @@ public function testTranslate( $locale, $translation ): void { - $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale); + $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale, $this->enabledLocales); $this->translator ->expects($this->once()) @@ -64,7 +66,7 @@ public function testTranslateWithNoLocale( $domain, $translation ): void { - $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale); + $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale, $this->enabledLocales); $this->translator ->expects($this->once()) @@ -84,7 +86,7 @@ public function testLoadCurrentLocale( $requestedLocale, $expectedLocale ): void { - $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale); + $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale, $this->enabledLocales); $request = new Request(['locale' => $requestedLocale]); @@ -106,16 +108,9 @@ public function testLoadAcceptedLanguagesHeader( $acceptedLanguage, $expectedLocale ): void { - $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale); + $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale, $this->enabledLocales); - $request = $this->getMockBuilder(Request::class) - ->setConstructorArgs([ - ['locale' => $requestedLocale] - ]) - ->onlyMethods(['getPreferredLanguage']) - ->getMock(); - - $request->method('getPreferredLanguage')->willReturn($acceptedLanguage); + $request = new Request(['locale' => $requestedLocale], [], [], [], [], ['HTTP_ACCEPT-LANGUAGE' => $acceptedLanguage]); $this->requestStack ->expects($this->once()) @@ -131,7 +126,7 @@ public function testLoadAcceptedLanguagesHeader( */ public function testLoadCurrentLocaleWithNoRequest(): void { - $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale); + $translator = new Translator($this->translator, $this->requestStack, $this->defaultLocale, $this->enabledLocales); $request = null; @@ -182,5 +177,7 @@ public function provideLocalesWithAcceptLanguage(): \Generator yield['nl', null, 'nl']; yield['', '', 'en']; yield[null, null, 'en']; + yield[null, 'fr_FR', 'fr']; + yield[null, 'es', 'en']; // Local not enabled } }