[FrameworkBundle] Move translation service configuration from xml to php #37186

This commit is contained in:
Malte Schlüter 2020-06-12 17:02:37 +02:00 committed by Tobias Schultze
parent 813d220aa1
commit e4bc48f334
6 changed files with 196 additions and 170 deletions

View File

@ -368,7 +368,7 @@ class FrameworkExtension extends Extension
$this->registerEsiConfiguration($config['esi'], $container, $loader);
$this->registerSsiConfiguration($config['ssi'], $container, $phpLoader);
$this->registerFragmentsConfiguration($config['fragments'], $container, $phpLoader);
$this->registerTranslatorConfiguration($config['translator'], $container, $loader, $config['default_locale']);
$this->registerTranslatorConfiguration($config['translator'], $container, $phpLoader, $config['default_locale']);
$this->registerProfilerConfiguration($config['profiler'], $container, $loader, $phpLoader);
$this->registerWorkflowConfiguration($config['workflows'], $container, $loader);
$this->registerDebugConfiguration($config['php_errors'], $container, $phpLoader);
@ -590,7 +590,7 @@ class FrameworkExtension extends Extension
}
if ($this->translationConfigEnabled) {
$loader->load('translation_debug.xml');
$phpLoader->load('translation_debug.php');
$container->getDefinition('translator.data_collector')->setDecoratedService('translator');
}
@ -1086,7 +1086,7 @@ class FrameworkExtension extends Extension
return;
}
$loader->load('translation.xml');
$loader->load('translation.php');
// Use the "real" translator instead of the identity default
$container->setAlias('translator', 'translator.default')->setPublic(true);

View File

@ -0,0 +1,162 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\Translation\Dumper\CsvFileDumper;
use Symfony\Component\Translation\Dumper\IcuResFileDumper;
use Symfony\Component\Translation\Dumper\IniFileDumper;
use Symfony\Component\Translation\Dumper\JsonFileDumper;
use Symfony\Component\Translation\Dumper\MoFileDumper;
use Symfony\Component\Translation\Dumper\PhpFileDumper;
use Symfony\Component\Translation\Dumper\PoFileDumper;
use Symfony\Component\Translation\Dumper\QtFileDumper;
use Symfony\Component\Translation\Dumper\XliffFileDumper;
use Symfony\Component\Translation\Dumper\YamlFileDumper;
use Symfony\Component\Translation\Extractor\ChainExtractor;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\Extractor\PhpExtractor;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\Loader\CsvFileLoader;
use Symfony\Component\Translation\Loader\IcuDatFileLoader;
use Symfony\Component\Translation\Loader\IcuResFileLoader;
use Symfony\Component\Translation\Loader\IniFileLoader;
use Symfony\Component\Translation\Loader\JsonFileLoader;
use Symfony\Component\Translation\Loader\MoFileLoader;
use Symfony\Component\Translation\Loader\PhpFileLoader;
use Symfony\Component\Translation\Loader\PoFileLoader;
use Symfony\Component\Translation\Loader\QtFileLoader;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\LoggingTranslator;
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Symfony\Component\Translation\Writer\TranslationWriter;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
return static function (ContainerConfigurator $container) {
$container->services()
->set('translator.default', Translator::class)
->args([
abstract_arg('translation loaders locator'),
service('translator.formatter'),
param('kernel.default_locale'),
abstract_arg('translation loaders ids'),
[
'cache_dir' => param('kernel.cache_dir').'/translations',
'debug' => param('kernel.debug'),
],
abstract_arg('enabled locales'),
])
->call('setConfigCacheFactory', [service('config_cache_factory')])
->tag('kernel.locale_aware')
->alias(TranslatorInterface::class, 'translator')
->set('translator.logging', LoggingTranslator::class)
->args([
service('translator.logging.inner'),
service('logger'),
])
->tag('monolog.logger', ['channel' => 'translation'])
->set('translator.formatter.default', MessageFormatter::class)
->args([service('identity_translator')])
->set('translation.loader.php', PhpFileLoader::class)
->tag('translation.loader', ['alias' => 'php'])
->set('translation.loader.yml', YamlFileLoader::class)
->tag('translation.loader', ['alias' => 'yaml', 'legacy-alias' => 'yml'])
->set('translation.loader.xliff', XliffFileLoader::class)
->tag('translation.loader', ['alias' => 'xlf', 'legacy-alias' => 'xliff'])
->set('translation.loader.po', PoFileLoader::class)
->tag('translation.loader', ['alias' => 'po'])
->set('translation.loader.mo', MoFileLoader::class)
->tag('translation.loader', ['alias' => 'mo'])
->set('translation.loader.qt', QtFileLoader::class)
->tag('translation.loader', ['alias' => 'ts'])
->set('translation.loader.csv', CsvFileLoader::class)
->tag('translation.loader', ['alias' => 'csv'])
->set('translation.loader.res', IcuResFileLoader::class)
->tag('translation.loader', ['alias' => 'res'])
->set('translation.loader.dat', IcuDatFileLoader::class)
->tag('translation.loader', ['alias' => 'dat'])
->set('translation.loader.ini', IniFileLoader::class)
->tag('translation.loader', ['alias' => 'ini'])
->set('translation.loader.json', JsonFileLoader::class)
->tag('translation.loader', ['alias' => 'json'])
->set('translation.dumper.php', PhpFileDumper::class)
->tag('translation.dumper', ['alias' => 'php'])
->set('translation.dumper.xliff', XliffFileDumper::class)
->tag('translation.dumper', ['alias' => 'xlf'])
->set('translation.dumper.po', PoFileDumper::class)
->tag('translation.dumper', ['alias' => 'po'])
->set('translation.dumper.mo', MoFileDumper::class)
->tag('translation.dumper', ['alias' => 'mo'])
->set('translation.dumper.yml', YamlFileDumper::class)
->tag('translation.dumper', ['alias' => 'yml'])
->set('translation.dumper.yaml', YamlFileDumper::class)
->args(['yaml'])
->tag('translation.dumper', ['alias' => 'yaml'])
->set('translation.dumper.qt', QtFileDumper::class)
->tag('translation.dumper', ['alias' => 'ts'])
->set('translation.dumper.csv', CsvFileDumper::class)
->tag('translation.dumper', ['alias' => 'csv'])
->set('translation.dumper.ini', IniFileDumper::class)
->tag('translation.dumper', ['alias' => 'ini'])
->set('translation.dumper.json', JsonFileDumper::class)
->tag('translation.dumper', ['alias' => 'json'])
->set('translation.dumper.res', IcuResFileDumper::class)
->tag('translation.dumper', ['alias' => 'res'])
->set('translation.extractor.php', PhpExtractor::class)
->tag('translation.extractor', ['alias' => 'php'])
->set('translation.reader', TranslationReader::class)
->alias(TranslationReaderInterface::class, 'translation.reader')
->set('translation.extractor', ChainExtractor::class)
->alias(ExtractorInterface::class, 'translation.extractor')
->set('translation.writer', TranslationWriter::class)
->alias(TranslationWriterInterface::class, 'translation.writer')
->set('translation.warmer', TranslationsCacheWarmer::class)
->args([service(ContainerInterface::class)])
->tag('container.service_subscriber', ['id' => 'translator'])
->tag('kernel.cache_warmer')
;
};

