feature #15756 [Translation] added option json_options to JsonFileDumper. (gepo)

This PR was merged into the 2.8 branch.

Discussion
----------

[Translation] added option json_options to JsonFileDumper.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | no
| Fixed tickets | ~
| License       | MIT
| Doc PR        |

Replaces #14750

Commits
-------

bfdc354 [Translation] added option flags to JsonFileDumper. It's passed to json_encode function second argument.
This commit is contained in:
Abdellatif Ait boudad 2015-09-11 11:34:42 +00:00
commit f29dd93d5c
4 changed files with 41 additions and 6 deletions

View File

@ -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
-----

View File

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

View File

@ -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'));
}
}

View File

@ -0,0 +1 @@
{"foo":"\u0022bar\u0022"}