[Translation] Introduce a way to configure the enabled locales
This commit is contained in:
parent
6c163906f2
commit
765843426e
@ -677,6 +677,16 @@ class Configuration implements ConfigurationInterface
|
||||
->arrayNode('paths')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->arrayNode('enabled_locales')
|
||||
->prototype('scalar')
|
||||
->defaultValue([])
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then(function ($config) {
|
||||
return array_unique((array) $config);
|
||||
})
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
|
@ -1039,6 +1039,8 @@ class FrameworkExtension extends Extension
|
||||
$defaultOptions['cache_dir'] = $config['cache_dir'];
|
||||
$translator->setArgument(4, $defaultOptions);
|
||||
|
||||
$translator->setArgument(6, $config['enabled_locales']);
|
||||
|
||||
$container->setParameter('translator.logging', $config['logging']);
|
||||
$container->setParameter('translator.default_path', $config['default_path']);
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
|
||||
<argument key="debug">%kernel.debug%</argument>
|
||||
</argument>
|
||||
<argument type="collection" /> <!-- enabled locales -->
|
||||
<call method="setConfigCacheFactory">
|
||||
<argument type="service" id="config_cache_factory" />
|
||||
</call>
|
||||
|
@ -373,6 +373,7 @@ class ConfigurationTest extends TestCase
|
||||
'formatter' => 'translator.formatter.default',
|
||||
'paths' => [],
|
||||
'default_path' => '%kernel.project_dir%/translations',
|
||||
'enabled_locales' => [],
|
||||
],
|
||||
'validation' => [
|
||||
'enabled' => !class_exists(FullStack::class),
|
||||
|
@ -109,7 +109,7 @@ class TranslatorTest extends TestCase
|
||||
|
||||
public function testLoadResourcesWithoutCaching()
|
||||
{
|
||||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
|
||||
$loader = new YamlFileLoader();
|
||||
$resourceFiles = [
|
||||
'fr' => [
|
||||
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
|
||||
@ -186,7 +186,7 @@ class TranslatorTest extends TestCase
|
||||
|
||||
public function testCatalogResourcesAreAddedForScannedDirectories()
|
||||
{
|
||||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
|
||||
$loader = new YamlFileLoader();
|
||||
$resourceFiles = [
|
||||
'fr' => [
|
||||
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
|
||||
@ -329,9 +329,9 @@ class TranslatorTest extends TestCase
|
||||
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) {
|
||||
$translator->addResource('loader', 'foo', 'fr');
|
||||
@ -348,7 +348,7 @@ class TranslatorTest extends TestCase
|
||||
|
||||
public function testWarmup()
|
||||
{
|
||||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
|
||||
$loader = new YamlFileLoader();
|
||||
$resourceFiles = [
|
||||
'fr' => [
|
||||
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
|
||||
@ -371,9 +371,34 @@ class TranslatorTest extends TestCase
|
||||
$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()
|
||||
{
|
||||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
|
||||
$loader = new YamlFileLoader();
|
||||
$resourceFiles = [
|
||||
'en' => [
|
||||
__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'));
|
||||
}
|
||||
|
||||
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) {
|
||||
return new $translatorClass(
|
||||
$this->getContainer($loader),
|
||||
new MessageFormatter(),
|
||||
[$loaderFomat => [$loaderFomat]],
|
||||
$options
|
||||
$options,
|
||||
$enabledLocales
|
||||
);
|
||||
}
|
||||
|
||||
@ -402,7 +428,8 @@ class TranslatorTest extends TestCase
|
||||
new MessageFormatter(),
|
||||
$defaultLocale,
|
||||
[$loaderFomat => [$loaderFomat]],
|
||||
$options
|
||||
$options,
|
||||
$enabledLocales
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -57,6 +57,11 @@ class Translator extends BaseTranslator implements WarmableInterface
|
||||
*/
|
||||
private $scannedDirectories;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $enabledLocales;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
@ -69,10 +74,11 @@ class Translator extends BaseTranslator implements WarmableInterface
|
||||
*
|
||||
* @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->loaderIds = $loaderIds;
|
||||
$this->enabledLocales = $enabledLocales;
|
||||
|
||||
// check option names
|
||||
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
|
||||
@ -97,8 +103,9 @@ class Translator extends BaseTranslator implements WarmableInterface
|
||||
return;
|
||||
}
|
||||
|
||||
$locales = array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales);
|
||||
foreach (array_unique($locales) as $locale) {
|
||||
$localesToWarmUp = $this->enabledLocales ?: array_merge($this->getFallbackLocales(), [$this->getLocale()], $this->resourceLocales);
|
||||
|
||||
foreach (array_unique($localesToWarmUp) as $locale) {
|
||||
// reset catalogue in case it's already loaded during the dump of the other locales.
|
||||
if (isset($this->catalogues[$locale])) {
|
||||
unset($this->catalogues[$locale]);
|
||||
|
Reference in New Issue
Block a user