Skip to content

Commit 51b9f72

Browse files
committed
Create an interface for TranslationReader and moved TranslationLoader to Translation component
1 parent 2210dcc commit 51b9f72

File tree

9 files changed

+41
-64
lines changed

9 files changed

+41
-64
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ CHANGELOG
2323
name as value, using it makes the command lazy
2424
* Added `cache:pool:prune` command to allow manual stale cache item pruning of supported PSR-6 and PSR-16 cache pool
2525
implementations
26+
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
27+
`Symfony\Component\Translation\Reader\TranslationReader` instead
28+
* Deprecated `translation.loader` service, use `translation.reader` instead
2629

2730
3.3.0
2831
-----

Command/TranslationDebugCommand.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
1514
use Symfony\Component\Console\Style\SymfonyStyle;
1615
use Symfony\Component\Console\Input\InputInterface;
1716
use Symfony\Component\Console\Input\InputArgument;
@@ -21,6 +20,7 @@
2120
use Symfony\Component\Translation\Catalogue\MergeOperation;
2221
use Symfony\Component\Translation\Extractor\ExtractorInterface;
2322
use Symfony\Component\Translation\MessageCatalogue;
23+
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
2424
use Symfony\Component\Translation\Translator;
2525
use Symfony\Component\Translation\DataCollectorTranslator;
2626
use Symfony\Component\Translation\LoggingTranslator;
@@ -43,15 +43,15 @@ class TranslationDebugCommand extends ContainerAwareCommand
4343
protected static $defaultName = 'debug:translation';
4444

4545
private $translator;
46-
private $loader;
46+
private $reader;
4747
private $extractor;
4848

