Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode PHP strings to prevent slashes in output #296

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.')