[Translation][File dumper] allow get file content without writing in file.

This commit is contained in:
Abdellatif Ait boudad 2015-09-18 16:59:19 +00:00
parent b1e0bd76c4
commit 9b877cfa23
23 changed files with 63 additions and 120 deletions

View File

@ -6,6 +6,7 @@ CHANGELOG
* deprecated FileDumper::format(), overwrite FileDumper::formatCatalogue() instead.
* deprecated Translator::getMessages(), rely on TranslatorBagInterface::getCatalogue() instead.
* added `FileDumper::formatCatalogue` which allows format the catalogue without dumping it into file.
* added option `json_encoding` to JsonFileDumper
* added options `as_tree`, `inline` to YamlFileDumper
* added support for XLIFF 2.0.

View File

@ -36,7 +36,7 @@ class CsvFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
$handle = fopen('php://memory', 'rb+');

View File

@ -97,7 +97,7 @@ abstract class FileDumper implements DumperInterface
*
* @return string representation
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
@trigger_error('The '.__METHOD__.' method will replace the format method in 3.0. You should overwritten it instead of overwriting format instead.', E_USER_DEPRECATED);

View File

@ -38,7 +38,7 @@ class IcuResFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
$data = $indexes = $resources = '';

View File

@ -33,7 +33,7 @@ class IniFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
$output = '';

View File

