Skip to content

Create an interface for TranslationWriter #23665

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

Closed
wants to merge 12 commits into from
Closed
7 changes: 7 additions & 0 deletions UPGRADE-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ SecurityBundle

* `FirewallContext::getListeners()` now returns `\Traversable|array`

Translation
-----------

* `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations` has been deprecated
and will be removed in 4.0, use `Symfony\Component\Translation\Writer\TranslationWriter::write`
instead.

TwigBridge
----------

Expand Down
3 changes: 3 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ Translation
-----------

* Removed the backup feature from the file dumper classes.

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

TwigBundle
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Translation\Extractor\ExtractorInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;

/**
* A command that parses templates to extract translation messages and adds them
Expand All @@ -39,14 +39,14 @@ class TranslationUpdateCommand extends ContainerAwareCommand
private $defaultLocale;

/**
* @param TranslationWriter $writer
* @param TranslationLoader $loader
* @param ExtractorInterface $extractor
* @param string $defaultLocale
* @param TranslationWriterInterface $writer
* @param TranslationLoader $loader
* @param ExtractorInterface $extractor
* @param string $defaultLocale
*/
public function __construct($writer = null, TranslationLoader $loader = null, ExtractorInterface $extractor = null, $defaultLocale = null)
{
if (!$writer instanceof TranslationWriter) {
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);

parent::__construct($writer);
Expand Down Expand Up @@ -276,7 +276,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$bundleTransPath = end($transPaths).'translations';
}

$this->writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath, 'default_locale' => $this->defaultLocale));
$this->writer->write($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath, 'default_locale' => $this->defaultLocale));

if (true === $input->getOption('dump-messages')) {
$resultMessage .= ' and translation files were updated';
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* Added `TranslationDumperPass`
* Added `TranslationExtractorPass`
* Added `TranslatorPass`
* Added `TranslationWriterInterface`
* Deprecated `TranslationWriter::writeTranslations` in favor of `TranslationWriter::write`

3.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

class TranslationWriterTest extends TestCase
{
/**
* @group legacy
* @expectedDeprecation Method Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations() is deprecated since version 3.4 and will be removed in 4.0. Use write() instead.
*/
public function testWriteTranslations()
{
$dumper = $this->getMockBuilder('Symfony\Component\Translation\Dumper\DumperInterface')->getMock();
Expand All @@ -30,6 +34,18 @@ public function testWriteTranslations()
$writer->writeTranslations(new MessageCatalogue(array()), 'test');
}

public function testWrite()
{
$dumper = $this->getMockBuilder('Symfony\Component\Translation\Dumper\DumperInterface')->getMock();
$dumper
->expects($this->once())
->method('dump');

$writer = new TranslationWriter();
$writer->addDumper('test', $dumper);
$writer->write(new MessageCatalogue(array()), 'test');
}

public function testDisableBackup()
{
$nonBackupDumper = new NonBackupDumper();
Expand Down
23 changes: 20 additions & 3 deletions src/Symfony/Component/Translation/Writer/TranslationWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class TranslationWriter
class TranslationWriter implements TranslationWriterInterface
{
/**
* Dumpers used for export.
Expand Down Expand Up @@ -66,13 +66,13 @@ public function getFormats()
/**
* Writes translation from the catalogue according to the selected format.
*
* @param MessageCatalogue $catalogue The message catalogue to dump
* @param MessageCatalogue $catalogue The message catalogue to write
* @param string $format The format to use to dump the messages
* @param array $options Options that are passed to the dumper
*
* @throws InvalidArgumentException
*/
public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
public function write(MessageCatalogue $catalogue, $format, $options = array())
{
if (!isset($this->dumpers[$format])) {
throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
Expand All @@ -88,4 +88,21 @@ public function writeTranslations(MessageCatalogue $catalogue, $format, $options
// save
$dumper->dump($catalogue, $options);
}

/**
* Writes translation from the catalogue according to the selected format.
*
* @param MessageCatalogue $catalogue The message catalogue to write
* @param string $format The format to use to dump the messages
* @param array $options Options that are passed to the dumper
*
* @throws InvalidArgumentException
*
* @deprecated since 3.4 will be removed in 4.0. Use write instead.
*/
public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
{
@trigger_error(sprintf('Method %s() is deprecated since version 3.4 and will be removed in 4.0. Use write() instead.', __METHOD__), E_USER_DEPRECATED);
$this->write($catalogue, $format, $options);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Translation\Writer;

use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\MessageCatalogue;

/**
* TranslationWriter writes translation messages.
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
interface TranslationWriterInterface
{
/**
* Writes translation from the catalogue according to the selected format.
*
* @param MessageCatalogue $catalogue The message catalogue to write
* @param string $format The format to use to dump the messages
* @param array $options Options that are passed to the dumper
*
* @throws InvalidArgumentException
*/
public function write(MessageCatalogue $catalogue, $format, $options = array());
}