[Translation] made XliffFileDumper support CDATA sections.

This commit is contained in:
Hugo Hamon 2014-07-29 21:37:35 +02:00
parent cd005e69ff
commit 9926845cf3
3 changed files with 19 additions and 3 deletions

View File

@ -47,8 +47,13 @@ class XliffFileDumper extends FileDumper
$s = $translation->appendChild($dom->createElement('source'));
$s->appendChild($dom->createTextNode($source));
// Does the target contain characters requiring a CDATA section?
$text = (1 === preg_match('/[&<>]/', $target))
? $dom->createCDATASection($target)
: $dom->createTextNode($target);
$t = $translation->appendChild($dom->createElement('target'));
$t->appendChild($dom->createTextNode($target));
$t->appendChild($text);
$xliffBody->appendChild($translation);
}

View File

@ -19,13 +19,20 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
public function testDump()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar', 'key' => ''));
$catalogue->add(array(
'foo' => 'bar',
'key' => '',
'key.with.cdata' => '<source> & <target>',
));
$tempDir = sys_get_temp_dir();
$dumper = new XliffFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'), file_get_contents($tempDir.'/messages.en.xlf'));
$this->assertSame(
file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'),
file_get_contents($tempDir.'/messages.en.xlf')
);
unlink($tempDir.'/messages.en.xlf');
}

View File

@ -10,6 +10,10 @@
<source>key</source>
<target></target>
</trans-unit>
<trans-unit id="18e6a493872558d949b4c16ea1fa6ab6" resname="key.with.cdata">
<source>key.with.cdata</source>
<target><![CDATA[<source> & <target>]]></target>
</trans-unit>
</body>
</file>
</xliff>