4949
/**
50-
* @param TranslatorInterface $translator
51-
* @param TranslationLoader $loader
52-
* @param ExtractorInterface $extractor
50+
* @param TranslatorInterface $translator
51+
* @param TranslationReaderInterface $reader
52+
* @param ExtractorInterface $extractor
5353
*/
54-
public function __construct($translator = null, TranslationLoader $loader = null, ExtractorInterface $extractor = null)
54+
public function __construct($translator = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null)
5555
{
5656
if (!$translator instanceof TranslatorInterface) {
5757
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
@@ -64,7 +64,7 @@ public function __construct($translator = null, TranslationLoader $loader = null
6464
parent::__construct();
6565

6666
$this->translator = $translator;
67-
$this->loader = $loader;
67+
$this->reader = $reader;
6868
$this->extractor = $extractor;
6969
}
7070

@@ -142,7 +142,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
142142
// BC to be removed in 4.0
143143
if (null === $this->translator) {
144144
$this->translator = $this->getContainer()->get('translator');
145-
$this->loader = $this->getContainer()->get('translation.loader');
145+
$this->reader = $this->getContainer()->get('translation.reader');
146146
$this->extractor = $this->getContainer()->get('translation.extractor');
147147
}
148148

@@ -331,7 +331,7 @@ private function loadCurrentMessages($locale, $transPaths)
331331
foreach ($transPaths as $path) {
332332
$path = $path.'translations';
333333
if (is_dir($path)) {
334-
$this->loader->loadMessages($path, $currentCatalogue);
334+
$this->reader->read($path, $currentCatalogue);
335335
}
336336
}
337337

@@ -357,7 +357,7 @@ private function loadFallbackCatalogues($locale, $transPaths)
357357
foreach ($transPaths as $path) {
358358
$path = $path.'translations';
359359
if (is_dir($path)) {
360-
$this->loader->loadMessages($path, $fallbackCatalogue);
360+
$this->reader->read($path, $fallbackCatalogue);
361361
}
362362
}
363363
$fallbackCatalogues[] = $fallbackCatalogue;

Command/TranslationUpdateCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
1514
use Symfony\Component\Console\Style\SymfonyStyle;
1615
use Symfony\Component\Translation\Catalogue\TargetOperation;
1716
use Symfony\Component\Translation\Catalogue\MergeOperation;
@@ -21,6 +20,7 @@
2120
use Symfony\Component\Console\Input\InputOption;
2221
use Symfony\Component\Translation\Extractor\ExtractorInterface;
2322
use Symfony\Component\Translation\MessageCatalogue;
23+
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
2424
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
2525

2626
/**
@@ -36,17 +36,17 @@ class TranslationUpdateCommand extends ContainerAwareCommand
3636
protected static $defaultName = 'translation:update';
3737

3838
private $writer;
39-
private $loader;
39+
private $reader;
4040
private $extractor;
4141
private $defaultLocale;
4242

4343
/**
4444
* @param TranslationWriterInterface $writer
45-
* @param TranslationLoader $loader
45+
* @param TranslationReaderInterface $reader
4646
* @param ExtractorInterface $extractor
4747
* @param string $defaultLocale
4848
*/
49-
public function __construct($writer = null, TranslationLoader $loader = null, ExtractorInterface $extractor = null, $defaultLocale = null)
49+
public function __construct($writer = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null, $defaultLocale = null)
5050
{
5151
if (!$writer instanceof TranslationWriterInterface) {
5252
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
@@ -59,7 +59,7 @@ public function __construct($writer = null, TranslationLoader $loader = null, Ex
5959
parent::__construct();
6060

6161
$this->writer = $writer;
62-
$this->loader = $loader;
62+
$this->reader = $reader;
6363
$this->extractor = $extractor;
6464
$this->defaultLocale = $defaultLocale;
6565
}
@@ -127,7 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
127127
// BC to be removed in 4.0
128128
if (null === $this->writer) {
129129
$this->writer = $this->getContainer()->get('translation.writer');
130-
$this->loader = $this->getContainer()->get('translation.loader');
130+
$this->reader = $this->getContainer()->get('translation.reader');
131131
$this->extractor = $this->getContainer()->get('translation.extractor');
132132
$this->defaultLocale = $this->getContainer()->getParameter('kernel.default_locale');
133133
}
@@ -201,7 +201,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
201201
foreach ($transPaths as $path) {
202202
$path .= 'translations';
203203
if (is_dir($path)) {
204-
$this->loader->loadMessages($path, $currentCatalogue);
204+
$this->reader->read($path, $currentCatalogue);
205205
}
206206
}
207207

FrameworkBundle.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public function build(ContainerBuilder $container)
9696
$container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
9797
$this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
9898
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class);
99-
$this->addCompilerPassIfExists($container, TranslatorPass::class);
99+
if (class_exists(TranslatorPass::class)) {
100+
// Arguments to be removed in 4.0, relying on the default values
101+
$container->addCompilerPass(new TranslatorPass('translator.default', 'translation.loader'));
102+
}
100103
$container->addCompilerPass(new LoggingTranslatorPass());
101104
$container->addCompilerPass(new AddCacheWarmerPass());
102105
$container->addCompilerPass(new AddCacheClearerPass());

Resources/config/translation.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@
121121
<tag name="translation.extractor" alias="php" />
122122
</service>
123123

124-
<service id="translation.loader" class="Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader" public="true" />
124+
<service id="translation.loader" class="Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader" public="true">
125+
<deprecated>The "%service_id%" service is deprecated since Symfony 3.4 and will be removed in 4.0. Use "translation.reader" instead.</deprecated>
126+
</service>
127+
<service id="translation.reader" class="Symfony\Component\Translation\Reader\TranslationReader" public="true" />
125128

126129
<service id="translation.extractor" class="Symfony\Component\Translation\Extractor\ChainExtractor" public="true" />
127130

Tests/Command/TranslationDebugCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
130130
})
131131
);
132132

