Skip to content

Commit

Permalink
Merge pull request #1 from q--/Handle_single_quotes_in_PHP_strings_pr…
Browse files Browse the repository at this point in the history
…operly_cherry_pick_for_upstream

Decode PHP strings to prevent slashes in output
  • Loading branch information
q-- authored Feb 27, 2024
2 parents feba4d1 + 4a0447c commit f680759
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function findTranslations()
foreach ($this->disk->allFiles($this->scanPaths) as $file) {
if (preg_match_all("/$matchingPattern/siU", $file->getContents(), $matches)) {
foreach ($matches[2] as $key) {
// Decode php strings, so in the final JSON e.g. __('Don\'t') becomes "Don't" instead of "Don\\'t"
$key = stripcslashes($key);
if (preg_match("/(^[a-zA-Z0-9:_-]+([.][^\1)\ ]+)+$)/siU", $key, $arrayMatches)) {
[$file, $k] = explode('.', $arrayMatches[0], 2);
$results['group'][$file][$k] = '';
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' => '', 'trans' => '']], 'group' => ['lang' => ['first_match' => ''], 'lang_get' => ['first' => '', 'second' => ''], 'trans' => ['first_match' => '', 'third_match' => ''], 'trans_choice' => ['with_params' => '']]]);
$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->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 @@ -3,4 +3,6 @@ __('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.')

0 comments on commit f680759

Please sign in to comment.