Skip to content

Commit bfdc354

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

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
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: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
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+
1931
public function testDump()
2032
{
2133
if (PHP_VERSION_ID < 50400) {
@@ -25,12 +37,20 @@ public function testDump()
2537
$catalogue = new MessageCatalogue('en');
2638
$catalogue->add(array('foo' => 'bar'));
2739

28-
$tempDir = sys_get_temp_dir();
2940
$dumper = new JsonFileDumper();
30-
$dumper->dump($catalogue, array('path' => $tempDir));
41+
$dumper->dump($catalogue, array('path' => $this->tempDir));
3142

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

34-
unlink($tempDir.'/messages.en.json');
54+
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.dump.json'), file_get_contents($this->tempDir.'/messages.en.json'));
3555
}
3656
}
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy