[Translation] Introduce a way to configure the enabled locales

This commit is contained in:
Javier Eguiluz 2019-07-08 15:51:16 +02:00 committed by Nicolas Grekas
parent 6c163906f2
commit 765843426e
6 changed files with 60 additions and 12 deletions

View File

@ -677,6 +677,16 @@ class Configuration implements ConfigurationInterface
->arrayNode('paths') ->arrayNode('paths')
->prototype('scalar')->end() ->prototype('scalar')->end()
->end() ->end()
->arrayNode('enabled_locales')
->prototype('scalar')
->defaultValue([])
->beforeNormalization()
->always()
->then(function ($config) {
return array_unique((array) $config);
})
->end()
->end()
->end() ->end()
->end() ->end()
->end() ->end()

View File

@ -1039,6 +1039,8 @@ class FrameworkExtension extends Extension
$defaultOptions['cache_dir'] = $config['cache_dir']; $defaultOptions['cache_dir'] = $config['cache_dir'];
$translator->setArgument(4, $defaultOptions); $translator->setArgument(4, $defaultOptions);
$translator->setArgument(6, $config['enabled_locales']);
$container->setParameter('translator.logging', $config['logging']); $container->setParameter('translator.logging', $config['logging']);
$container->setParameter('translator.default_path', $config['default_path']); $container->setParameter('translator.default_path', $config['default_path']);

View File

@ -16,6 +16,7 @@
<argument key="cache_dir">%kernel.cache_dir%/translations</argument> <argument key="cache_dir">%kernel.cache_dir%/translations</argument>
<argument key="debug">%kernel.debug%</argument> <argument key="debug">%kernel.debug%</argument>
</argument> </argument>
<argument type="collection" /> <!-- enabled locales -->
<call method="setConfigCacheFactory"> <call method="setConfigCacheFactory">
<argument type="service" id="config_cache_factory" /> <argument type="service" id="config_cache_factory" />
</call> </call>

View File

@ -373,6 +373,7 @@ class ConfigurationTest extends TestCase
'formatter' => 'translator.formatter.default', 'formatter' => 'translator.formatter.default',
'paths' => [], 'paths' => [],
'default_path' => '%kernel.project_dir%/translations', 'default_path' => '%kernel.project_dir%/translations',
'enabled_locales' => [],
], ],
'validation' => [ 'validation' => [
'enabled' => !class_exists(FullStack::class), 'enabled' => !class_exists(FullStack::class),

View File

@ -109,7 +109,7 @@ class TranslatorTest extends TestCase
public function testLoadResourcesWithoutCaching() public function testLoadResourcesWithoutCaching()
{ {
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader(); $loader = new YamlFileLoader();
$resourceFiles = [ $resourceFiles = [
'fr' => [ 'fr' => [
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml', __DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
@ -186,7 +186,7 @@ class TranslatorTest extends TestCase
public function testCatalogResourcesAreAddedForScannedDirectories() public function testCatalogResourcesAreAddedForScannedDirectories()
{ {
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader(); $loader = new YamlFileLoader();
$resourceFiles = [ $resourceFiles = [
'fr' => [ 'fr' => [
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml', __DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
@ -329,9 +329,9 @@ class TranslatorTest extends TestCase
return $container; return $container;
} }
public function getTranslator($loader, $options = [], $loaderFomat = 'loader', $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $defaultLocale = 'en') public function getTranslator($loader, $options = [], $loaderFomat = 'loader', $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $defaultLocale = 'en', array $enabledLocales = [])
{ {
$translator = $this->createTranslator($loader, $options, $translatorClass, $loaderFomat, $defaultLocale); $translator = $this->createTranslator($loader, $options, $translatorClass, $loaderFomat, $defaultLocale, $enabledLocales);
if ('loader' === $loaderFomat) { if ('loader' === $loaderFomat) {
$translator->addResource('loader', 'foo', 'fr'); $translator->addResource('loader', 'foo', 'fr');
@ -348,7 +348,7 @@ class TranslatorTest extends TestCase
public function testWarmup() public function testWarmup()
{ {
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader(); $loader = new YamlFileLoader();
$resourceFiles = [ $resourceFiles = [
'fr' => [ 'fr' => [
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml', __DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
@ -371,9 +371,34 @@ class TranslatorTest extends TestCase
$this->assertEquals('répertoire', $translator->trans('folder')); $this->assertEquals('répertoire', $translator->trans('folder'));
} }
public function testEnabledLocales()
{
$loader = new YamlFileLoader();
$resourceFiles = [
'fr' => [
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
],
];
// prime the cache without configuring the enabled locales
$translator = $this->getTranslator($loader, ['cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles], 'yml', Translator::class, 'en', []);
$translator->setFallbackLocales(['fr']);
$translator->warmup($this->tmpDir);
$this->assertCount(2, glob($this->tmpDir.'/catalogue.*.*.php'), 'Both "en" and "fr" catalogues are generated.');
// prime the cache and configure the enabled locales
$this->deleteTmpDir();
$translator = $this->getTranslator($loader, ['cache_dir' => $this->tmpDir, 'resource_files' => $resourceFiles], 'yml', Translator::class, 'en', ['fr']);
$translator->setFallbackLocales(['fr']);
$translator->warmup($this->tmpDir);
$this->assertCount(1, glob($this->tmpDir.'/catalogue.*.*.php'), 'Only the "fr" catalogue is generated.');
}
public function testLoadingTranslationFilesWithDotsInMessageDomain() public function testLoadingTranslationFilesWithDotsInMessageDomain()
{ {
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader(); $loader = new YamlFileLoader();
$resourceFiles = [ $resourceFiles = [
'en' => [ 'en' => [
__DIR__.'/../Fixtures/Resources/translations/domain.with.dots.en.yml', __DIR__.'/../Fixtures/Resources/translations/domain.with.dots.en.yml',
@ -386,14 +411,15 @@ class TranslatorTest extends TestCase
$this->assertEquals('It works!', $translator->trans('message', [], 'domain.with.dots')); $this->assertEquals('It works!', $translator->trans('message', [], 'domain.with.dots'));
} }
private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $loaderFomat = 'loader', $defaultLocale = 'en') private function createTranslator($loader, $options, $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator', $loaderFomat = 'loader', $defaultLocale = 'en', array $enabledLocales = [])
{ {
if (null === $defaultLocale) { if (null === $defaultLocale) {
return new $translatorClass( return new $translatorClass(
$this->getContainer($loader), $this->getContainer($loader),
new MessageFormatter(), new MessageFormatter(),
[$loaderFomat => [$loaderFomat]], [$loaderFomat => [$loaderFomat]],
$options $options,
$enabledLocales
); );
} }
@ -402,7 +428,8 @@ class TranslatorTest extends TestCase
new MessageFormatter(), new MessageFormatter(),
$defaultLocale, $defaultLocale,
[$loaderFomat => [$loaderFomat]], [$loaderFomat => [$loaderFomat]],
$options $options,
$enabledLocales
); );
} }
} }

View File

@ -57,6 +57,11 @@ class Translator extends BaseTranslator implements WarmableInterface
*/ */
private $scannedDirectories; private $scannedDirectories;
/**
* @var string[]
*/
private $enabledLocales;
/** /**
* Constructor. * Constructor.
* *
@ -69,10 +74,11 @@ class Translator extends BaseTranslator implements WarmableInterface
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function __construct(ContainerInterface $container, MessageFormatterInterface $formatter, string $defaultLocale, array $loaderIds = [], array $options = []) public function __construct(ContainerInterface $container, MessageFormatterInterface $formatter, string $defaultLocale, array $loaderIds = [], array $options = [], array $enabledLocales = [])
{ {
$this->container = $container; $this->container = $container;
$this->loaderIds = $loaderIds; $this->loaderIds = $loaderIds;
$this->enabledLocales = $enabledLocales;
// check option names // check option names
if ($diff = array_diff(array_keys($options), array_keys($this->options))) { if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
@ -97,8 +103,9 @@ class Translator extends BaseTranslator implements WarmableInterface
return; return;
} }
$locales = array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales); $localesToWarmUp = $this->enabledLocales ?: array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales);
foreach (array_unique($locales) as $locale) {
foreach (array_unique($localesToWarmUp) as $locale) {
// reset catalogue in case it's already loaded during the dump of the other locales. // reset catalogue in case it's already loaded during the dump of the other locales.
if (isset($this->catalogues[$locale])) { if (isset($this->catalogues[$locale])) {
unset($this->catalogues[$locale]); unset($this->catalogues[$locale]);