Skip to content

Commit 16b6178

Browse files
authored
Fix double escaping in XLIFF (#1011)
1 parent a2d2e5d commit 16b6178

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

app/classes/Transvision/Xliff.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static function getStrings($xliff_path, $project_name, $reference_locale
3232
translation (target).
3333
*/
3434
$trans_units = $xml->xpath('//x:trans-unit');
35+
3536
foreach ($trans_units as $trans_unit) {
3637
$file_node = $trans_unit->xpath('../..');
3738
$file_orig = $file_node[0]['original'];
@@ -40,16 +41,15 @@ public static function getStrings($xliff_path, $project_name, $reference_locale
4041

4142
if ($reference_locale) {
4243
// If it's the reference locale, we use the source instead of the target
43-
$translation = str_replace("'", "\\'", $trans_unit->source);
44-
$strings[$string_id] = $translation;
44+
45+
$strings[$string_id] = addslashes($trans_unit->source);
4546
} elseif (isset($trans_unit->target)) {
4647
/*
4748
We only store the translation if the target is set.
4849
simplexml returns an empty string if the element is
4950
missing.
5051
*/
51-
$translation = str_replace("'", "\\'", $trans_unit->target);
52-
$strings[$string_id] = $translation;
52+
$strings[$string_id] = addslashes($trans_unit->target);
5353
}
5454
}
5555
}

tests/testfiles/xliff/firefox-ios.xliff

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@
7373
<target>Accessi salvati</target>
7474
<note>Settings item for clearing passwords and login data</note>
7575
</trans-unit>
76+
<trans-unit id="test_escape">
77+
<source>Test with '</source>
78+
<target>Test con '</target>
79+
</trans-unit>
80+
<trans-unit id="test_already_escaped">
81+
<source>Test with \' already escaped</source>
82+
<target>Test con \' già escaped</target>
83+
</trans-unit>
7684
</body>
7785
</file>
7886
<file original="Client/ClearPrivateData2.strings" source-language="en" datatype="plaintext" target-language="it">

tests/units/Transvision/Xliff.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testGetStrings()
1616
// Check total number of strings
1717
$this
1818
->integer(count($strings))
19-
->isEqualTo(13);
19+
->isEqualTo(15);
2020

2121
// Check strings
2222
$this
@@ -26,6 +26,15 @@ public function testGetStrings()
2626
$this
2727
->string($strings['firefox_ios/firefox-ios.xliff:f6b6d1aff4ade5b867d563d74b01a429'])
2828
->isEqualTo('Segnalibri pc desktop');
29+
30+
// Check escaped single straight quotes
31+
$this
32+
->string($strings['firefox_ios/firefox-ios.xliff:e15c1a9a6082aa32623205328418a603'])
33+
->isEqualTo("Test con \'");
34+
35+
$this
36+
->string($strings['firefox_ios/firefox-ios.xliff:1348465d2e7136641805937598daaeda'])
37+
->isEqualTo("Test con \\\\\' già escaped");
2938
}
3039

3140
public function testGetStringsReference()
@@ -36,7 +45,7 @@ public function testGetStringsReference()
3645
// Check total number of strings
3746
$this
3847
->integer(count($strings))
39-
->isEqualTo(14);
48+
->isEqualTo(16);
4049

4150
// Check strings
4251
$this
@@ -46,6 +55,15 @@ public function testGetStringsReference()
4655
$this
4756
->string($strings['firefox_ios/firefox-ios.xliff:f6b6d1aff4ade5b867d563d74b01a429'])
4857
->isEqualTo('Desktop Bookmarks');
58+
59+
// Check escaped single straight quotes
60+
$this
61+
->string($strings['firefox_ios/firefox-ios.xliff:e15c1a9a6082aa32623205328418a603'])
62+
->isEqualTo("Test with \'");
63+
64+
$this
65+
->string($strings['firefox_ios/firefox-ios.xliff:1348465d2e7136641805937598daaeda'])
66+
->isEqualTo("Test with \\\\\' already escaped");
4967
}
5068

5169
public function generateStringID_DP()

0 commit comments

Comments
 (0)