Skip to content

Commit

Permalink
[Translation] added option flags to JsonFileDumper.
Browse files Browse the repository at this point in the history
It's passed to json_encode function second argument.
  • Loading branch information
gepo authored and aitboudad committed Sep 11, 2015
1 parent f8fa136 commit bfdc354
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

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


2.7.0
-----

Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/Translation/Dumper/JsonFileDumper.php
Expand Up @@ -25,7 +25,21 @@ class JsonFileDumper extends FileDumper
*/
public function format(MessageCatalogue $messages, $domain = 'messages')
{
return json_encode($messages->all($domain), defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0);
return $this->formatCatalogue($messages, $domain);
}

/**
* {@inheritdoc}
*/
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
if (isset($options['json_encoding'])) {
$flags = $options['json_encoding'];
} else {
$flags = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
}

return json_encode($messages->all($domain), $flags);
}

/**
Expand Down
Expand Up @@ -16,6 +16,18 @@

class JsonFileDumperTest extends \PHPUnit_Framework_TestCase
{
private $tempDir;

protected function setUp()
{
$this->tempDir = sys_get_temp_dir();
}

protected function tearDown()
{
unlink($this->tempDir.'/messages.en.json');
}

public function testDump()
{
if (PHP_VERSION_ID < 50400) {
Expand All @@ -25,12 +37,20 @@ public function testDump()
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));

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

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

public function testDumpWithCustomEncoding()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => '"bar"'));

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

unlink($tempDir.'/messages.en.json');
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.dump.json'), file_get_contents($this->tempDir.'/messages.en.json'));
}
}
@@ -0,0 +1 @@
{"foo":"\u0022bar\u0022"}

0 comments on commit bfdc354

Please sign in to comment.