133-
$loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
133+
$loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
134134
$loader
135135
->expects($this->any())
136-
->method('loadMessages')
136+
->method('read')
137137
->will(
138138
$this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
139139
$catalogue->add($loadedMessages);
@@ -197,7 +197,7 @@ public function testLegacyDebugCommand()
197197
->method('get')
198198
->will($this->returnValueMap(array(
199199
array('translation.extractor', 1, $extractor),
200-
array('translation.loader', 1, $loader),
200+
array('translation.reader', 1, $loader),
201201
array('translator', 1, $translator),
202202
array('kernel', 1, $kernel),
203203
)));

Tests/Command/TranslationUpdateCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
101101
})
102102
);
103103

104-
$loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
104+
$loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
105105
$loader
106106
->expects($this->any())
107-
->method('loadMessages')
107+
->method('read')
108108
->will(
109109
$this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
110110
$catalogue->add($loadedMessages);
@@ -177,7 +177,7 @@ public function testLegacyUpdateCommand()
177177
->method('get')
178178
->will($this->returnValueMap(array(
179179
array('translation.extractor', 1, $extractor),
180-
array('translation.loader', 1, $loader),
180+
array('translation.reader', 1, $loader),
181181
array('translation.writer', 1, $writer),
182182
array('translator', 1, $translator),
183183
array('kernel', 1, $kernel),

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ protected function createContainerFromFile($file, $data = array(), $resetCompile
10001000
$container->getCompilerPassConfig()->setOptimizationPasses(array());
10011001
$container->getCompilerPassConfig()->setRemovingPasses(array());
10021002
}
1003-
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddAnnotationsCachedReaderPass(), new AddConstraintValidatorsPass(), new TranslatorPass()));
1003+
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddAnnotationsCachedReaderPass(), new AddConstraintValidatorsPass(), new TranslatorPass('translator.default', 'translation.reader')));
10041004
$container->compile();
10051005

10061006
return self::$containerCache[$cacheKey] = $container;

Translation/TranslationLoader.php

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,16 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Translation;
1313

14-
use Symfony\Component\Finder\Finder;
14+
use Symfony\Component\Translation\Reader\TranslationReader;
1515
use Symfony\Component\Translation\MessageCatalogue;
16-
use Symfony\Component\Translation\Loader\LoaderInterface;
16+
17+
@trigger_error(sprintf('The class "%s" is deprecated since version 3.4 and will be removed in 4.0. Use "%s" instead. ', TranslationLoader::class, TranslationReader::class), E_USER_DEPRECATED);
1718

1819
/**
19-
* TranslationLoader loads translation messages from translation files.
20-
*
21-
* @author Michel Salib <[email protected]>
20+
* @deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\Translation\Reader\TranslationReader instead
2221
*/
23-
class TranslationLoader
22+
class TranslationLoader extends TranslationReader
2423
{
25-
/**
26-
* Loaders used for import.
27-
*
28-
* @var array
29-
*/
30-
private $loaders = array();
31-
32-
/**
33-
* Adds a loader to the translation extractor.
34-
*
35-
* @param string $format The format of the loader
36-
* @param LoaderInterface $loader
37-
*/
38-
public function addLoader($format, LoaderInterface $loader)
39-
{
40-
$this->loaders[$format] = $loader;
41-
}
42-
4324
/**
4425
* Loads translation messages from a directory to the catalogue.
4526
*
@@ -48,19 +29,6 @@ public function addLoader($format, LoaderInterface $loader)
4829
*/
4930
public function loadMessages($directory, MessageCatalogue $catalogue)
5031
{
51-
if (!is_dir($directory)) {
52-
return;
53-
}
54-
55-
foreach ($this->loaders as $format => $loader) {
56-
// load any existing translation files
57-
$finder = new Finder();
58-
$extension = $catalogue->getLocale().'.'.$format;
59-
$files = $finder->files()->name('*.'.$extension)->in($directory);
60-
foreach ($files as $file) {
61-
$domain = substr($file->getFilename(), 0, -1 * strlen($extension) - 1);
62-
$catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
63-
}
64-
}
32+
$this->read($directory, $catalogue);
6533
}
6634
}

0 commit comments

Comments
 (0)