@ -33,7 +33,7 @@ class JsonFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
if (isset($options['json_encoding'])) {
$flags = $options['json_encoding'];

View File

@ -34,7 +34,7 @@ class MoFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
$output = $sources = $targets = $sourceOffsets = $targetOffsets = '';
$offsets = array();

View File

@ -33,7 +33,7 @@ class PhpFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
$output = "<?php\n\nreturn ".var_export($messages->all($domain), true).";\n";

View File

@ -33,7 +33,7 @@ class PoFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
$output = 'msgid ""'."\n";
$output .= 'msgstr ""'."\n";

View File

@ -33,7 +33,7 @@ class QtFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;

View File

@ -23,7 +23,7 @@ class XliffFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
$xliffVersion = '1.2';
if (array_key_exists('xliff_version', $options)) {

View File

@ -25,7 +25,7 @@ class YamlFileDumper extends FileDumper
/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
throw new \LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');

View File

@ -16,18 +16,14 @@ use Symfony\Component\Translation\Dumper\CsvFileDumper;
class CsvFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar', 'bar' => 'foo
foo', 'foo;foo' => 'bar'));
$tempDir = sys_get_temp_dir();
$dumper = new CsvFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/valid.csv'), file_get_contents($tempDir.'/messages.en.csv'));
unlink($tempDir.'/messages.en.csv');
$this->assertStringEqualsFile(__DIR__.'/../fixtures/valid.csv', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@ -16,6 +16,19 @@ use Symfony\Component\Translation\Dumper\FileDumper;
class FileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
{
$tempDir = sys_get_temp_dir();
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$dumper = new ConcreteFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertTrue(file_exists($tempDir.'/messages.en.concrete'));
}
public function testDumpBackupsFileIfExisting()
{
$tempDir = sys_get_temp_dir();
@ -58,7 +71,7 @@ class FileDumperTest extends \PHPUnit_Framework_TestCase
class ConcreteFileDumper extends FileDumper
{
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
return '';
}

View File

@ -16,7 +16,7 @@ use Symfony\Component\Translation\Dumper\IcuResFileDumper;
class IcuResFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
public function testFormatCatalogue()
{
if (!function_exists('mb_convert_encoding')) {
$this->markTestSkipped('This test requires mbstring to work.');
@ -25,14 +25,8 @@ class IcuResFileDumperTest extends \PHPUnit_Framework_TestCase
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$tempDir = sys_get_temp_dir().'/IcuResFileDumperTest';
$dumper = new IcuResFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resourcebundle/res/en.res'), file_get_contents($tempDir.'/messages/en.res'));
@unlink($tempDir.'/messages/en.res');
@rmdir($tempDir.'/messages');
@rmdir($tempDir);
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resourcebundle/res/en.res', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@ -16,17 +16,13 @@ use Symfony\Component\Translation\Dumper\IniFileDumper;
class IniFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$tempDir = sys_get_temp_dir();
$dumper = new IniFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ini'), file_get_contents($tempDir.'/messages.en.ini'));
unlink($tempDir.'/messages.en.ini');
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.ini', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@ -16,19 +16,7 @@ use Symfony\Component\Translation\Dumper\JsonFileDumper;
class JsonFileDumperTest extends \PHPUnit_Framework_TestCase
{
private $tempDir;
protected function setUp()
{
$this->tempDir = sys_get_temp_dir();
}
protected function tearDown()
{
unlink($this->tempDir.'/messages.en.json');
}
public function testDump()
public function testFormatCatalogue()
{
if (PHP_VERSION_ID < 50400) {
$this->markTestIncomplete('PHP below 5.4 doesn\'t support JSON pretty printing');
@ -38,9 +26,8 @@ class JsonFileDumperTest extends \PHPUnit_Framework_TestCase
$catalogue->add(array('foo' => 'bar'));
$dumper = new JsonFileDumper();
$dumper->dump($catalogue, array('path' => $this->tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.json'), file_get_contents($this->tempDir.'/messages.en.json'));
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.json', $dumper->formatCatalogue($catalogue, 'messages'));
}
public function testDumpWithCustomEncoding()
@ -49,8 +36,7 @@ class JsonFileDumperTest extends \PHPUnit_Framework_TestCase
$catalogue->add(array('foo' => '"bar"'));
$dumper = new JsonFileDumper();
$dumper->dump($catalogue, array('path' => $this->tempDir, 'json_encoding' => JSON_HEX_QUOT));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.dump.json'), file_get_contents($this->tempDir.'/messages.en.json'));
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.dump.json', $dumper->formatCatalogue($catalogue, 'messages', array('json_encoding' => JSON_HEX_QUOT)));
}
}

View File

@ -16,16 +16,13 @@ use Symfony\Component\Translation\Dumper\MoFileDumper;
class MoFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$tempDir = sys_get_temp_dir();
$dumper = new MoFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.mo'), file_get_contents($tempDir.'/messages.en.mo'));
unlink($tempDir.'/messages.en.mo');
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.mo', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@ -16,17 +16,13 @@ use Symfony\Component\Translation\Dumper\PhpFileDumper;
class PhpFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$tempDir = sys_get_temp_dir();
$dumper = new PhpFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.php'), file_get_contents($tempDir.'/messages.en.php'));
unlink($tempDir.'/messages.en.php');
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.php', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@ -16,16 +16,13 @@ use Symfony\Component\Translation\Dumper\PoFileDumper;
class PoFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$tempDir = sys_get_temp_dir();
$dumper = new PoFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.po'), file_get_contents($tempDir.'/messages.en.po'));
unlink($tempDir.'/messages.en.po');
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.po', $dumper->formatCatalogue($catalogue, 'messages'));
}
}

View File

@ -16,17 +16,13 @@ use Symfony\Component\Translation\Dumper\QtFileDumper;
class QtFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDump()
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'), 'resources');
$tempDir = sys_get_temp_dir();
$dumper = new QtFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ts'), file_get_contents($tempDir.'/resources.en.ts'));
unlink($tempDir.'/resources.en.ts');
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.ts', $dumper->formatCatalogue($catalogue, 'resources'));
}
}

View File

@ -16,14 +16,7 @@ use Symfony\Component\Translation\Dumper\XliffFileDumper;
class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
{
private $tempDir;
protected function setUp()
{
$this->tempDir = sys_get_temp_dir();
}
public function testDump()
public function testFormatCatalogue()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
@ -35,17 +28,14 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
$catalogue->setMetadata('key', array('notes' => array(array('content' => 'baz'), array('content' => 'qux'))));
$dumper = new XliffFileDumper();
$dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR'));
$this->assertEquals(
file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'),
file_get_contents($this->tempDir.'/messages.en_US.xlf')
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-clean.xlf',
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR'))
);
unlink($this->tempDir.'/messages.en_US.xlf');
}
public function testDumpXliff2()
public function testFormatCatalogueXliff2()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
@ -56,20 +46,16 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
$catalogue->setMetadata('key', array('target-attributes' => array('order' => 1)));
$dumper = new XliffFileDumper();
$dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR', 'xliff_version' => '2.0'));
$this->assertEquals(
file_get_contents(__DIR__.'/../fixtures/resources-2.0-clean.xlf'),
file_get_contents($this->tempDir.'/messages.en_US.xlf')
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-2.0-clean.xlf',
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR', 'xliff_version' => '2.0'))
);
unlink($this->tempDir.'/messages.en_US.xlf');
}
public function testDumpWithCustomToolInfo()
public function testFormatCatalogueWithCustomToolInfo()
{
$options = array(
'path' => $this->tempDir,
'default_locale' => 'en_US',
'tool_info' => array('tool-id' => 'foo', 'tool-name' => 'foo', 'tool-version' => '0.0', 'tool-company' => 'Foo'),
);
@ -78,17 +64,14 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
$catalogue->add(array('foo' => 'bar'));
$dumper = new XliffFileDumper();
$dumper->dump($catalogue, $options);
$this->assertEquals(
file_get_contents(__DIR__.'/../fixtures/resources-tool-info.xlf'),
file_get_contents($this->tempDir.'/messages.en_US.xlf')
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-tool-info.xlf',
$dumper->formatCatalogue($catalogue, 'messages', $options)
);
unlink($this->tempDir.'/messages.en_US.xlf');
}
public function testDumpWithTargetAttributesMetadata()
public function testFormatCatalogueWithTargetAttributesMetadata()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
@ -96,15 +79,11 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
));
$catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
$this->tempDir = sys_get_temp_dir();
$dumper = new XliffFileDumper();
$dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR'));
$this->assertEquals(
file_get_contents(__DIR__.'/../fixtures/resources-target-attributes.xlf'),
file_get_contents($this->tempDir.'/messages.en_US.xlf')
$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-target-attributes.xlf',
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR'))
);
unlink($this->tempDir.'/messages.en_US.xlf');
}
}

