Skip to content

Commit

Permalink
Merge pull request #12 from q--/Ignore_escaped_quotes_when_detecting_…
Browse files Browse the repository at this point in the history
…end_of_string

Ignore escaped quotes when detecting end of string

So, for `__('\'Hello\', World!')` it previously detected the string `'Hello\`; after this fix, it'll properly detect `'Hello', World!`
  • Loading branch information
q-- authored Mar 26, 2024
2 parents d664ade + 09dd34b commit 51eb4fc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function findTranslations()
'('. // Start a new group to match:
'.+'. // Must start with group
')'. // Close group
"[\'\"]". // Closing quote
"(?<!\\\\)[\'\"]". // Closing quote (don't match if escaped with backslash)
"\s*". // Whitespace after param
"[\),]"; // Close parentheses or new parameter

Expand Down
2 changes: 1 addition & 1 deletion tests/ScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function it_finds_all_translations()
$this->scanner = app()->make(Scanner::class);
$matches = $this->scanner->findTranslations();

$this->assertEquals($matches, ['single' => ['single' => ['This will go in the JSON array' => '', 'This will also go in the JSON array' => '', 'This will go in the JSON array, and it\'ll properly unescape the apostrophe.' => '', 'trans' => '']], 'group' => ['lang' => ['first_match' => ''], 'lang_get' => ['first' => '', 'second' => ''], 'trans' => ['first_match' => '', 'third_match' => ''], 'trans_choice' => ['with_params' => '']]]);
$this->assertEquals(['single' => ['single' => ['This will go in the JSON array' => '', 'This will also go in the JSON array' => '', 'This will go in the JSON array, and it\'ll properly unescape the apostrophe.' => '', 'The first half of this sentence should go into the \'JSON array\', but the second part should as well!' => '', 'trans' => '']], 'group' => ['lang' => ['first_match' => ''], 'lang_get' => ['first' => '', 'second' => ''], 'trans' => ['first_match' => '', 'third_match' => ''], 'trans_choice' => ['with_params' => '']]], $matches);
$this->assertCount(2, $matches);
}
}
4 changes: 3 additions & 1 deletion tests/fixtures/scan-tests/__.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ __(
'This will also go in the JSON array'
)

__('This will go in the JSON array, and it\'ll properly unescape the apostrophe.')
__('This will go in the JSON array, and it\'ll properly unescape the apostrophe.')

__('The first half of this sentence should go into the \'JSON array\', but the second part should as well!')

0 comments on commit 51eb4fc

Please sign in to comment.