Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb11aac

Browse files
committed
[Translation] added option flags to JsonFileDumper.
It's passed to json_encode function second argument.
1 parent a57ce90 commit fb11aac

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* deprecated Translator::getMessages(), use TranslatorBagInterface::getCatalogue() instead.
88
* added options 'as_tree', 'inline' to YamlFileDumper
9+
* added option 'json_encoding' to JsonFileDumper
910

1011
2.7.0
1112
-----

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+
$flags = isset($options['json_encoding']) ? $options['json_encoding'] : 0;
29+
30+
if (defined('JSON_PRETTY_PRINT')) {
31+
$flags |= JSON_PRETTY_PRINT;
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public function testDump()
2323
}
2424

2525
$catalogue = new MessageCatalogue('en');
26-
$catalogue->add(array('foo' => 'bar'));
26+
$catalogue->add(array('foo' => '"bar"'));
2727

2828
$tempDir = sys_get_temp_dir();
2929
$dumper = new JsonFileDumper();
30-
$dumper->dump($catalogue, array('path' => $tempDir));
30+
$dumper->dump($catalogue, array('path' => $tempDir, 'json_encoding' => JSON_HEX_QUOT));
3131

3232
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.json'), file_get_contents($tempDir.'/messages.en.json'));
3333

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"foo": "bar"
2+
"foo": "\u0022bar\u0022"
33
}

0 commit comments

Comments
 (0)