Browse Source

Merge pull request #14754 from cakephp/backport-14751

3.x - Don't modify strings from binary MO files
Mark Story 5 years ago
parent
commit
eab8d2b531

+ 2 - 2
src/I18n/Parser/MoFileParser.php

@@ -119,11 +119,11 @@ class MoFileParser
 
             if ($pluralId !== null || strpos($translated, "\000") !== false) {
                 $translated = explode("\000", $translated);
-                $plurals = $pluralId !== null ? array_map('stripcslashes', $translated) : null;
+                $plurals = $pluralId !== null ? $translated : null;
                 $translated = $translated[0];
             }
 
-            $singular = stripcslashes($translated);
+            $singular = $translated;
             if ($context !== null) {
                 $messages[$singularId]['_context'][$context] = $singular;
                 if ($pluralId !== null) {

+ 6 - 1
tests/TestCase/I18n/Parser/MoFileParserTest.php

@@ -66,7 +66,7 @@ class MoFileParserTest extends TestCase
         $parser = new MoFileParser();
         $file = APP . 'Locale' . DS . 'rule_0_mo' . DS . 'core.mo';
         $messages = $parser->parse($file);
-        $this->assertCount(3, $messages);
+        $this->assertCount(4, $messages);
         $expected = [
             'Plural Rule 1 (from core)' => [
                 '_context' => [
@@ -85,6 +85,11 @@ class MoFileParserTest extends TestCase
                     ],
                 ],
             ],
+            "new line: \nno new line: \\n" => [
+                '_context' => [
+                    '' => "new line: \nno new line: \\n (translated)",
+                ],
+            ],
         ];
         $this->assertEquals($expected, $messages);
     }

+ 3 - 1
tests/TestCase/I18n/Parser/PoFileParserTest.php

@@ -133,8 +133,10 @@ class PoFileParserTest extends TestCase
         $parser = new PoFileParser();
         $file = APP . 'Locale' . DS . 'en' . DS . 'default.po';
         $messages = $parser->parse($file);
-        $this->assertCount(12, $messages);
+        $this->assertCount(13, $messages);
         $this->assertTextEquals("v\nsecond line", $messages["valid\nsecond line"]['_context']['']);
+
+        $this->assertTextEquals("new line: \nno new line: \\n (translated)", $messages["new line: \nno new line: \\n"]['_context']['']);
     }
 
     /**

+ 6 - 0
tests/test_app/TestApp/Locale/en/default.po

@@ -74,3 +74,9 @@ msgstr[1] "%d is 2-4\n"
 
 msgid "this is a \"quoted string\""
 msgstr "this is a \"quoted string\" (translated)"
+""
+
+msgid "new line: \n"
+"no new line: \\n"
+msgstr "new line: \n"
+"no new line: \\n (translated)"

BIN
tests/test_app/TestApp/Locale/rule_0_mo/core.mo


+ 5 - 0
tests/test_app/TestApp/Locale/rule_0_po/core.po

@@ -19,3 +19,8 @@ msgid "%d = 1 (from core)"
 msgid_plural "%d = 0 or > 1 (from core)"
 msgstr[0] "%d ends with any # (from core translated)"
 
+msgid "new line: \n"
+"no new line: \\n"
+msgstr "new line: \n"
+"no new line: \\n (translated)"
+