From 632059b5d7e4fc5d30f393d81f90bb6f00139294 Mon Sep 17 00:00:00 2001 From: Amir Rami Date: Tue, 18 May 2021 09:09:31 +0200 Subject: [PATCH] Strip slashes from translation keys (#17) Co-authored-by: Amir --- src/Services/Parser.php | 3 +++ tests/LocalizatorTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Services/Parser.php b/src/Services/Parser.php index 07f0fc8..75ab2a4 100644 --- a/src/Services/Parser.php +++ b/src/Services/Parser.php @@ -51,6 +51,9 @@ public function parseKeys(Collection $files): void return $this->getStrings($file); }) ->flatten() + ->map(function (string $string) { + return stripslashes($string); + }) ->each(function (string $string) { if ($this->isDotKey($string)) { $this->defaultKeys->push($string); diff --git a/tests/LocalizatorTest.php b/tests/LocalizatorTest.php index 7ebe427..21e96e3 100644 --- a/tests/LocalizatorTest.php +++ b/tests/LocalizatorTest.php @@ -141,4 +141,32 @@ public function testLocalizeCommandByMergingTheExistingTranslations(): void // Cleanup. self::flushDirectories('lang', 'views'); } + + /** + * @return void + */ + public function testLocalizeCommandWhereKeysAreEscapedWithSlashes(): void + { + $this->createTestView("{{ __('Amir\'s PC') }} {{ __('Jacob\'s Ladder') }} {{ __('mom\'s spaghetti') }}"); + + // Run localize command. + $this->artisan('localize') + ->assertExitCode(0); + + // Do created locale files exist? + self::assertJsonLangFilesExist('en'); + + // Do their contents match the expected results? + $enJsonContents = $this->getJsonLangContents('en'); + + // Did it sort the translation keys like we expected? + self::assertSame([ + 'Amir\'s PC' => 'Amir\'s PC', + 'Jacob\'s Ladder' => 'Jacob\'s Ladder', + 'mom\'s spaghetti' => 'mom\'s spaghetti', + ], $enJsonContents); + + // Cleanup. + self::flushDirectories('lang', 'views'); + } }