Skip to content

Commit 3cbba5e

Browse files
committed
[Translation][File dumper] allow get file content without writing in file.
1 parent 327c647 commit 3cbba5e

23 files changed

+63
-120
lines changed

Diff for: src/Symfony/Component/Translation/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* deprecated FileDumper::format(), overwrite FileDumper::formatCatalogue() instead.
88
* deprecated Translator::getMessages(), rely on TranslatorBagInterface::getCatalogue() instead.
9+
* added `FileDumper::formatCatalogue` which allows format the catalogue without dumping it into file.
910
* added option `json_encoding` to JsonFileDumper
1011
* added options `as_tree`, `inline` to YamlFileDumper
1112
* added support for XLIFF 2.0.

Diff for: src/Symfony/Component/Translation/Dumper/CsvFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function format(MessageCatalogue $messages, $domain = 'messages')
3636
/**
3737
* {@inheritdoc}
3838
*/
39-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
39+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
4040
{
4141
$handle = fopen('php://memory', 'rb+');
4242

Diff for: src/Symfony/Component/Translation/Dumper/FileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function dump(MessageCatalogue $messages, $options = array())
9797
*
9898
* @return string representation
9999
*/
100-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
100+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
101101
{
102102
@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);
103103

Diff for: src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function format(MessageCatalogue $messages, $domain = 'messages')
3838
/**
3939
* {@inheritdoc}
4040
*/
41-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
41+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
4242
{
4343
$data = $indexes = $resources = '';
4444

Diff for: src/Symfony/Component/Translation/Dumper/IniFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function format(MessageCatalogue $messages, $domain = 'messages')
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
36+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3737
{
3838
$output = '';
3939

Diff for: src/Symfony/Component/Translation/Dumper/JsonFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function format(MessageCatalogue $messages, $domain = 'messages')
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
36+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3737
{
3838
if (isset($options['json_encoding'])) {
3939
$flags = $options['json_encoding'];

Diff for: src/Symfony/Component/Translation/Dumper/MoFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function format(MessageCatalogue $messages, $domain = 'messages')
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
37+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3838
{
3939
$output = $sources = $targets = $sourceOffsets = $targetOffsets = '';
4040
$offsets = array();

Diff for: src/Symfony/Component/Translation/Dumper/PhpFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function format(MessageCatalogue $messages, $domain)
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
36+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3737
{
3838
$output = "<?php\n\nreturn ".var_export($messages->all($domain), true).";\n";
3939

Diff for: src/Symfony/Component/Translation/Dumper/PoFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function format(MessageCatalogue $messages, $domain = 'messages')
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
36+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3737
{
3838
$output = 'msgid ""'."\n";
3939
$output .= 'msgstr ""'."\n";

Diff for: src/Symfony/Component/Translation/Dumper/QtFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function format(MessageCatalogue $messages, $domain)
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
36+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3737
{
3838
$dom = new \DOMDocument('1.0', 'utf-8');
3939
$dom->formatOutput = true;

Diff for: src/Symfony/Component/Translation/Dumper/XliffFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class XliffFileDumper extends FileDumper
2323
/**
2424
* {@inheritdoc}
2525
*/
26-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
26+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
2727
{
2828
$xliffVersion = '1.2';
2929
if (array_key_exists('xliff_version', $options)) {

Diff for: src/Symfony/Component/Translation/Dumper/YamlFileDumper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class YamlFileDumper extends FileDumper
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
28+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
2929
{
3030
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
3131
throw new \LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.');

Diff for: src/Symfony/Component/Translation/Tests/Dumper/CsvFileDumperTest.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@
1616

1717
class CsvFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testDump()
19+
public function testFormatCatalogue()
2020
{
2121
$catalogue = new MessageCatalogue('en');
2222
$catalogue->add(array('foo' => 'bar', 'bar' => 'foo
2323
foo', 'foo;foo' => 'bar'));
2424

25-
$tempDir = sys_get_temp_dir();
2625
$dumper = new CsvFileDumper();
27-
$dumper->dump($catalogue, array('path' => $tempDir));
2826

29-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/valid.csv'), file_get_contents($tempDir.'/messages.en.csv'));
30-
31-
unlink($tempDir.'/messages.en.csv');
27+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/valid.csv', $dumper->formatCatalogue($catalogue, 'messages'));
3228
}
3329
}

Diff for: src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616

1717
class FileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19+
public function testDump()
20+
{
21+
$tempDir = sys_get_temp_dir();
22+
23+
$catalogue = new MessageCatalogue('en');
24+
$catalogue->add(array('foo' => 'bar'));
25+
26+
$dumper = new ConcreteFileDumper();
27+
$dumper->dump($catalogue, array('path' => $tempDir));
28+
29+
$this->assertTrue(file_exists($tempDir.'/messages.en.concrete'));
30+
}
31+
1932
public function testDumpBackupsFileIfExisting()
2033
{
2134
$tempDir = sys_get_temp_dir();
@@ -58,7 +71,7 @@ public function testDumpCreatesNestedDirectoriesAndFile()
5871

5972
class ConcreteFileDumper extends FileDumper
6073
{
61-
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
74+
public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
6275
{
6376
return '';
6477
}

Diff for: src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class IcuResFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testDump()
19+
public function testFormatCatalogue()
2020
{
2121
if (!function_exists('mb_convert_encoding')) {
2222
$this->markTestSkipped('This test requires mbstring to work.');
@@ -25,14 +25,8 @@ public function testDump()
2525
$catalogue = new MessageCatalogue('en');
2626
$catalogue->add(array('foo' => 'bar'));
2727

28-
$tempDir = sys_get_temp_dir().'/IcuResFileDumperTest';
2928
$dumper = new IcuResFileDumper();
30-
$dumper->dump($catalogue, array('path' => $tempDir));
3129

32-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resourcebundle/res/en.res'), file_get_contents($tempDir.'/messages/en.res'));
33-
34-
@unlink($tempDir.'/messages/en.res');
35-
@rmdir($tempDir.'/messages');
36-
@rmdir($tempDir);
30+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resourcebundle/res/en.res', $dumper->formatCatalogue($catalogue, 'messages'));
3731
}
3832
}

Diff for: src/Symfony/Component/Translation/Tests/Dumper/IniFileDumperTest.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616

1717
class IniFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testDump()
19+
public function testFormatCatalogue()
2020
{
2121
$catalogue = new MessageCatalogue('en');
2222
$catalogue->add(array('foo' => 'bar'));
2323

24-
$tempDir = sys_get_temp_dir();
2524
$dumper = new IniFileDumper();
26-
$dumper->dump($catalogue, array('path' => $tempDir));
2725

28-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ini'), file_get_contents($tempDir.'/messages.en.ini'));
29-
30-
unlink($tempDir.'/messages.en.ini');
26+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.ini', $dumper->formatCatalogue($catalogue, 'messages'));
3127
}
3228
}

Diff for: src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php

+3-17
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,7 @@
1616

1717
class JsonFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
private $tempDir;
20-
21-
protected function setUp()
22-
{
23-
$this->tempDir = sys_get_temp_dir();
24-
}
25-
26-
protected function tearDown()
27-
{
28-
unlink($this->tempDir.'/messages.en.json');
29-
}
30-
31-
public function testDump()
19+
public function testFormatCatalogue()
3220
{
3321
if (PHP_VERSION_ID < 50400) {
3422
$this->markTestIncomplete('PHP below 5.4 doesn\'t support JSON pretty printing');
@@ -38,9 +26,8 @@ public function testDump()
3826
$catalogue->add(array('foo' => 'bar'));
3927

4028
$dumper = new JsonFileDumper();
41-
$dumper->dump($catalogue, array('path' => $this->tempDir));
4229

43-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.json'), file_get_contents($this->tempDir.'/messages.en.json'));
30+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.json', $dumper->formatCatalogue($catalogue, 'messages'));
4431
}
4532

4633
public function testDumpWithCustomEncoding()
@@ -49,8 +36,7 @@ public function testDumpWithCustomEncoding()
4936
$catalogue->add(array('foo' => '"bar"'));
5037

5138
$dumper = new JsonFileDumper();
52-
$dumper->dump($catalogue, array('path' => $this->tempDir, 'json_encoding' => JSON_HEX_QUOT));
5339

54-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.dump.json'), file_get_contents($this->tempDir.'/messages.en.json'));
40+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.dump.json', $dumper->formatCatalogue($catalogue, 'messages', array('json_encoding' => JSON_HEX_QUOT)));
5541
}
5642
}

Diff for: src/Symfony/Component/Translation/Tests/Dumper/MoFileDumperTest.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616

1717
class MoFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testDump()
19+
public function testFormatCatalogue()
2020
{
2121
$catalogue = new MessageCatalogue('en');
2222
$catalogue->add(array('foo' => 'bar'));
2323

24-
$tempDir = sys_get_temp_dir();
2524
$dumper = new MoFileDumper();
26-
$dumper->dump($catalogue, array('path' => $tempDir));
27-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.mo'), file_get_contents($tempDir.'/messages.en.mo'));
2825

29-
unlink($tempDir.'/messages.en.mo');
26+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.mo', $dumper->formatCatalogue($catalogue, 'messages'));
3027
}
3128
}

Diff for: src/Symfony/Component/Translation/Tests/Dumper/PhpFileDumperTest.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616

1717
class PhpFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testDump()
19+
public function testFormatCatalogue()
2020
{
2121
$catalogue = new MessageCatalogue('en');
2222
$catalogue->add(array('foo' => 'bar'));
2323

24-
$tempDir = sys_get_temp_dir();
2524
$dumper = new PhpFileDumper();
26-
$dumper->dump($catalogue, array('path' => $tempDir));
2725

28-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.php'), file_get_contents($tempDir.'/messages.en.php'));
29-
30-
unlink($tempDir.'/messages.en.php');
26+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.php', $dumper->formatCatalogue($catalogue, 'messages'));
3127
}
3228
}

Diff for: src/Symfony/Component/Translation/Tests/Dumper/PoFileDumperTest.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,13 @@
1616

1717
class PoFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testDump()
19+
public function testFormatCatalogue()
2020
{
2121
$catalogue = new MessageCatalogue('en');
2222
$catalogue->add(array('foo' => 'bar'));
2323

24-
$tempDir = sys_get_temp_dir();
2524
$dumper = new PoFileDumper();
26-
$dumper->dump($catalogue, array('path' => $tempDir));
27-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.po'), file_get_contents($tempDir.'/messages.en.po'));
2825

29-
unlink($tempDir.'/messages.en.po');
26+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.po', $dumper->formatCatalogue($catalogue, 'messages'));
3027
}
3128
}

Diff for: src/Symfony/Component/Translation/Tests/Dumper/QtFileDumperTest.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616

1717
class QtFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testDump()
19+
public function testFormatCatalogue()
2020
{
2121
$catalogue = new MessageCatalogue('en');
2222
$catalogue->add(array('foo' => 'bar'), 'resources');
2323

24-
$tempDir = sys_get_temp_dir();
2524
$dumper = new QtFileDumper();
26-
$dumper->dump($catalogue, array('path' => $tempDir));
2725

28-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.ts'), file_get_contents($tempDir.'/resources.en.ts'));
29-
30-
unlink($tempDir.'/resources.en.ts');
26+
$this->assertStringEqualsFile(__DIR__.'/../fixtures/resources.ts', $dumper->formatCatalogue($catalogue, 'resources'));
3127
}
3228
}

0 commit comments

Comments
 (0)