diff --git a/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php b/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php index 47e082c22e99..668c90d6468a 100644 --- a/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php @@ -20,30 +20,22 @@ */ class XliffFileDumper extends FileDumper { - /** - * @var string - */ - private $defaultLocale; - /** * {@inheritdoc} */ - public function dump(MessageCatalogue $messages, $options = array()) + protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) { if (array_key_exists('default_locale', $options)) { - $this->defaultLocale = $options['default_locale']; + $defaultLocale = $options['default_locale']; } else { - $this->defaultLocale = \Locale::getDefault(); + $defaultLocale = \Locale::getDefault(); } - parent::dump($messages, $options); - } + $toolInfo = array('tool-id' => 'symfony', 'tool-name' => 'Symfony'); + if (array_key_exists('tool_info', $options)) { + $toolInfo = array_merge($toolInfo, $options['tool_info']); + } - /** - * {@inheritdoc} - */ - protected function format(MessageCatalogue $messages, $domain) - { $dom = new \DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; @@ -52,11 +44,17 @@ protected function format(MessageCatalogue $messages, $domain) $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'); $xliffFile = $xliff->appendChild($dom->createElement('file')); - $xliffFile->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale)); + $xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale)); $xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale())); $xliffFile->setAttribute('datatype', 'plaintext'); $xliffFile->setAttribute('original', 'file.ext'); + $xliffHead = $xliffFile->appendChild($dom->createElement('header')); + $xliffTool = $xliffHead->appendChild($dom->createElement('tool')); + foreach ($toolInfo as $id => $value) { + $xliffTool->setAttribute($id, $value); + } + $xliffBody = $xliffFile->appendChild($dom->createElement('body')); foreach ($messages->all($domain) as $source => $target) { $translation = $dom->createElement('trans-unit'); @@ -105,6 +103,14 @@ protected function format(MessageCatalogue $messages, $domain) return $dom->saveXML(); } + /** + * {@inheritdoc} + */ + protected function format(MessageCatalogue $messages, $domain) + { + return $this->formatCatalogue($messages, $domain); + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumperTest.php index b1f9826c295b..d2cbf4668dee 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumperTest.php @@ -16,6 +16,13 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase { + private $tempDir; + + protected function setUp() + { + $this->tempDir = sys_get_temp_dir(); + } + public function testDump() { $catalogue = new MessageCatalogue('en_US'); @@ -27,19 +34,40 @@ public function testDump() $catalogue->setMetadata('foo', array('notes' => array(array('priority' => 1, 'from' => 'bar', 'content' => 'baz')))); $catalogue->setMetadata('key', array('notes' => array(array('content' => 'baz'), array('content' => 'qux')))); - $tempDir = sys_get_temp_dir(); $dumper = new XliffFileDumper(); - $dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR')); + $dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR')); $this->assertEquals( file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'), - file_get_contents($tempDir.'/messages.en_US.xlf') + file_get_contents($this->tempDir.'/messages.en_US.xlf') + ); + + unlink($this->tempDir.'/messages.en_US.xlf'); + } + + public function testDumpWithCustomToolInfo() + { + $options = array( + 'path' => $this->tempDir, + 'default_locale' => 'en_US', + 'tool_info' => array('tool-id' => 'foo', 'tool-name' => 'foo', 'tool-version' => '0.0', 'tool-company' => 'Foo'), + ); + + $catalogue = new MessageCatalogue('en_US'); + $catalogue->add(array('foo' => 'bar')); + + $dumper = new XliffFileDumper(); + $dumper->dump($catalogue, $options); + + $this->assertEquals( + file_get_contents(__DIR__.'/../fixtures/resources-tool-info.xlf'), + file_get_contents($this->tempDir.'/messages.en_US.xlf') ); - unlink($tempDir.'/messages.en_US.xlf'); + unlink($this->tempDir.'/messages.en_US.xlf'); } - public function testTargetAttributesMetadataIsSetInFile() + public function testDumpWithTargetAttributesMetadata() { $catalogue = new MessageCatalogue('en_US'); $catalogue->add(array( @@ -47,30 +75,15 @@ public function testTargetAttributesMetadataIsSetInFile() )); $catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation'))); - $tempDir = sys_get_temp_dir(); + $this->tempDir = sys_get_temp_dir(); $dumper = new XliffFileDumper(); - $dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR')); - - $content = << - - - - - foo - bar - - - - - -EOT; + $dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR')); $this->assertEquals( - $content, - file_get_contents($tempDir.'/messages.en_US.xlf') + file_get_contents(__DIR__.'/../fixtures/resources-target-attributes.xlf'), + file_get_contents($this->tempDir.'/messages.en_US.xlf') ); - unlink($tempDir.'/messages.en_US.xlf'); + unlink($this->tempDir.'/messages.en_US.xlf'); } } diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf b/src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf index 4ce15af2f5f7..436e19ec98be 100644 --- a/src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf +++ b/src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf @@ -1,6 +1,9 @@ +
+ +
foo diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resources-target-attributes.xlf b/src/Symfony/Component/Translation/Tests/fixtures/resources-target-attributes.xlf new file mode 100644 index 000000000000..e3afb498b76b --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/resources-target-attributes.xlf @@ -0,0 +1,14 @@ + + + +
+ +
+ + + foo + bar + + +
+
diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resources-tool-info.xlf b/src/Symfony/Component/Translation/Tests/fixtures/resources-tool-info.xlf new file mode 100644 index 000000000000..1ed06d2ac427 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/resources-tool-info.xlf @@ -0,0 +1,14 @@ + + + +
+ +
+ + + foo + bar + + +
+