diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index d02919d9da..41f99cb4e9 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -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. @@ -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 ----- diff --git a/src/Symfony/Component/Translation/Dumper/JsonFileDumper.php b/src/Symfony/Component/Translation/Dumper/JsonFileDumper.php index 7ad35184cd..2f4156d899 100644 --- a/src/Symfony/Component/Translation/Dumper/JsonFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/JsonFileDumper.php @@ -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); } /** diff --git a/src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php index 697cd939f6..0d6ffd58f9 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php @@ -16,6 +16,18 @@ use Symfony\Component\Translation\Dumper\JsonFileDumper; 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) { @@ -25,12 +37,20 @@ class JsonFileDumperTest extends \PHPUnit_Framework_TestCase $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')); + } - unlink($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)); + + $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.dump.json'), file_get_contents($this->tempDir.'/messages.en.json')); } } diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resources.dump.json b/src/Symfony/Component/Translation/Tests/fixtures/resources.dump.json new file mode 100644 index 0000000000..335965d592 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/resources.dump.json @@ -0,0 +1 @@ +{"foo":"\u0022bar\u0022"} \ No newline at end of file