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

Skip to content

[Translation] Introduce a way to configure the enabled locales #32433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,16 @@ private function addTranslatorSection(ArrayNodeDefinition $rootNode)
->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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,8 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$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']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ protected static function getBundleDefaultConfig()
'formatter' => 'translator.formatter.default',
'paths' => [],
'default_path' => '%kernel.project_dir%/translations',
'enabled_locales' => [],
],
'validation' => [
'enabled' => !class_exists(FullStack::class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testTransWithCachingWithInvalidLocale()

public function testLoadResourcesWithoutCaching()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$loader = new YamlFileLoader();
$resourceFiles = [
'fr' => [
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
Expand Down Expand Up @@ -186,7 +186,7 @@ public function getDebugModeAndCacheDirCombinations()

public function testCatalogResourcesAreAddedForScannedDirectories()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$loader = new YamlFileLoader();
$resourceFiles = [
'fr' => [
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
Expand Down Expand Up @@ -329,9 +329,9 @@ protected function getContainer($loader)
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');
Expand All @@ -348,7 +348,7 @@ public function getTranslator($loader, $options = [], $loaderFomat = 'loader', $

public function testWarmup()
{
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
$loader = new YamlFileLoader();
$resourceFiles = [
'fr' => [
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
Expand All @@ -371,9 +371,34 @@ public function testWarmup()
$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',
Expand All @@ -386,14 +411,15 @@ public function testLoadingTranslationFilesWithDotsInMessageDomain()
$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
);
}

Expand All @@ -402,7 +428,8 @@ private function createTranslator($loader, $options, $translatorClass = '\Symfon
new MessageFormatter(),
$defaultLocale,
[$loaderFomat => [$loaderFomat]],
$options
$options,
$enabledLocales
);
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class Translator extends BaseTranslator implements WarmableInterface
*/
private $scannedDirectories;

/**
* @var string[]
*/
private $enabledLocales;

/**
* Constructor.
*
Expand All @@ -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))) {
Expand All @@ -97,8 +103,9 @@ public function warmUp(string $cacheDir)
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]);
Expand Down