From db03c9a9747702454292cb2cdb7412450371893c Mon Sep 17 00:00:00 2001 From: Hugo Monteiro Date: Mon, 23 Feb 2026 17:55:27 +0000 Subject: [PATCH] Fix bundle configuration compatibility with ENV variables Move the fallback locale injection from the DI extension (compile-time) to the TranslationDumper constructor (runtime), so that ENV variable placeholders are resolved before the comparison is made. Closes #364 --- .../BazingaJsTranslationExtension.php | 5 --- Dumper/TranslationDumper.php | 4 ++ .../TranslationDumperFallbackLocaleTest.php | 43 +++++++++++++++++++ Tests/Fixtures/app/config/base_config.yml | 2 +- 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 Tests/Dumper/TranslationDumperFallbackLocaleTest.php diff --git a/DependencyInjection/BazingaJsTranslationExtension.php b/DependencyInjection/BazingaJsTranslationExtension.php index 2859b28a..a3b84c98 100644 --- a/DependencyInjection/BazingaJsTranslationExtension.php +++ b/DependencyInjection/BazingaJsTranslationExtension.php @@ -32,11 +32,6 @@ public function load(array $configs, ContainerBuilder $container): void ->replaceArgument(6, $config['default_domain']) ->replaceArgument(7, $config['http_cache_time']); - // Add fallback locale to active locales if missing - if ($config['active_locales'] && !in_array($config['locale_fallback'], $config['active_locales'])) { - array_push($config['active_locales'], $config['locale_fallback']); - } - $container ->getDefinition('bazinga.jstranslation.translation_dumper') ->replaceArgument(3, $config['locale_fallback']) diff --git a/Dumper/TranslationDumper.php b/Dumper/TranslationDumper.php index f5d8d0ff..7b5856a5 100644 --- a/Dumper/TranslationDumper.php +++ b/Dumper/TranslationDumper.php @@ -79,6 +79,10 @@ public function __construct( $this->filesystem = $filesystem; $this->localeFallback = $localeFallback; $this->defaultDomain = $defaultDomain; + // Add fallback locale to active locales if missing + if ($activeLocales && !in_array($localeFallback, $activeLocales)) { + array_push($activeLocales, $localeFallback); + } $this->activeLocales = $activeLocales; $this->activeDomains = $activeDomains; } diff --git a/Tests/Dumper/TranslationDumperFallbackLocaleTest.php b/Tests/Dumper/TranslationDumperFallbackLocaleTest.php new file mode 100644 index 00000000..04609bc2 --- /dev/null +++ b/Tests/Dumper/TranslationDumperFallbackLocaleTest.php @@ -0,0 +1,43 @@ +createMock(Environment::class); + $finder = $this->createMock(TranslationFinder::class); + $filesystem = $this->createMock(Filesystem::class); + + return new TranslationDumper($twig, $finder, $filesystem, $localeFallback, 'messages', $activeLocales); + } + + public function testFallbackLocaleIsAddedToActiveLocalesWhenMissing() + { + $dumper = $this->createDumper('en', ['fr']); + + $this->assertContains('en', $dumper->getActiveLocales()); + $this->assertContains('fr', $dumper->getActiveLocales()); + } + + public function testFallbackLocaleIsNotDuplicatedWhenAlreadyPresent() + { + $dumper = $this->createDumper('en', ['fr', 'en']); + + $this->assertEquals(['fr', 'en'], $dumper->getActiveLocales()); + } + + public function testFallbackLocaleIsNotAddedWhenActiveLocalesIsEmpty() + { + $dumper = $this->createDumper('en', []); + + $this->assertEmpty($dumper->getActiveLocales()); + } +} diff --git a/Tests/Fixtures/app/config/base_config.yml b/Tests/Fixtures/app/config/base_config.yml index 25fb855e..35b1ee0a 100644 --- a/Tests/Fixtures/app/config/base_config.yml +++ b/Tests/Fixtures/app/config/base_config.yml @@ -15,7 +15,7 @@ twig: bazinga_js_translation: active_locales: - fr - - en + locale_fallback: en active_domains: - messages - numerics