View File

@ -16,7 +16,7 @@ use Symfony\Component\Translation\Dumper\YamlFileDumper;
class YamlFileDumperTest extends \PHPUnit_Framework_TestCase
{
public function testTreeDump()
public function testTreeFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(
@ -25,16 +25,12 @@ class YamlFileDumperTest extends \PHPUnit_Framework_TestCase
'foo.bar2' => 'value2',
));
$tempDir = sys_get_temp_dir();
$dumper = new YamlFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir, 'as_tree' => true, 'inline' => 999));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/messages.yml'), file_get_contents($tempDir.'/messages.en.yml'));
unlink($tempDir.'/messages.en.yml');
$this->assertStringEqualsFile(__DIR__.'/../fixtures/messages.yml', $dumper->formatCatalogue($catalogue, 'messages', array('as_tree' => true, 'inline' => 999)));
}
public function testLinearDump()
public function testLinearFormatCatalogue()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(
@ -43,12 +39,8 @@ class YamlFileDumperTest extends \PHPUnit_Framework_TestCase
'foo.bar2' => 'value2',
));
$tempDir = sys_get_temp_dir();
$dumper = new YamlFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/messages_linear.yml'), file_get_contents($tempDir.'/messages.en.yml'));
unlink($tempDir.'/messages.en.yml');
$this->assertStringEqualsFile(__DIR__.'/../fixtures/messages_linear.yml', $dumper->formatCatalogue($catalogue, 'messages'));
}
}