From 460f4fff15f12cced89387e236f087659b6efbc8 Mon Sep 17 00:00:00 2001 From: khalyomede Date: Tue, 19 Sep 2023 23:21:21 +0200 Subject: [PATCH] #49 fix texts starting with "New" not translated --- CHANGELOG.md | 1 + src/Commands/Translate.php | 5 ++- tests/Feature/Commands/TranslateTest.php | 31 +++++++++++++++++-- .../resources/views/about-us/index.blade.php | 1 + 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 tests/misc/resources/views/about-us/index.blade.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 85626c6..582be19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - The command will not hang a few seconds without showing progress anymore ([#46](https://github.com/khalyomede/laravel-translate/issues/46)). +- Texts starting with "New" will now be correctly translated ([#49](https://github.com/khalyomede/laravel-translate/issues/49)). ## [0.1.0] - 2023-03-25 diff --git a/src/Commands/Translate.php b/src/Commands/Translate.php index 39b2996..392e22f 100644 --- a/src/Commands/Translate.php +++ b/src/Commands/Translate.php @@ -7,11 +7,13 @@ use Illuminate\Console\Command; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; +use Illuminate\Support\Str; use InvalidArgumentException; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\New_; use PhpParser\Node\Name; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Expression; @@ -477,7 +479,8 @@ private function filterTranslationKeys(ProgressBar $bar, Collection $translation return false; } - if ($expression->expr instanceof ConstFetch) { + // instanceof New -> "New account", "New" is parsed as new class keyword + if ($expression->expr instanceof ConstFetch || $expression->expr instanceof New_) { $bar->advance(); return true; diff --git a/tests/Feature/Commands/TranslateTest.php b/tests/Feature/Commands/TranslateTest.php index 598dc72..f02f6e7 100644 --- a/tests/Feature/Commands/TranslateTest.php +++ b/tests/Feature/Commands/TranslateTest.php @@ -88,6 +88,7 @@ public function testReturnsZeroCodeIfNoNewKeysHaveBeenFound(): void "test 2" => "", "test 3" => "", "Unable to perform anti-bot validation." => "", + "New journey" => "", ], flags: JSON_PRETTY_PRINT); assert(is_string($content)); @@ -598,7 +599,7 @@ public function testDisplaysHowManyNewKeysHaveBeenAddedToFile(): void $this->artisan(Translate::class) ->assertSuccessful() - ->expectsOutputToContain("Added 21 new key(s) on each lang files."); + ->expectsOutputToContain("Added 22 new key(s) on each lang files."); } public function testDisplayOnlyNewKeysAddedToFileWithoutTakingIntoAccountCurrentExistingKeys(): void @@ -626,6 +627,7 @@ public function testDisplayOnlyNewKeysAddedToFileWithoutTakingIntoAccountCurrent "test" => "", "test 2" => "", "test 3" => "", + "New journey" => "", ], flags: JSON_PRETTY_PRINT); assert(is_string($content)); @@ -672,7 +674,7 @@ public function testDisplayNumberOfKeysInConditionalIfNewKeysHaveBeenFoundWhenUs assert($command instanceof PendingCommand); - $command->expectsOutputToContain("21 key(s) would have been added (using --dry-run) on each lang files."); + $command->expectsOutputToContain("22 key(s) would have been added (using --dry-run) on each lang files."); } public function testDoesNotAddShortKeys(): void @@ -915,7 +917,7 @@ public function testDisplaysProgressBarWhenFetchingTranslationKeys(): void $this->artisan(Translate::class) ->assertSuccessful() - ->expectsOutputToContain("12/12"); + ->expectsOutputToContain("13/13"); } public function testDisplaysElapsedTimeAndMaxMemoryConsumption(): void @@ -965,4 +967,27 @@ public function testCanAddKeysFromTranslatables(): void "Yellow" => "", ]); } + + public function testCanTranslateTextStartingWithNew(): void + { + $this->app?->useLangPath(__DIR__ . "/../../misc/resources/lang"); + + config([ + "translate" => [ + "langs" => [ + "fr", + ], + "include" => [ + "tests/misc/resources/views/about-us" + ], + ], + ]); + + $this->artisan(Translate::class) + ->assertSuccessful(); + + $this->assertFileContainsJson(__DIR__ . "/../../misc/resources/lang/fr.json", [ + "New journey" => "", + ]); + } } diff --git a/tests/misc/resources/views/about-us/index.blade.php b/tests/misc/resources/views/about-us/index.blade.php new file mode 100644 index 0000000..0e2a9ae --- /dev/null +++ b/tests/misc/resources/views/about-us/index.blade.php @@ -0,0 +1 @@ +

@lang('New journey')