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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'cache_dir' => '%kernel.cache_dir%/translations',
'debug' => '%kernel.debug%',
],
'%kernel.enabled_locales%',
])
;

Expand Down
8 changes: 7 additions & 1 deletion src/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ final class Translator extends BaseTranslator implements WarmableInterface

private bool $resourcesLoaded = false;

/** @var array<string> */
private array $enabledLocales = [];

public function __construct(
TranslatorLoaderProviderInterface $loaderProvider,
TranslatorResourceProviderInterface $resourceProvider,
MessageFormatterInterface $messageFormatter,
string $locale,
array $options = [],
array $enabledLocales = [],
) {
$this->assertOptionsAreKnown($options);

$this->loaderProvider = $loaderProvider;
$this->resourceProvider = $resourceProvider;
$this->enabledLocales = $enabledLocales;

$this->options = array_merge($this->options, $options);
if (null !== $this->options['cache_dir'] && $this->options['debug']) {
Expand All @@ -59,11 +64,12 @@ public function warmUp($cacheDir, ?string $buildDir = null): array
return [];
}

$locales = array_merge(
$locales = $this->enabledLocales ?: array_merge(
$this->getFallbackLocales(),
[$this->getLocale()],
$this->resourceProvider->getResourcesLocales(),
);

foreach (array_unique($locales) as $locale) {
// reset catalogue in case it's already loaded during the dump of the other locales.
if (isset($this->catalogues[$locale])) {
Expand Down
17 changes: 15 additions & 2 deletions tests/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ public function it_creates_a_nested_catalogue_with_fallback_translations(): void
$this->assertEquals($catalogue, $translator->getCatalogue());
}

/**
* @test
*/
public function it_does_not_load_unused_locales(): void
{
$translator = $this->createTranslator('en', enabledLocales: ['en']);
$translator->setFallbackLocales(['en']);

$this->assertNotNull($translator->getCatalogue());
$this->assertCount(1, $translator->getCatalogues());
}

public static function getInvalidLocalesTests(): array
{
return [
Expand Down Expand Up @@ -351,13 +363,14 @@ public static function getInvalidOptionsTests(): array

/**
* @param string[] $options
* @param string[] $enabledLocales
*/
private function createTranslator(string $locale = 'en', array $options = []): Translator
private function createTranslator(string $locale = 'en', array $options = [], array $enabledLocales = []): Translator
{
$loaderProvider = new TranslatorLoaderProvider();
$resourceProvider = new SymfonyTranslatorResourceProvider();
$messageFormatter = new MessageFormatter();

return new Translator($loaderProvider, $resourceProvider, $messageFormatter, $locale, $options);
return new Translator($loaderProvider, $resourceProvider, $messageFormatter, $locale, $options, $enabledLocales);
}
}