From c4fa1cbe88e4ad7460271fe532c10e7cd85883aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Fri, 8 Mar 2013 15:35:32 +0100 Subject: [PATCH 1/3] [Translation] added xliff loader/dumper with resname support --- .../Translation/Dumper/XliffFileDumper.php | 33 ++++++++++---- .../Translation/Dumper/XliffFileDumper2.php | 29 ++++++++++++ .../Translation/Loader/XliffFileLoader.php | 35 ++++++++++++++- .../Translation/Loader/XliffFileLoader2.php | 45 +++++++++++++++++++ .../Tests/Dumper/XliffFileDumper2Test.php | 32 +++++++++++++ .../Tests/Loader/XliffFileLoader2Test.php | 30 +++++++++++++ .../Tests/Loader/XliffFileLoaderTest.php | 17 ++++--- .../Tests/fixtures/resname-clean.xlf | 15 +++++++ .../Translation/Tests/fixtures/resname.xlf | 19 ++++++++ 9 files changed, 238 insertions(+), 17 deletions(-) create mode 100644 src/Symfony/Component/Translation/Dumper/XliffFileDumper2.php create mode 100644 src/Symfony/Component/Translation/Loader/XliffFileLoader2.php create mode 100644 src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumper2Test.php create mode 100644 src/Symfony/Component/Translation/Tests/Loader/XliffFileLoader2Test.php create mode 100644 src/Symfony/Component/Translation/Tests/fixtures/resname-clean.xlf create mode 100644 src/Symfony/Component/Translation/Tests/fixtures/resname.xlf diff --git a/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php b/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php index ab93959ca91e9..89de8e3dd8557 100644 --- a/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php @@ -27,24 +27,22 @@ 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; + $xliffBody = $xliffFile->appendChild($dom->createElement('body')); foreach ($messages->all($domain) as $source => $target) { - $trans = $dom->createElement('trans-unit'); - $trans->setAttribute('id', $id); - $s = $trans->appendChild($dom->createElement('source')); - $s->appendChild($dom->createTextNode($source)); - $t = $trans->appendChild($dom->createElement('target')); - $t->appendChild($dom->createTextNode($target)); - $xliffBody->appendChild($trans); - $id++; + $translation = $dom->createElement('trans-unit'); + $this->buildTranslation($translation, $id++, $source, $target); + $xliffBody->appendChild($translation); } return $dom->saveXML(); @@ -57,4 +55,21 @@ protected function getExtension() { return 'xlf'; } + + /** + * @param \DOMElement $node + * @param string $id + * @param string $source + * @param string $target + */ + protected function buildTranslation(\DOMElement $node, $id, $source, $target) + { + $node->setAttribute('id', $id); + + $s = $node->appendChild($node->ownerDocument->createElement('source')); + $s->appendChild($node->ownerDocument->createTextNode($source)); + + $t = $node->appendChild($node->ownerDocument->createElement('target')); + $t->appendChild($node->ownerDocument->createTextNode($target)); + } } diff --git a/src/Symfony/Component/Translation/Dumper/XliffFileDumper2.php b/src/Symfony/Component/Translation/Dumper/XliffFileDumper2.php new file mode 100644 index 0000000000000..558bcc7a2b888 --- /dev/null +++ b/src/Symfony/Component/Translation/Dumper/XliffFileDumper2.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Dumper; + +/** + * XliffFileDumper generates xliff files with 'resname' attribute support from a message catalogue. + * + * @author Jean-François Simon + */ +class XliffFileDumper2 extends XliffFileDumper +{ + /** + * {@inheritdoc} + */ + protected function buildTranslation(\DOMElement $node, $id, $source, $target) + { + parent::buildTranslation($node, $id, $source, $target); + $node->setAttribute('resname', $source); + } +} diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index e8bc40d47ab8c..3415d296ad6da 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,21 +40,49 @@ 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)) { + if (!$this->validateTranslation($translation)) { continue; } - $catalogue->set((string) $translation->source, (string) $translation->target, $domain); + + list($source, $target) = $this->parseTranslation($translation); + $catalogue->set($source, $target, $domain); } $catalogue->addResource(new FileResource($resource)); return $catalogue; } + /** + * Validates a 'trans-unit' tag. + * + * @param \SimpleXMLElement $translation + * + * @return bool + */ + protected function validateTranslation(\SimpleXMLElement $translation) + { + return isset($translation->source) && isset($translation->target); + } + + /** + * Parses 'trans-unit' tag element. + * + * @param \SimpleXMLElement $translation + * + * @return array An array of 2 elements: the source and the target + */ + protected function parseTranslation(\SimpleXMLElement $translation) + { + return array((string) $translation->source, (string) $translation->target); + } + /** * Validates and parses the given file into a SimpleXMLElement * * @param string $file * + * @throws \RuntimeException + * * @return \SimpleXMLElement */ private function parseFile($file) @@ -109,6 +138,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/Loader/XliffFileLoader2.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader2.php new file mode 100644 index 0000000000000..49b6d45f139bf --- /dev/null +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader2.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Loader; + +use Symfony\Component\DependencyInjection\SimpleXMLElement; + +/** + * XliffFileLoader2 loads translations from XLIFF files with 'resname' attribute support. + * + * @author Jean-François Simon + * + * @api + */ +class XliffFileLoader2 extends XliffFileLoader +{ + /** + * {@inheritdoc} + */ + protected function validateTranslation(\SimpleXMLElement $translation) + { + $attributes = $translation->attributes(); + + return (isset($attributes['resname']) || isset($translation->source)) && isset($translation->target); + } + + /** + * {@inheritdoc} + */ + protected function parseTranslation(\SimpleXMLElement $translation) + { + $attributes = $translation->attributes(); + $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source; + + return array((string) $source, (string) $translation->target); + } +} diff --git a/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumper2Test.php b/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumper2Test.php new file mode 100644 index 0000000000000..eea399b247a78 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumper2Test.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Tests\Dumper; + +use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Dumper\XliffFileDumper2; + +class XliffFileDumper2Test extends \PHPUnit_Framework_TestCase +{ + public function testDump() + { + $catalogue = new MessageCatalogue('en'); + $catalogue->add(array('foo' => 'bar', 'key' => '')); + + $tempDir = sys_get_temp_dir(); + $dumper = new XliffFileDumper2(); + $dumper->dump($catalogue, array('path' => $tempDir)); + + $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resname-clean.xlf'), file_get_contents($tempDir.'/messages.en.xlf')); + + unlink($tempDir.'/messages.en.xlf'); + } +} diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoader2Test.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoader2Test.php new file mode 100644 index 0000000000000..8587fd89ef90a --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoader2Test.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Tests\Loader; + +use Symfony\Component\Translation\Loader\XliffFileLoader2; + +class XliffFileLoader2Test extends XliffFileLoaderTest +{ + 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')); + } + + protected function createLoader() + { + return new XliffFileLoader2(); + } +} diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php index 748c13483f5ec..edd876718156c 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'); @@ -35,7 +35,7 @@ public function testLoad() 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 +47,7 @@ public function testIncompleteResource() */ public function testLoadInvalidResource() { - $loader = new XliffFileLoader(); + $loader = $this->createLoader(); $catalogue = $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1'); } @@ -56,7 +56,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 +65,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 +76,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-clean.xlf b/src/Symfony/Component/Translation/Tests/fixtures/resname-clean.xlf new file mode 100644 index 0000000000000..265adc5488cfd --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/resname-clean.xlf @@ -0,0 +1,15 @@ + + + + + + foo + bar + + + key + + + + + 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 + + + + From 4b3b4e38a1c0c3d1b4bf777ed24f56aab7ec1336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Fri, 8 Mar 2013 16:36:57 +0100 Subject: [PATCH 2/3] [Translation] renamed new xliff loader/dumper --- ...{XliffFileDumper2.php => XliffFileWithResnameDumper.php} | 2 +- ...{XliffFileLoader2.php => XliffFileWithResnameLoader.php} | 2 +- ...leDumper2Test.php => XliffFileWithResnameDumperTest.php} | 6 +++--- ...leLoader2Test.php => XliffFileLoaderWithResnameTest.php} | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) rename src/Symfony/Component/Translation/Dumper/{XliffFileDumper2.php => XliffFileWithResnameDumper.php} (92%) rename src/Symfony/Component/Translation/Loader/{XliffFileLoader2.php => XliffFileWithResnameLoader.php} (95%) rename src/Symfony/Component/Translation/Tests/Dumper/{XliffFileDumper2Test.php => XliffFileWithResnameDumperTest.php} (80%) rename src/Symfony/Component/Translation/Tests/Loader/{XliffFileLoader2Test.php => XliffFileLoaderWithResnameTest.php} (77%) diff --git a/src/Symfony/Component/Translation/Dumper/XliffFileDumper2.php b/src/Symfony/Component/Translation/Dumper/XliffFileWithResnameDumper.php similarity index 92% rename from src/Symfony/Component/Translation/Dumper/XliffFileDumper2.php rename to src/Symfony/Component/Translation/Dumper/XliffFileWithResnameDumper.php index 558bcc7a2b888..6d7eb86e30371 100644 --- a/src/Symfony/Component/Translation/Dumper/XliffFileDumper2.php +++ b/src/Symfony/Component/Translation/Dumper/XliffFileWithResnameDumper.php @@ -16,7 +16,7 @@ * * @author Jean-François Simon */ -class XliffFileDumper2 extends XliffFileDumper +class XliffFileWithResnameDumper extends XliffFileDumper { /** * {@inheritdoc} diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader2.php b/src/Symfony/Component/Translation/Loader/XliffFileWithResnameLoader.php similarity index 95% rename from src/Symfony/Component/Translation/Loader/XliffFileLoader2.php rename to src/Symfony/Component/Translation/Loader/XliffFileWithResnameLoader.php index 49b6d45f139bf..0d61233b8ce7d 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader2.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileWithResnameLoader.php @@ -20,7 +20,7 @@ * * @api */ -class XliffFileLoader2 extends XliffFileLoader +class XliffFileWithResnameLoader extends XliffFileLoader { /** * {@inheritdoc} diff --git a/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumper2Test.php b/src/Symfony/Component/Translation/Tests/Dumper/XliffFileWithResnameDumperTest.php similarity index 80% rename from src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumper2Test.php rename to src/Symfony/Component/Translation/Tests/Dumper/XliffFileWithResnameDumperTest.php index eea399b247a78..563cd84c3b850 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumper2Test.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/XliffFileWithResnameDumperTest.php @@ -12,9 +12,9 @@ namespace Symfony\Component\Translation\Tests\Dumper; use Symfony\Component\Translation\MessageCatalogue; -use Symfony\Component\Translation\Dumper\XliffFileDumper2; +use Symfony\Component\Translation\Dumper\XliffFileWithResnameDumper; -class XliffFileDumper2Test extends \PHPUnit_Framework_TestCase +class XliffFileWithResnameDumperTest extends \PHPUnit_Framework_TestCase { public function testDump() { @@ -22,7 +22,7 @@ public function testDump() $catalogue->add(array('foo' => 'bar', 'key' => '')); $tempDir = sys_get_temp_dir(); - $dumper = new XliffFileDumper2(); + $dumper = new XliffFileWithResnameDumper(); $dumper->dump($catalogue, array('path' => $tempDir)); $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resname-clean.xlf'), file_get_contents($tempDir.'/messages.en.xlf')); diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoader2Test.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderWithResnameTest.php similarity index 77% rename from src/Symfony/Component/Translation/Tests/Loader/XliffFileLoader2Test.php rename to src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderWithResnameTest.php index 8587fd89ef90a..89dbcb65c4e82 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoader2Test.php +++ b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderWithResnameTest.php @@ -11,9 +11,9 @@ namespace Symfony\Component\Translation\Tests\Loader; -use Symfony\Component\Translation\Loader\XliffFileLoader2; +use Symfony\Component\Translation\Loader\XliffFileWithResnameLoader; -class XliffFileLoader2Test extends XliffFileLoaderTest +class XliffFileWithResnameLoaderTest extends XliffFileLoaderTest { public function testLoadWithResname() { @@ -25,6 +25,6 @@ public function testLoadWithResname() protected function createLoader() { - return new XliffFileLoader2(); + return new XliffFileWithResnameLoader(); } } From 44daf7f1b16e9ad68f2b0635cf079ad1179e3dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Sat, 9 Mar 2013 14:14:17 +0100 Subject: [PATCH 3/3] [Translator] merged xlif classes --- .../Translation/Dumper/XliffFileDumper.php | 29 +++++------- .../Dumper/XliffFileWithResnameDumper.php | 29 ------------ .../Translation/Loader/XliffFileLoader.php | 32 +++---------- .../Loader/XliffFileWithResnameLoader.php | 45 ------------------- .../Dumper/XliffFileWithResnameDumperTest.php | 32 ------------- .../Tests/Loader/XliffFileLoaderTest.php | 8 ++++ .../Loader/XliffFileLoaderWithResnameTest.php | 30 ------------- .../Tests/fixtures/resname-clean.xlf | 15 ------- .../Tests/fixtures/resources-clean.xlf | 4 +- 9 files changed, 25 insertions(+), 199 deletions(-) delete mode 100644 src/Symfony/Component/Translation/Dumper/XliffFileWithResnameDumper.php delete mode 100644 src/Symfony/Component/Translation/Loader/XliffFileWithResnameLoader.php delete mode 100644 src/Symfony/Component/Translation/Tests/Dumper/XliffFileWithResnameDumperTest.php delete mode 100644 src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderWithResnameTest.php delete mode 100644 src/Symfony/Component/Translation/Tests/fixtures/resname-clean.xlf diff --git a/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php b/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php index 89de8e3dd8557..0d258aad363cb 100644 --- a/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/XliffFileDumper.php @@ -37,11 +37,19 @@ protected function format(MessageCatalogue $messages, $domain) $xliffFile->setAttribute('datatype', 'plaintext'); $xliffFile->setAttribute('original', 'file.ext'); - $id = 1; $xliffBody = $xliffFile->appendChild($dom->createElement('body')); foreach ($messages->all($domain) as $source => $target) { $translation = $dom->createElement('trans-unit'); - $this->buildTranslation($translation, $id++, $source, $target); + + $translation->setAttribute('id', md5($source)); + $translation->setAttribute('resname', $source); + + $s = $translation->appendChild($dom->createElement('source')); + $s->appendChild($dom->createTextNode($source)); + + $t = $translation->appendChild($dom->createElement('target')); + $t->appendChild($dom->createTextNode($target)); + $xliffBody->appendChild($translation); } @@ -55,21 +63,4 @@ protected function getExtension() { return 'xlf'; } - - /** - * @param \DOMElement $node - * @param string $id - * @param string $source - * @param string $target - */ - protected function buildTranslation(\DOMElement $node, $id, $source, $target) - { - $node->setAttribute('id', $id); - - $s = $node->appendChild($node->ownerDocument->createElement('source')); - $s->appendChild($node->ownerDocument->createTextNode($source)); - - $t = $node->appendChild($node->ownerDocument->createElement('target')); - $t->appendChild($node->ownerDocument->createTextNode($target)); - } } diff --git a/src/Symfony/Component/Translation/Dumper/XliffFileWithResnameDumper.php b/src/Symfony/Component/Translation/Dumper/XliffFileWithResnameDumper.php deleted file mode 100644 index 6d7eb86e30371..0000000000000 --- a/src/Symfony/Component/Translation/Dumper/XliffFileWithResnameDumper.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Translation\Dumper; - -/** - * XliffFileDumper generates xliff files with 'resname' attribute support from a message catalogue. - * - * @author Jean-François Simon - */ -class XliffFileWithResnameDumper extends XliffFileDumper -{ - /** - * {@inheritdoc} - */ - protected function buildTranslation(\DOMElement $node, $id, $source, $target) - { - parent::buildTranslation($node, $id, $source, $target); - $node->setAttribute('resname', $source); - } -} diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index 3415d296ad6da..996b86d824543 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php @@ -40,42 +40,20 @@ public function load($resource, $locale, $domain = 'messages') $catalogue = new MessageCatalogue($locale); foreach ($xml->xpath('//xliff:trans-unit') as $translation) { - if (!$this->validateTranslation($translation)) { + $attributes = $translation->attributes(); + + if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) { continue; } - list($source, $target) = $this->parseTranslation($translation); - $catalogue->set($source, $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)); return $catalogue; } - /** - * Validates a 'trans-unit' tag. - * - * @param \SimpleXMLElement $translation - * - * @return bool - */ - protected function validateTranslation(\SimpleXMLElement $translation) - { - return isset($translation->source) && isset($translation->target); - } - - /** - * Parses 'trans-unit' tag element. - * - * @param \SimpleXMLElement $translation - * - * @return array An array of 2 elements: the source and the target - */ - protected function parseTranslation(\SimpleXMLElement $translation) - { - return array((string) $translation->source, (string) $translation->target); - } - /** * Validates and parses the given file into a SimpleXMLElement * diff --git a/src/Symfony/Component/Translation/Loader/XliffFileWithResnameLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileWithResnameLoader.php deleted file mode 100644 index 0d61233b8ce7d..0000000000000 --- a/src/Symfony/Component/Translation/Loader/XliffFileWithResnameLoader.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Translation\Loader; - -use Symfony\Component\DependencyInjection\SimpleXMLElement; - -/** - * XliffFileLoader2 loads translations from XLIFF files with 'resname' attribute support. - * - * @author Jean-François Simon - * - * @api - */ -class XliffFileWithResnameLoader extends XliffFileLoader -{ - /** - * {@inheritdoc} - */ - protected function validateTranslation(\SimpleXMLElement $translation) - { - $attributes = $translation->attributes(); - - return (isset($attributes['resname']) || isset($translation->source)) && isset($translation->target); - } - - /** - * {@inheritdoc} - */ - protected function parseTranslation(\SimpleXMLElement $translation) - { - $attributes = $translation->attributes(); - $source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source; - - return array((string) $source, (string) $translation->target); - } -} diff --git a/src/Symfony/Component/Translation/Tests/Dumper/XliffFileWithResnameDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/XliffFileWithResnameDumperTest.php deleted file mode 100644 index 563cd84c3b850..0000000000000 --- a/src/Symfony/Component/Translation/Tests/Dumper/XliffFileWithResnameDumperTest.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Translation\Tests\Dumper; - -use Symfony\Component\Translation\MessageCatalogue; -use Symfony\Component\Translation\Dumper\XliffFileWithResnameDumper; - -class XliffFileWithResnameDumperTest extends \PHPUnit_Framework_TestCase -{ - public function testDump() - { - $catalogue = new MessageCatalogue('en'); - $catalogue->add(array('foo' => 'bar', 'key' => '')); - - $tempDir = sys_get_temp_dir(); - $dumper = new XliffFileWithResnameDumper(); - $dumper->dump($catalogue, array('path' => $tempDir)); - - $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resname-clean.xlf'), file_get_contents($tempDir.'/messages.en.xlf')); - - unlink($tempDir.'/messages.en.xlf'); - } -} diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php index edd876718156c..5fad58617a5b6 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php @@ -33,6 +33,14 @@ 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 = $this->createLoader(); diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderWithResnameTest.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderWithResnameTest.php deleted file mode 100644 index 89dbcb65c4e82..0000000000000 --- a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderWithResnameTest.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Translation\Tests\Loader; - -use Symfony\Component\Translation\Loader\XliffFileWithResnameLoader; - -class XliffFileWithResnameLoaderTest extends XliffFileLoaderTest -{ - 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')); - } - - protected function createLoader() - { - return new XliffFileWithResnameLoader(); - } -} diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resname-clean.xlf b/src/Symfony/Component/Translation/Tests/fixtures/resname-clean.xlf deleted file mode 100644 index 265adc5488cfd..0000000000000 --- a/src/Symfony/Component/Translation/Tests/fixtures/resname-clean.xlf +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - foo - bar - - - key - - - - - 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