View File

@ -1,145 +0,0 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults public="false" />
<service id="translator.default" class="Symfony\Bundle\FrameworkBundle\Translation\Translator">
<argument /> <!-- translation loaders locator -->
<argument type="service" id="translator.formatter" />
<argument>%kernel.default_locale%</argument>
<argument type="collection" /> <!-- translation loaders ids -->
<argument type="collection">
<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>
<tag name="kernel.locale_aware" />
</service>
<service id="Symfony\Contracts\Translation\TranslatorInterface" alias="translator" />
<service id="translator.logging" class="Symfony\Component\Translation\LoggingTranslator">
<argument type="service" id="translator.logging.inner" />
<argument type="service" id="logger" />
<tag name="monolog.logger" channel="translation" />
</service>
<service id="translator.formatter.default" class="Symfony\Component\Translation\Formatter\MessageFormatter">
<argument type="service" id="identity_translator" />
</service>
<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader">
<tag name="translation.loader" alias="php" />
</service>
<service id="translation.loader.yml" class="Symfony\Component\Translation\Loader\YamlFileLoader">
<tag name="translation.loader" alias="yaml" legacy-alias="yml" />
</service>
<service id="translation.loader.xliff" class="Symfony\Component\Translation\Loader\XliffFileLoader">
<tag name="translation.loader" alias="xlf" legacy-alias="xliff" />
</service>
<service id="translation.loader.po" class="Symfony\Component\Translation\Loader\PoFileLoader">
<tag name="translation.loader" alias="po" />
</service>
<service id="translation.loader.mo" class="Symfony\Component\Translation\Loader\MoFileLoader">
<tag name="translation.loader" alias="mo" />
</service>
<service id="translation.loader.qt" class="Symfony\Component\Translation\Loader\QtFileLoader">
<tag name="translation.loader" alias="ts" />
</service>
<service id="translation.loader.csv" class="Symfony\Component\Translation\Loader\CsvFileLoader">
<tag name="translation.loader" alias="csv" />
</service>
<service id="translation.loader.res" class="Symfony\Component\Translation\Loader\IcuResFileLoader">
<tag name="translation.loader" alias="res" />
</service>
<service id="translation.loader.dat" class="Symfony\Component\Translation\Loader\IcuDatFileLoader">
<tag name="translation.loader" alias="dat" />
</service>
<service id="translation.loader.ini" class="Symfony\Component\Translation\Loader\IniFileLoader">
<tag name="translation.loader" alias="ini" />
</service>
<service id="translation.loader.json" class="Symfony\Component\Translation\Loader\JsonFileLoader">
<tag name="translation.loader" alias="json" />
</service>
<service id="translation.dumper.php" class="Symfony\Component\Translation\Dumper\PhpFileDumper">
<tag name="translation.dumper" alias="php" />
</service>
<service id="translation.dumper.xliff" class="Symfony\Component\Translation\Dumper\XliffFileDumper">
<tag name="translation.dumper" alias="xlf" />
</service>
<service id="translation.dumper.po" class="Symfony\Component\Translation\Dumper\PoFileDumper">
<tag name="translation.dumper" alias="po" />
</service>
<service id="translation.dumper.mo" class="Symfony\Component\Translation\Dumper\MoFileDumper">
<tag name="translation.dumper" alias="mo" />
</service>
<service id="translation.dumper.yml" class="Symfony\Component\Translation\Dumper\YamlFileDumper">
<tag name="translation.dumper" alias="yml" />
</service>
<service id="translation.dumper.yaml" class="Symfony\Component\Translation\Dumper\YamlFileDumper">
<argument>yaml</argument>
<tag name="translation.dumper" alias="yaml" />
</service>
<service id="translation.dumper.qt" class="Symfony\Component\Translation\Dumper\QtFileDumper">
<tag name="translation.dumper" alias="ts" />
</service>
<service id="translation.dumper.csv" class="Symfony\Component\Translation\Dumper\CsvFileDumper">
<tag name="translation.dumper" alias="csv" />
</service>
<service id="translation.dumper.ini" class="Symfony\Component\Translation\Dumper\IniFileDumper">
<tag name="translation.dumper" alias="ini" />
</service>
<service id="translation.dumper.json" class="Symfony\Component\Translation\Dumper\JsonFileDumper">
<tag name="translation.dumper" alias="json" />
</service>
<service id="translation.dumper.res" class="Symfony\Component\Translation\Dumper\IcuResFileDumper">
<tag name="translation.dumper" alias="res" />
</service>
<service id="translation.extractor.php" class="Symfony\Component\Translation\Extractor\PhpExtractor">
<tag name="translation.extractor" alias="php" />
</service>
<service id="translation.reader" class="Symfony\Component\Translation\Reader\TranslationReader" />
<service id="Symfony\Component\Translation\Reader\TranslationReaderInterface" alias="translation.reader" />
<service id="translation.extractor" class="Symfony\Component\Translation\Extractor\ChainExtractor" />
<service id="Symfony\Component\Translation\Extractor\ExtractorInterface" alias="translation.extractor" />
<service id="translation.writer" class="Symfony\Component\Translation\Writer\TranslationWriter" />
<service id="Symfony\Component\Translation\Writer\TranslationWriterInterface" alias="translation.writer" />
<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer">
<tag name="container.service_subscriber" id="translator" />
<tag name="kernel.cache_warmer" />
<argument type="service" id="Psr\Container\ContainerInterface" />
</service>
</services>
</container>

