8000 feature #32433 [Translation] Introduce a way to configure the enabled… · symfony/symfony@f23aa96 · GitHub
[go: up one dir, main page]

Skip to content

Commit f23aa96

Browse files
committed
feature #32433 [Translation] Introduce a way to configure the enabled locales (javiereguiluz)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Translation] Introduce a way to configure the enabled locales | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #31563 | License | MIT | Doc PR | - This implements the idea #31563 so we can decide if we want to add this or not. I tested it in the "Symfony Demo" app. Before: 107 catalogs created in cache/dev/translations/. After: 43 catalogs. But that's because the app is translated into lots of languages. In most cases, only 2 catalog files will be generated (vs 107 before). If this idea is approved, I'll add tests and docs. Thanks. Commits ------- 7658434 [Translation] Introduce a way to configure the enabled locales
2 parents 61774e6 + 7658434 commit f23aa96

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
@@ -686,6 +686,16 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
686686
->arrayNode('paths')
687687
->prototype('scalar')->end()
688688
->end()
689+
->arrayNode('enabled_locales')
690+
->prototype('scalar')
691+
->defaultValue([])
692+
->beforeNormalization()
693+
->always()
694+
->then(function ($config) {
695+
return array_unique((array) $config);
696+
})
697+
->end()
698+
->end()
689699
->end()
690700
->end()
691701
->end()

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

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

1069+
$translator->setArgument(6, $config['enabled_locales']);
1070+
10691071
$container->setParameter('translator.logging', $config['logging']);
10701072
$container->setParameter('translator.default_path', $config['default_path']);
10711073

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();
19 341A 0190
$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