Skip to content

Commit

Permalink
#49 fix texts starting with "New" not translated (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
khalyomede authored Sep 19, 2023
1 parent 2dcba64 commit d081740
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/Commands/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 28 additions & 3 deletions tests/Feature/Commands/TranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -626,6 +627,7 @@ public function testDisplayOnlyNewKeysAddedToFileWithoutTakingIntoAccountCurrent
"test" => "",
"test 2" => "",
"test 3" => "",
"New journey" => "",
], flags: JSON_PRETTY_PRINT);

assert(is_string($content));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -915,7 +917,7 @@ public function testDisplaysProgressBarWhenFetchingTranslationKeys(): void

$this->artisan(Translate::class)
->assertSuccessful()
->expectsOutputToContain("12/12");
->expectsOutputToContain("13/13");
}

public function testDisplaysElapsedTimeAndMaxMemoryConsumption(): void
Expand Down Expand Up @@ -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" => "",
]);
}
}
1 change: 1 addition & 0 deletions tests/misc/resources/views/about-us/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>@lang('New journey')</h1>

0 comments on commit d081740

Please sign in to comment.