Skip to content

Commit

Permalink
Decode PHP strings to prevent slashes in output
Browse files Browse the repository at this point in the history
Decode PHP strings, so in the final JSON e.g. __('Don\'t') becomes
"Don't" instead of "Don\\'t"
  • Loading branch information
q-- committed Feb 27, 2024
1 parent feba4d1 commit 4a0447c
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 4a0447c

Please sign in to comment.