8000 [Translation] Introduce a way to configure the enabled locales · Pchol/symfony@7658434 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7658434

Browse files
javiereguiluznicolas-grekas
authored andcommitted
[Translation] Introduce a way to configure the enabled locales
1 parent 6c16390 commit 7658434

File tree

6 files changed

+60
-12
lines changed

6 files changed

+60
-12
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,16 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
677677
->arrayNode('paths')
678678
->prototype('scalar')->end()
679679
->end()
680+
->arrayNode('enabled_locales')
681+
->prototype('scalar')
682+
->defaultValue([])
683+
->beforeNormalization()
684+
->always()
685+
->then(function ($config) {
686+
return array_unique((array) $config);
687+
})
688+
->end()
689+
->end()
680690
->end()
681691
->end()
682692
->end()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,8 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
10391039
$defaultOptions['cache_dir'] = $config['cache_dir'];
10401040
$translator->setArgument(4, $defaultOptions);
10411041

1042+
$translator->setArgument(6, $config['enabled_locales']);
1043+
10421044
$container->setParameter('translator.logging', $config['logging']);
10431045
$container->setParameter('translator.default_path', $config['default_path']);
10441046

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
1717
<argument key="debug">%kernel.debug%</argument>
1818
</argument>
19+
<argument type="collection" /> <!-- enabled locales -->
1920
<call method="setConfigCacheFactory">
2021
<argument type="service" id="config_cache_factory" />
2122
</call>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ protected static function getBundleDefaultConfig()
373373
'formatter' => 'translator.formatter.default',
374374
'paths' => [],
375375
'default_path' => '%kernel.project_dir%/translations',
376+
'enabled_locales' => [],
376377
],
377378
'validation' => [
378379
'enabled' => !class_exists(FullStack::class),

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function testTransWithCachingWithInvalidLocale()
109109

110110
public function testLoadResourcesWithoutCaching()
111111
{
112-
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
112+
$loader = new YamlFileLoader();
113113
$resourceFiles = [
114114
'fr' => [
115115
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
@@ -186,7 +186,7 @@ public function getDebugModeAndCacheDirCombinations()
186186

187187
public function testCatalogResourcesAreAddedForScannedDirectories()
188188
{
189-
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
189+
$loader = new YamlFileLoader();
190190
$resourceFiles = [
191191
'fr' => [
192192
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
@@ -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 = [])
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');
@@ -348,7 +348,7 @@ public function getTranslator($loader, $options = [], $loaderFomat = 'loader', $
348348

349349
public function testWarmup()
350350
{
351-
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
351+
$loader = new YamlFileLoader();
352352
$resourceFiles = [
353353
'fr' => [
354354
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
@@ -371,9 +371,34 @@ public function testWarmup()
371371
$this->assertEquals('répertoire', $translator->trans('folder'));
372372
}
373373

374+
public function testEnabledLocales()
375+
{
376+
$loader = new 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', []);
385+
$translator->setFallbackLocales(['fr']);
386+
$translator->warmup($this->tmpDir);
387+
388+
$this->assertCount(2, 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->assertCount(1, glob($this->tmpDir.'/catalogue.*.*.php'), 'Only the "fr" catalogue is generated.');
397+
}
398+
374399
public function testLoadingTranslationFilesWithDotsInMessageDomain()
375400
{
376-
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
401+
$loader = new YamlFileLoader();
377402
$resourceFiles = [
378403
'en' => [
379404
__DIR__.'/../Fixtures/Resources/translations/domain.with.dots.en.yml',
@@ -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 = [])
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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ class Translator extends BaseTranslator implements WarmableInterface
5757
*/
5858
private $scannedDirectories;
5959

60+
/**
61+
* @var string[]
62+
*/
63+
private $enabledLocales;
64+
6065
/**
6166
* Constructor.
6267
*
@@ -69,10 +74,11 @@ class Translator extends BaseTranslator implements WarmableInterface
6974
*
7075
* @throws InvalidArgumentException
7176
*/
72-
public function __construct(ContainerInterface $container, MessageFormatterInterface $formatter, string $defaultLocale, array $loaderIds = [], array $options = [])
77+
public function __construct(ContainerInterface $container, MessageFormatterInterface $formatter, string $defaultLocale, array $loaderIds = [], array $options = [], array $enabledLocales = [])
7378
{
7479
$this->container = $container;
7580
$this->loaderIds = $loaderIds;
81+
$this->enabledLocales = $enabledLocales;
7682

7783
// check option names
7884
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
@@ -97,8 +103,9 @@ public function warmUp(string $cacheDir)
97103
return;
98104
}
99105

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

0 commit comments

Comments
 (0)
0