From dc92f6544fe1284ff59729538922b1bb7e1835ae Mon Sep 17 00:00:00 2001 From: oscarotero Date: Tue, 26 Mar 2019 02:11:34 +0100 Subject: [PATCH] fixed mulitiline extractions in jsCode #200 --- src/Utils/JsFunctionsScanner.php | 55 +++++++++++++++++++++++- tests/AssetsTest.php | 1 - tests/assets/jscode2/Csv.csv | 3 +- tests/assets/jscode2/CsvDictionary.csv | 3 +- tests/assets/jscode2/Jed.json | 2 +- tests/assets/jscode2/Json.json | 2 +- tests/assets/jscode2/JsonDictionary.json | 2 +- tests/assets/jscode2/PhpArray.php | 3 +- tests/assets/jscode2/Po.po | 4 +- tests/assets/jscode2/Xliff.xlf | 5 ++- tests/assets/jscode2/Yaml.yml | 2 +- tests/assets/jscode2/YamlDictionary.yml | 2 +- tests/assets/jscode2/input.js | 2 +- 13 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/Utils/JsFunctionsScanner.php b/src/Utils/JsFunctionsScanner.php index 319cf888..4d57747d 100644 --- a/src/Utils/JsFunctionsScanner.php +++ b/src/Utils/JsFunctionsScanner.php @@ -37,6 +37,20 @@ public function getFunctions(array $constants = []) switch ($char) { case '\\': + switch ($this->status()) { + case 'simple-quote': + if ($next !== "'") { + break 2; + } + break; + + case 'double-quote': + if ($next !== '"') { + break 2; + } + break; + } + $prev = $char; $char = $next; $pos++; @@ -233,7 +247,46 @@ protected function upStatus() protected static function prepareArgument($argument) { if ($argument && ($argument[0] === '"' || $argument[0] === "'")) { - return substr($argument, 1, -1); + return static::convertString(substr($argument, 1, -1)); } } + + /** + * Decodes a string with an argument. + * + * @param string $value + * + * @return string + */ + protected static function convertString($value) + { + if (strpos($value, '\\') === false) { + return $value; + } + + return preg_replace_callback( + '/\\\(n|r|t|v|e|f|"|\\\)/', + function ($match) { + switch ($match[1][0]) { + case 'n': + return "\n"; + case 'r': + return "\r"; + case 't': + return "\t"; + case 'v': + return "\v"; + case 'e': + return "\e"; + case 'f': + return "\f"; + case '"': + return '"'; + case '\\': + return '\\'; + } + }, + $value + ); + } } diff --git a/tests/AssetsTest.php b/tests/AssetsTest.php index e90968b8..53704090 100644 --- a/tests/AssetsTest.php +++ b/tests/AssetsTest.php @@ -239,7 +239,6 @@ public function testJs2Code() $this->assertCount($countTranslations, $translations); $this->assertCount($countHeaders, $translations->getHeaders()); $this->assertEquals(0, $translations->countTranslated()); - $this->assertContent($translations, 'jscode2/Po'); $this->assertContent($translations, 'jscode2/Mo'); $this->assertContent($translations, 'jscode2/PhpArray'); diff --git a/tests/assets/jscode2/Csv.csv b/tests/assets/jscode2/Csv.csv index e8f44049..f2cc5a60 100644 --- a/tests/assets/jscode2/Csv.csv +++ b/tests/assets/jscode2/Csv.csv @@ -8,6 +8,7 @@ Project-Id-Version: Report-Msgid-Bugs-To: " ,"some message", -"some context","some message in a context", +"some context","some ""message"" in +a context", ,"%s message", ,"my translate 3", diff --git a/tests/assets/jscode2/CsvDictionary.csv b/tests/assets/jscode2/CsvDictionary.csv index 308fbe10..21f041fd 100644 --- a/tests/assets/jscode2/CsvDictionary.csv +++ b/tests/assets/jscode2/CsvDictionary.csv @@ -1,4 +1,5 @@ "some message", -"some message in a context", +"some ""message"" in +a context", "%s message", "my translate 3", diff --git a/tests/assets/jscode2/Jed.json b/tests/assets/jscode2/Jed.json index 95d27b1f..b10c0c70 100644 --- a/tests/assets/jscode2/Jed.json +++ b/tests/assets/jscode2/Jed.json @@ -8,7 +8,7 @@ "some message": [ "" ], - "some context\\u0004some message in a context": [ + "some context\\u0004some \"message\" in \na context": [ "" ], "%s message": [ diff --git a/tests/assets/jscode2/Json.json b/tests/assets/jscode2/Json.json index 5f82a692..a7871ecd 100644 --- a/tests/assets/jscode2/Json.json +++ b/tests/assets/jscode2/Json.json @@ -17,7 +17,7 @@ ] }, "some context": { - "some message in a context": [ + "some \"message\" in \na context": [ "" ] } diff --git a/tests/assets/jscode2/JsonDictionary.json b/tests/assets/jscode2/JsonDictionary.json index e153b772..48709102 100644 --- a/tests/assets/jscode2/JsonDictionary.json +++ b/tests/assets/jscode2/JsonDictionary.json @@ -1,6 +1,6 @@ { "some message": "", - "some message in a context": "", + "some \"message\" in \na context": "", "%s message": "", "my translate 3": "" } \ No newline at end of file diff --git a/tests/assets/jscode2/PhpArray.php b/tests/assets/jscode2/PhpArray.php index 80f60982..0c15bf8c 100644 --- a/tests/assets/jscode2/PhpArray.php +++ b/tests/assets/jscode2/PhpArray.php @@ -32,7 +32,8 @@ ), 'some context' => array ( - 'some message in a context' => + 'some "message" in +a context' => array ( 0 => '', ), diff --git a/tests/assets/jscode2/Po.po b/tests/assets/jscode2/Po.po index a20c0fb0..2d73df0a 100644 --- a/tests/assets/jscode2/Po.po +++ b/tests/assets/jscode2/Po.po @@ -15,7 +15,9 @@ msgstr "" #: ./tests/assets/jscode2/input.js:2 msgctxt "some context" -msgid "some message in a context" +msgid "" +"some \"message\" in \n" +"a context" msgstr "" #: ./tests/assets/jscode2/input.js:3 diff --git a/tests/assets/jscode2/Xliff.xlf b/tests/assets/jscode2/Xliff.xlf index ee4ed99c..42fbde8e 100644 --- a/tests/assets/jscode2/Xliff.xlf +++ b/tests/assets/jscode2/Xliff.xlf @@ -21,13 +21,14 @@ - + some context ./tests/assets/jscode2/input.js:2 - some message in a context + some "message" in +a context diff --git a/tests/assets/jscode2/Yaml.yml b/tests/assets/jscode2/Yaml.yml index e99fc2df..64e7a8e6 100644 --- a/tests/assets/jscode2/Yaml.yml +++ b/tests/assets/jscode2/Yaml.yml @@ -8,4 +8,4 @@ messages: '%s message': '' 'my translate 3': '' 'some context': - 'some message in a context': '' + "some \"message\" in \na context": '' diff --git a/tests/assets/jscode2/YamlDictionary.yml b/tests/assets/jscode2/YamlDictionary.yml index 0dc4c524..11998ce3 100644 --- a/tests/assets/jscode2/YamlDictionary.yml +++ b/tests/assets/jscode2/YamlDictionary.yml @@ -1,4 +1,4 @@ 'some message': '' -'some message in a context': '' +"some \"message\" in \na context": '' '%s message': '' 'my translate 3': '' diff --git a/tests/assets/jscode2/input.js b/tests/assets/jscode2/input.js index 02ca1d44..dcf22303 100644 --- a/tests/assets/jscode2/input.js +++ b/tests/assets/jscode2/input.js @@ -1,5 +1,5 @@ gettext("some message"); -pgettext("some context", "some message in a context"); +pgettext("some context", "some \"message\" in \na context"); ngettext("%s message", "%s messages", 2); (function(){