Skip to content

[Translation] added option json_options to JsonFileDumper. #14750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

2.7.0
-----
Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/Translation/Dumper/JsonFileDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,26 @@
*/
class JsonFileDumper extends FileDumper
{
/**
* {@inheritdoc}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong docblock. This method isn't inherited from anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's 2.8 branch

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry. Now I understand. Please forget about my next comment.

*/
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);
}

/**
* {@inheritdoc}
*/
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public function testDump()
}

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

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

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

unlink($tempDir.'/messages.en.json');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo":"\u0022bar\u0022"}