diff --git a/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php b/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php index ab93959ca91e9..0d258aad363cb 100644 --- a/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php @@ -27,24 +27,30 @@ protected function format(MessageCatalogue $messages, $domain) { $dom = new \DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; + $xliff = $dom->appendChild($dom->createElement('xliff')); $xliff->setAttribute('version', '1.2'); $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'); + $xliffFile = $xliff->appendChild($dom->createElement('file')); $xliffFile->setAttribute('source-language', $messages->getLocale()); $xliffFile->setAttribute('datatype', 'plaintext'); $xliffFile->setAttribute('original', 'file.ext'); + $xliffBody = $xliffFile->appendChild($dom->createElement('body')); - $id = 1; foreach ($messages->all($domain) as $source => $target) { - $trans = $dom->createElement('trans-unit'); - $trans->setAttribute('id', $id); - $s = $trans->appendChild($dom->createElement('source')); + $translation = $dom->createElement('trans-unit'); + + $translation->setAttribute('id', md5($source)); + $translation->setAttribute('resname', $source); + + $s = $translation->appendChild($dom->createElement('source')); $s->appendChild($dom->createTextNode($source)); - $t = $trans->appendChild($dom->createElement('target')); + + $t = $translation->appendChild($dom->createElement('target')); $t->appendChild($dom->createTextNode($target)); - $xliffBody->appendChild($trans); - $id++; + + $xliffBody->appendChild($translation); } return $dom->saveXML(); diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index e8bc40d47ab8c..996b86d824543 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Translation\Loader; +use Symfony\Component\DependencyInjection\SimpleXMLElement; use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Config\Resource\FileResource; @@ -39,10 +40,14 @@ public function load($resource, $locale, $domain = 'messages') $catalogue = new MessageCatalogue($locale); foreach ($xml->xpath('//xliff:trans-unit') as $translation) { - if (!isset($translation->source) || !isset($translation->target)) { + $attributes = $translation->attributes(); + + if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) { continue; } - $catalogue->set((string) $translation->source, (string) $translation->target, $domain); + + $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source; + $catalogue->set((string) $source, (string) $translation->target, $domain); } $catalogue->addResource(new FileResource($resource)); @@ -54,6 +59,8 @@ public function load($resource, $locale, $domain = 'messages') * * @param string $file * + * @throws \RuntimeException + * * @return \SimpleXMLElement */ private function parseFile($file) @@ -109,6 +116,8 @@ private function parseFile($file) /** * Returns the XML errors of the internal XML parser * + * @param boolean $internalErrors + * * @return array An array of errors */ private function getXmlErrors($internalErrors) diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php index 748c13483f5ec..5fad58617a5b6 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php @@ -25,7 +25,7 @@ protected function setUp() public function testLoad() { - $loader = new XliffFileLoader(); + $loader = $this->createLoader(); $resource = __DIR__.'/../fixtures/resources.xlf'; $catalogue = $loader->load($resource, 'en', 'domain1'); @@ -33,9 +33,17 @@ public function testLoad() $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); } + public function testLoadWithResname() + { + $loader = $this->createLoader(); + $catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1'); + + $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1')); + } + public function testIncompleteResource() { - $loader = new XliffFileLoader(); + $loader = $this->createLoader(); $catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1'); $this->assertEquals(array('foo' => 'bar', 'key' => '', 'test' => 'with'), $catalogue->all('domain1')); @@ -47,7 +55,7 @@ public function testIncompleteResource() */ public function testLoadInvalidResource() { - $loader = new XliffFileLoader(); + $loader = $this->createLoader(); $catalogue = $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1'); } @@ -56,7 +64,7 @@ public function testLoadInvalidResource() */ public function testLoadResourceDoesNotValidate() { - $loader = new XliffFileLoader(); + $loader = $this->createLoader(); $catalogue = $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1'); } @@ -65,7 +73,7 @@ public function testLoadResourceDoesNotValidate() */ public function testLoadThrowsAnExceptionIfFileNotLocal() { - $loader = new XliffFileLoader(); + $loader = $this->createLoader(); $resource = 'http://example.com/resources.xlf'; $loader->load($resource, 'en', 'domain1'); } @@ -76,7 +84,12 @@ public function testLoadThrowsAnExceptionIfFileNotLocal() */ public function testDocTypeIsNotAllowed() { - $loader = new XliffFileLoader(); + $loader = $this->createLoader(); $loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1'); } + + protected function createLoader() + { + return new XliffFileLoader(); + } } diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resname.xlf b/src/Symfony/Component/Translation/Tests/fixtures/resname.xlf new file mode 100644 index 0000000000000..2df16af942f43 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/resname.xlf @@ -0,0 +1,19 @@ + + + + + + + bar + + + bar source + baz + + + baz + foo + + + + diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf b/src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf index 231e8a6dd2e5c..464b079200211 100644 --- a/src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf +++ b/src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf @@ -2,11 +2,11 @@ - + foo bar - + key