Skip to content

Commit

Permalink
feature #23667 [Translation] Create an TranslationReaderInterface and…
Browse files Browse the repository at this point in the history
… move TranslationLoader to TranslationComponent (Nyholm)

This PR was merged into the 3.4 branch.

Discussion
----------

[Translation] Create an TranslationReaderInterface and move TranslationLoader to TranslationComponent

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Create an interface for TranslationLoader. I put this in the Translation component even though the TranslationLoader implementation is in the FrameworkBundle. (Here is a PR moving the TranslationLoader to the Translation component: #23666)

We need this in PHP-translation. We currently have to rewrite things in strange mannar like: https://github.com/php-translation/symfony-storage/blob/0.2.2/src/FileStorage.php#L62-L64

Commits
-------

5bc50da Create an interface for TranslationReader and moved TranslationLoader to Translation component
  • Loading branch information
fabpot committed Aug 29, 2017
2 parents d00ac8a + 5bc50da commit 173b747
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 76 deletions.
6 changes: 6 additions & 0 deletions UPGRADE-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ FrameworkBundle
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\DependencyInjection\TranslatorPass` class instead.

* The `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\Reader\TranslationReader` class instead.

* The `translation.loader` service has been deprecated and will be removed in 4.0. Use the `translation.reader` service instead.

HttpKernel
----------

Expand Down
8 changes: 8 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,13 @@ FrameworkBundle
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass`
class has been removed. Use the
`Symfony\Component\Translation\DependencyInjection\TranslatorPass` class instead.

* The `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`
class has been deprecated and will be removed in 4.0. Use the
`Symfony\Component\Translation\Reader\TranslationReader` class instead.

* The `translation.loader` service has been deprecated and will be removed in 4.0. Use the `translation.reader` service instead.

HttpFoundation
--------------

Expand Down Expand Up @@ -581,6 +587,8 @@ Translation

* Removed the backup feature from the file dumper classes.

* The default value of the `$readerServiceId` argument of `TranslatorPass::__construct()` has been changed to `"translation.reader"`.

* Removed `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations`,
use `Symfony\Component\Translation\Writer\TranslationWriter::write` instead.

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ CHANGELOG
name as value, using it makes the command lazy
* Added `cache:pool:prune` command to allow manual stale cache item pruning of supported PSR-6 and PSR-16 cache pool
implementations
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
`Symfony\Component\Translation\Reader\TranslationReader` instead
* Deprecated `translation.loader` service, use `translation.reader` instead

3.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -21,6 +20,7 @@
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\DataCollectorTranslator;
use Symfony\Component\Translation\LoggingTranslator;
Expand All @@ -43,15 +43,15 @@ class TranslationDebugCommand extends ContainerAwareCommand
protected static $defaultName = 'debug:translation';

private $translator;
private $loader;
private $reader;
private $extractor;

/**
* @param TranslatorInterface $translator
* @param TranslationLoader $loader
* @param ExtractorInterface $extractor
* @param TranslatorInterface $translator
* @param TranslationReaderInterface $reader
* @param ExtractorInterface $extractor
*/
public function __construct($translator = null, TranslationLoader $loader = null, ExtractorInterface $extractor = null)
public function __construct($translator = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null)
{
if (!$translator instanceof TranslatorInterface) {
@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);
Expand All @@ -64,7 +64,7 @@ public function __construct($translator = null, TranslationLoader $loader = null
parent::__construct();

$this->translator = $translator;
$this->loader = $loader;
$this->reader = $reader;
$this->extractor = $extractor;
}

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

Expand Down Expand Up @@ -331,7 +331,7 @@ private function loadCurrentMessages($locale, $transPaths)
foreach ($transPaths as $path) {
$path = $path.'translations';
if (is_dir($path)) {
$this->loader->loadMessages($path, $currentCatalogue);
$this->reader->read($path, $currentCatalogue);
}
}

Expand All @@ -357,7 +357,7 @@ private function loadFallbackCatalogues($locale, $transPaths)
foreach ($transPaths as $path) {
$path = $path.'translations';
if (is_dir($path)) {
$this->loader->loadMessages($path, $fallbackCatalogue);
$this->reader->read($path, $fallbackCatalogue);
}
}
$fallbackCatalogues[] = $fallbackCatalogue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Translation\Catalogue\TargetOperation;
use Symfony\Component\Translation\Catalogue\MergeOperation;
Expand All @@ -21,6 +20,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;

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

private $writer;
private $loader;
private $reader;
private $extractor;
private $defaultLocale;

/**
* @param TranslationWriterInterface $writer
* @param TranslationLoader $loader
* @param TranslationReaderInterface $reader
* @param ExtractorInterface $extractor
* @param string $defaultLocale
*/
public function __construct($writer = null, TranslationLoader $loader = null, ExtractorInterface $extractor = null, $defaultLocale = null)
public function __construct($writer = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null, $defaultLocale = null)
{
if (!$writer instanceof TranslationWriterInterface) {
@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);
Expand All @@ -59,7 +59,7 @@ public function __construct($writer = null, TranslationLoader $loader = null, Ex
parent::__construct();

$this->writer = $writer;
$this->loader = $loader;
$this->reader = $reader;
$this->extractor = $extractor;
$this->defaultLocale = $defaultLocale;
}
Expand Down Expand Up @@ -127,7 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
// BC to be removed in 4.0
if (null === $this->writer) {
$this->writer = $this->getContainer()->get('translation.writer');
$this->loader = $this->getContainer()->get('translation.loader');
$this->reader = $this->getContainer()->get('translation.reader');
$this->extractor = $this->getContainer()->get('translation.extractor');
$this->defaultLocale = $this->getContainer()->getParameter('kernel.default_locale');
}
Expand Down Expand Up @@ -201,7 +201,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($transPaths as $path) {
$path .= 'translations';
if (is_dir($path)) {
$this->loader->loadMessages($path, $currentCatalogue);
$this->reader->read($path, $currentCatalogue);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
$this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class);
$this->addCompilerPassIfExists($container, TranslatorPass::class);
if (class_exists(TranslatorPass::class)) {
// Arguments to be removed in 4.0, relying on the default values
$container->addCompilerPass(new TranslatorPass('translator.default', 'translation.loader'));
}
$container->addCompilerPass(new LoggingTranslatorPass());
$container->addCompilerPass(new AddCacheWarmerPass());
$container->addCompilerPass(new AddCacheClearerPass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@
<tag name="translation.extractor" alias="php" />
</service>

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
})
);

$loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
$loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
$loader
->expects($this->any())
->method('loadMessages')
->method('read')
->will(
$this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
$catalogue->add($loadedMessages);
Expand Down Expand Up @@ -197,7 +197,7 @@ public function testLegacyDebugCommand()
->method('get')
->will($this->returnValueMap(array(
array('translation.extractor', 1, $extractor),
array('translation.loader', 1, $loader),
array('translation.reader', 1, $loader),
array('translator', 1, $translator),
array('kernel', 1, $kernel),
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
})
);

$loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
$loader = $this->getMockBuilder('Symfony\Component\Translation\Reader\TranslationReader')->getMock();
$loader
->expects($this->any())
->method('loadMessages')
->method('read')
->will(
$this->returnCallback(function ($path, $catalogue) use ($loadedMessages) {
$catalogue->add($loadedMessages);
Expand Down Expand Up @@ -177,7 +177,7 @@ public function testLegacyUpdateCommand()
->method('get')
->will($this->returnValueMap(array(
array('translation.extractor', 1, $extractor),
array('translation.loader', 1, $loader),
array('translation.reader', 1, $loader),
array('translation.writer', 1, $writer),
array('translator', 1, $translator),
array('kernel', 1, $kernel),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ protected function createContainerFromFile($file, $data = array(), $resetCompile
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
}
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddAnnotationsCachedReaderPass(), new AddConstraintValidatorsPass(), new TranslatorPass()));
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddAnnotationsCachedReaderPass(), new AddConstraintValidatorsPass(), new TranslatorPass('translator.default', 'translation.reader')));
$container->compile();

return self::$containerCache[$cacheKey] = $container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,16 @@

namespace Symfony\Bundle\FrameworkBundle\Translation;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Loader\LoaderInterface;

@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);

/**
* TranslationLoader loads translation messages from translation files.
*
* @author Michel Salib <michelsalib@hotmail.com>
* @deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\Translation\Reader\TranslationReader instead
*/
class TranslationLoader
class TranslationLoader extends TranslationReader
{
/**
* Loaders used for import.
*
* @var array
*/
private $loaders = array();

/**
* Adds a loader to the translation extractor.
*
* @param string $format The format of the loader
* @param LoaderInterface $loader
*/
public function addLoader($format, LoaderInterface $loader)
{
$this->loaders[$format] = $loader;
}

/**
* Loads translation messages from a directory to the catalogue.
*
Expand All @@ -48,19 +29,6 @@ public function addLoader($format, LoaderInterface $loader)
*/
public function loadMessages($directory, MessageCatalogue $catalogue)
{
if (!is_dir($directory)) {
return;
}

foreach ($this->loaders as $format => $loader) {
// load any existing translation files
$finder = new Finder();
$extension = $catalogue->getLocale().'.'.$format;
$files = $finder->files()->name('*.'.$extension)->in($directory);
foreach ($files as $file) {
$domain = substr($file->getFilename(), 0, -1 * strlen($extension) - 1);
$catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
}
}
$this->read($directory, $catalogue);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Added `TranslationDumperPass`
* Added `TranslationExtractorPass`
* Added `TranslatorPass`
* Added `TranslationReader` and `TranslationReaderInterface`
* Added `<notes>` section to the Xliff 2.0 dumper.
* Improved Xliff 2.0 loader to load `<notes>` section.
* Added `TranslationWriterInterface`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
class TranslatorPass implements CompilerPassInterface
{
private $translatorServiceId;
private $loaderServiceId;
private $readerServiceId;
private $loaderTag;

public function __construct($translatorServiceId = 'translator.default', $loaderServiceId = 'translation.loader', $loaderTag = 'translation.loader')
public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader')
{
if ('translation.loader' === $readerServiceId && 2 > func_num_args()) {
@trigger_error('The default value for $readerServiceId will change in 4.0 to "translation.reader".', E_USER_DEPRECATED);
}

$this->translatorServiceId = $translatorServiceId;
$this->loaderServiceId = $loaderServiceId;
$this->readerServiceId = $readerServiceId;
$this->loaderTag = $loaderTag;
}

Expand All @@ -45,15 +49,27 @@ public function process(ContainerBuilder $container)
}
}

if ($container->hasDefinition($this->loaderServiceId)) {
$definition = $container->getDefinition($this->loaderServiceId);
if ($container->hasDefinition($this->readerServiceId)) {
$definition = $container->getDefinition($this->readerServiceId);
foreach ($loaders as $id => $formats) {
foreach ($formats as $format) {
$definition->addMethodCall('addLoader', array($format, $loaderRefs[$id]));
}
}
}

// Duplicated code to support "translation.reader", to be removed in 4.0
if ('translation.reader' !== $this->readerServiceId) {
if ($container->hasDefinition('translation.reader')) {
$definition = $container->getDefinition('translation.reader');
foreach ($loaders as $id => $formats) {
foreach ($formats as $format) {
$definition->addMethodCall('addLoader', array($format, $loaderRefs[$id]));
}
}
}
}

$container
->findDefinition($this->translatorServiceId)
->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
Expand Down
Loading

0 comments on commit 173b747

Please sign in to comment.