Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 26e2656

Browse files
committed
Rebased and added a test
1 parent db637cb commit 26e2656

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,12 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
679679
->end()
680680
->variableNode('enabled_locales')
681681
->defaultValue(['%kernel.default_locale%'])
682+
->beforeNormalization()
683+
->always()
684+
->then(function ($config) {
685+
return array_unique((array) $config);
686+
})
687+
->end()
682688
->beforeNormalization()->ifString()->castToArray()->end()
683689
->end()
684690
->end()

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ protected function getContainer($loader)
329329
return $container;
330330
}
331331

332-
public function getTranslator($loader, $options = [], $loaderFomat = 'loader', $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $defaultLocale = 'en')
332+
public function getTranslator($loader, $options = [], $loaderFomat = 'loader', $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $defaultLocale = 'en', array $enabledLocales = null)
333333
{
334-
$translator = $this->createTranslator($loader, $options, $translatorClass, $loaderFomat, $defaultLocale);
334+
$translator = $this->createTranslator($loader, $options, $translatorClass, $loaderFomat, $defaultLocale, $enabledLocales);
335335

336336
if ('loader' === $loaderFomat) {
337337
$translator->addResource('loader', 'foo', 'fr');
@@ -371,6 +371,31 @@ public function testWarmup()
371371
$this->assertEquals('répertoire', $translator->trans('folder'));
372372
}
373373

374+
public function testEnabledLocales()
375+
{
376+
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
377+
$resourceFiles = [
378+
'fr' => [
379+
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
380+
],
381+
];
382+
383+
// prime the cache without configuring the enabled locales
384+
$translator = $this->getTranslator($loader, ['cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles], 'yml', Translator::class, 'en', null);
385+
$translator->setFallbackLocales(['fr']);
386+
$translator->warmup($this->tmpDir);
387+
388+
$this->assertSame(2, count(glob($this->tmpDir.'/catalogue.*.*.php')), 'Both "en" and "fr" catalogues are generated.');
389+
390+
// prime the cache and configure the enabled locales
391+
$this->deleteTmpDir();
392+
$translator = $this->getTranslator($loader, ['cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles], 'yml', Translator::class, 'en', ['fr']);
393+
$translator->setFallbackLocales(['fr']);
394+
$translator->warmup($this->tmpDir);
395+
396+
$this->assertSame(1, count(glob($this->tmpDir.'/catalogue.*.*.php')), 'Only the "fr" catalogue is generated.');
397+
}
398+
374399
public function testLoadingTranslationFilesWithDotsInMessageDomain()
375400
{
376401
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
@@ -386,14 +411,15 @@ public function testLoadingTranslationFilesWithDotsInMessageDomain()
386411
$this->assertEquals('It works!', $translator->trans('message', [], 'domain.with.dots'));
387412
}
388413

389-
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $loaderFomat = 'loader', $defaultLocale = 'en')
414+
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $loaderFomat = 'loader', $defaultLocale = 'en', array $enabledLocales = null)
390415
{
391416
if (null === $defaultLocale) {
392417
return new $translatorClass(
393418
$this->getContainer($loader),
394419
new MessageFormatter(),
395420
[$loaderFomat => [$loaderFomat]],
396-
$options
421+
$options,
422+
$enabledLocales
397423
);
398424
}
399425

@@ -402,7 +428,8 @@ private function createTranslator($loader, $options, $translatorClass = '\Symfon
402428
new MessageFormatter(),
403429
$defaultLocale,
404430
[$loaderFomat => [$loaderFomat]],
405-
$options
431+
$options,
432+
$enabledLocales
406433
);
407434
}
408435
}

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public function warmUp(string $cacheDir)
103103
return;
104104
}
105105

106-
$locales = array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales);
107-
$localesToWarmUp = array_unique($this->enabledLocales ?? $locales);
106+
$locales = array_unique(array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales));
107+
$localesToWarmUp = $this->enabledLocales ?? $locales;
108108
foreach ($localesToWarmUp as $locale) {
109109
// reset catalogue in case it's already loaded during the dump of the other locales.
110110
if (isset($this->catalogues[$locale])) {

0 commit comments

Comments
 (0)