Skip to content

Commit 45e686c

Browse files
gepoaitboudad
authored andcommitted
[Translation] added option flags to JsonFileDumper.
It's passed to json_encode function second argument.
1 parent f8fa136 commit 45e686c

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
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
@@ -25,7 +25,21 @@ class JsonFileDumper extends FileDumper
2525
*/
2626
public function format(MessageCatalogue $messages, $domain = 'messages')
2727
{
28-
return json_encode($messages->all($domain), defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0);
28+
return $this->formatCatalogue($messages, $domain);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
35+
{
36+
if (isset($options['json_encoding'])) {
37+
$flags = $options['json_encoding'];
38+
} else {
39+
$flags = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
40+
}
41+
42+
return json_encode($messages->all($domain), $flags);
2943
}
3044

3145
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@ public function testDump()
3030
$dumper->dump($catalogue, array('path' => $tempDir));
3131

3232
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.json'), file_get_contents($tempDir.'/messages.en.json'));
33+
unlink($tempDir.'/messages.en.json');
34+
}
35+
36+
public function testDumpWithCustomEncoding()
37+
{
38+
$catalogue = new MessageCatalogue('en');
39+
$catalogue->add(array('foo' => '"bar"'));
40+
41+
$tempDir = sys_get_temp_dir();
42+
$dumper = new JsonFileDumper();
43+
$dumper->dump($catalogue, array('path' => $tempDir, 'json_encoding' => JSON_HEX_QUOT));
3344

45+
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.dump.json'), file_get_contents($tempDir.'/messages.en.json'));
3446
unlink($tempDir.'/messages.en.json');
3547
}
3648
}
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)