Skip to content

Commit

Permalink
fixed mulitiline extractions in jsCode #200
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Mar 26, 2019
1 parent 93176b2 commit dc92f65
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 14 deletions.
55 changes: 54 additions & 1 deletion src/Utils/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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
);
}
}
1 change: 0 additions & 1 deletion tests/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion tests/assets/jscode2/Csv.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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",
3 changes: 2 additions & 1 deletion tests/assets/jscode2/CsvDictionary.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"some message",
"some message in a context",
"some ""message"" in
a context",
"%s message",
"my translate 3",
2 changes: 1 addition & 1 deletion tests/assets/jscode2/Jed.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"some message": [
""
],
"some context\\u0004some message in a context": [
"some context\\u0004some \"message\" in \na context": [
""
],
"%s message": [
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/jscode2/Json.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
]
},
"some context": {
"some message in a context": [
"some \"message\" in \na context": [
""
]
}
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/jscode2/JsonDictionary.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"some message": "",
"some message in a context": "",
"some \"message\" in \na context": "",
"%s message": "",
"my translate 3": ""
}
3 changes: 2 additions & 1 deletion tests/assets/jscode2/PhpArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
),
'some context' =>
array (
'some message in a context' =>
'some "message" in
a context' =>
array (
0 => '',
),
Expand Down
4 changes: 3 additions & 1 deletion tests/assets/jscode2/Po.po
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions tests/assets/jscode2/Xliff.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
<target></target>
</segment>
</unit>
<unit id="79662f3fd7a86c2d14a8a9a707609a21">
<unit id="c3283d194287d65de0f6978057ce535a">
<notes>
<note category="context">some context</note>
<note category="reference">./tests/assets/jscode2/input.js:2</note>
</notes>
<segment>
<source>some message in a context</source>
<source>some "message" in
a context</source>
<target></target>
</segment>
</unit>
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/jscode2/Yaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ messages:
'%s message': ''
'my translate 3': ''
'some context':
'some message in a context': ''
"some \"message\" in \na context": ''
2 changes: 1 addition & 1 deletion tests/assets/jscode2/YamlDictionary.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'some message': ''
'some message in a context': ''
"some \"message\" in \na context": ''
'%s message': ''
'my translate 3': ''
2 changes: 1 addition & 1 deletion tests/assets/jscode2/input.js
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down

0 comments on commit dc92f65

Please sign in to comment.