Skip to content

Commit 1379b1c

Browse files
gepoaitboudad
authored andcommitted
[Translation] added option flags to JsonFileDumper.
It's passed to json_encode function second argument.
1 parent 4b68eb1 commit 1379b1c

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* deprecated Translator::getMessages(), rely on TranslatorBagInterface::getCatalogue() instead.
8+
* added option 'json_encoding' to JsonFileDumper
89
* added options `as_tree`, `inline` to YamlFileDumper
910
* added support for XLIFF target and tool attributes.
1011
* added message parameters to DataCollectorTranslator.
@@ -13,7 +14,6 @@ CHANGELOG
1314
so the class name is misleading. The `TargetOperation` class should be used for
1415
this use-case instead.
1516

16-
1717
2.7.0
1818
-----
1919

src/Symfony/Component/Translation/Dumper/JsonFileDumper.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,26 @@
2020
*/
2121
class JsonFileDumper extends FileDumper
2222
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
27+
{
28+
if (isset($options['json_encoding'])) {
29+
$flags = $options['json_encoding'];
30+
} else {
31+
$flags = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
32+
}
33+
34+
return json_encode($messages->all($domain), $flags);
35+
}
36+
2337
/**
2438
* {@inheritdoc}
2539
*/
2640
public function format(MessageCatalogue $messages, $domain = 'messages')
2741
{
28-
return json_encode($messages->all($domain), defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0);
42+
return $this->formatCatalogue($messages, $domain);
2943
}
3044

3145
/**

src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,39 @@
1616

1717
class JsonFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19-
public function testDump()
19+
private $tempDir;
20+
21+
protected function setUp()
2022
{
2123
if (PHP_VERSION_ID < 50400) {
2224
$this->markTestIncomplete('PHP below 5.4 doesn\'t support JSON pretty printing');
2325
}
2426

27+
$this->tempDir = sys_get_temp_dir();
28+
}
29+
30+
public function testDump()
31+
{
2532
$catalogue = new MessageCatalogue('en');
2633
$catalogue->add(array('foo' => 'bar'));
2734

28-
$tempDir = sys_get_temp_dir();
2935
$dumper = new JsonFileDumper();
30-
$dumper->dump($catalogue, array('path' => $tempDir));
36+
$dumper->dump($catalogue, array('path' => $this->tempDir));
3137

32-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.json'), file_get_contents($tempDir.'/messages.en.json'));
38+
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.json'), file_get_contents($this->tempDir.'/messages.en.json'));
39+
unlink($this->tempDir.'/messages.en.json');
40+
}
41+
42+
public function testDumpWithCustomEncoding()
43+
{
44+
$catalogue = new MessageCatalogue('en');
45+
$catalogue->add(array('foo' => '"bar"'));
46+
47+
$this->tempDir = sys_get_temp_dir();
48+
$dumper = new JsonFileDumper();
49+
$dumper->dump($catalogue, array('path' => $this->tempDir, 'json_encoding' => JSON_HEX_QUOT));
3350

34-
unlink($tempDir.'/messages.en.json');
51+
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.dump.json'), file_get_contents($this->tempDir.'/messages.en.json'));
52+
unlink($this->tempDir.'/messages.en.json');
3553
}
3654
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"foo":"\u0022bar\u0022"}

0 commit comments

Comments
 (0)