Skip to content

Commit 036726c

Browse files
committed
bug #11513 [Translation] made XliffFileDumper support CDATA sections. (hhamon)
This PR was merged into the 2.3 branch. Discussion ---------- [Translation] made XliffFileDumper support CDATA sections. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | maybe | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11256 | License | MIT Commits ------- 9926845 [Translation] made XliffFileDumper support CDATA sections.
2 parents f776e0c + 9926845 commit 036726c

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/Symfony/Component/Translation/Dumper/XliffFileDumper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ protected function format(MessageCatalogue $messages, $domain)
4747
$s = $translation->appendChild($dom->createElement('source'));
4848
$s->appendChild($dom->createTextNode($source));
4949

50+
// Does the target contain characters requiring a CDATA section?
51+
$text = (1 === preg_match('/[&<>]/', $target))
52+
? $dom->createCDATASection($target)
53+
: $dom->createTextNode($target);
54+
5055
$t = $translation->appendChild($dom->createElement('target'));
51-
$t->appendChild($dom->createTextNode($target));
56+
$t->appendChild($text);
5257

5358
$xliffBody->appendChild($translation);
5459
}

src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumperTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
1919
public function testDump()
2020
{
2121
$catalogue = new MessageCatalogue('en');
22-
$catalogue->add(array('foo' => 'bar', 'key' => ''));
22+
$catalogue->add(array(
23+
'foo' => 'bar',
24+
'key' => '',
25+
'key.with.cdata' => '<source> & <target>',
26+
));
2327

2428
$tempDir = sys_get_temp_dir();
2529
$dumper = new XliffFileDumper();
2630
$dumper->dump($catalogue, array('path' => $tempDir));
2731

28-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'), file_get_contents($tempDir.'/messages.en.xlf'));
32+
$this->assertSame(
33+
file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'),
34+
file_get_contents($tempDir.'/messages.en.xlf')
35+
);
2936

3037
unlink($tempDir.'/messages.en.xlf');
3138
}

src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
<source>key</source>
1111
<target></target>
1212
</trans-unit>
13+
<trans-unit id="18e6a493872558d949b4c16ea1fa6ab6" resname="key.with.cdata">
14+
<source>key.with.cdata</source>
15+
<target><![CDATA[<source> & <target>]]></target>
16+
</trans-unit>
1317
</body>
1418
</file>
1519
</xliff>

0 commit comments

Comments
 (0)