Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand Down Expand Up @@ -45,7 +46,7 @@ public function loadCurrentLocale(): string
return $localeCode;
}

$preferredLanguage = $request->getPreferredLanguage();
$preferredLanguage = $request->getPreferredLanguage($this->enabledLocales);

return empty($preferredLanguage) ? $this->defaultLocale : $preferredLanguage;
}
Expand Down
23 changes: 10 additions & 13 deletions tests/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ 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}
*/
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);
}
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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]);

Expand All @@ -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())
Expand All @@ -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;

Expand Down Expand Up @@ -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
}
}