View File

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
use Symfony\Component\Translation\DataCollectorTranslator;
return static function (ContainerConfigurator $container) {
$container->services()
->set('translator.data_collector', DataCollectorTranslator::class)
->args([service('translator.data_collector.inner')])
->set('data_collector.translation', TranslationDataCollector::class)
->args([service('translator.data_collector')])
->tag('data_collector', [
'template' => '@WebProfiler/Collector/translation.html.twig',
'id' => 'translation',
'priority' => 275,
])
;
};

View File

@ -1,21 +0,0 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults public="false" />
<!-- DataCollectorTranslator -->
<service id="translator.data_collector" class="Symfony\Component\Translation\DataCollectorTranslator">
<argument type="service" id="translator.data_collector.inner" />
</service>
<!-- DataCollector -->
<service id="data_collector.translation" class="Symfony\Component\Translation\DataCollector\TranslationDataCollector">
<tag name="data_collector" template="@WebProfiler/Collector/translation.html.twig" id="translation" priority="275" />
<argument type="service" id="translator.data_collector" />
</service>
</services>
</container>

View File

@ -729,7 +729,7 @@ abstract class FrameworkExtensionTest extends TestCase
public function testTranslator()
{
$container = $this->createContainerFromFile('full');
$this->assertTrue($container->hasDefinition('translator.default'), '->registerTranslatorConfiguration() loads translation.xml');
$this->assertTrue($container->hasDefinition('translator.default'), '->registerTranslatorConfiguration() loads translation.php');
$this->assertEquals('translator.default', (string) $container->getAlias('translator'), '->registerTranslatorConfiguration() redefines translator service from identity to real translator');
$options = $container->getDefinition('translator.default')->getArgument(4);