diff --git a/src/AutoTranslate.php b/src/AutoTranslate.php index 6b9c3e2..df5b31a 100644 --- a/src/AutoTranslate.php +++ b/src/AutoTranslate.php @@ -84,7 +84,7 @@ public function fillLanguageFiles(string $language, array $data) public function array_undot(array $dottedArray, array $initialArray = []) : array { foreach ($dottedArray as $key => $value) { - array_set($initialArray, $key, $value); + Arr::set($initialArray, $key, $value); } return $initialArray; diff --git a/src/Commands/AllCommand.php b/src/Commands/AllCommand.php index 687d340..c26c5b1 100644 --- a/src/Commands/AllCommand.php +++ b/src/Commands/AllCommand.php @@ -44,14 +44,23 @@ public function handle() { $targetLanguages = Arr::wrap(config('auto-translate.target_language')); + $this->line('Found '.count($targetLanguages).' languages to translate'); + + $bar = $this->output->createProgressBar(count($targetLanguages)); + $bar->start(); + foreach ($targetLanguages as $targetLanguage) { $sourceTranslations = $this->autoTranslator->getSourceTranslations(); $translated = $this->autoTranslator->translate($targetLanguage, $sourceTranslations); $this->autoTranslator->fillLanguageFiles($targetLanguage, $translated); + + $bar->advance(); } + $bar->finish(); + $this->info('Translated '.count(Arr::dot($sourceTranslations)).' language keys.'); } } diff --git a/src/Commands/MissingCommand.php b/src/Commands/MissingCommand.php index 57bcba4..1455786 100644 --- a/src/Commands/MissingCommand.php +++ b/src/Commands/MissingCommand.php @@ -44,6 +44,11 @@ public function handle() { $targetLanguages = Arr::wrap(config('auto-translate.target_language')); + $this->line('Found '.count($targetLanguages).' languages to translate'); + + $bar = $this->output->createProgressBar(count($targetLanguages)); + $bar->start(); + $missingCount = 0; foreach ($targetLanguages as $targetLanguage) { @@ -53,8 +58,12 @@ public function handle() $translated = $this->autoTranslator->translate($targetLanguage, $missing); $this->autoTranslator->fillLanguageFiles($targetLanguage, $translated); + + $bar->advance(); } + $bar->finish(); + $this->info('Translated '.$missingCount.' missing language keys.'); } } diff --git a/src/Exceptions/LanguageCodeNotExist.php b/src/Exceptions/LanguageCodeNotExist.php new file mode 100644 index 0000000..58c6e79 --- /dev/null +++ b/src/Exceptions/LanguageCodeNotExist.php @@ -0,0 +1,13 @@ +source = $source; + $this->translator->setSource($source); return $this; @@ -22,6 +27,8 @@ public function setSource(string $source) public function setTarget(string $target) { + $this->target = $target; + $this->translator->setTarget($target); return $this; @@ -29,6 +36,14 @@ public function setTarget(string $target) public function translate(string $string) : string { - return $this->translator->translate($string); + try { + sleep(random_int(1, 3)); + + return $this->translator->translate($string); + } catch (\Throwable $th) { + if ($th->getMessage() === 'Return value of Stichoza\GoogleTranslate\GoogleTranslate::translate() must be of the type string, null returned') { + throw LanguageCodeNotExist::throw($this->source, $this->target); + } + } } } diff --git a/tests/CommandTest.php b/tests/CommandTest.php deleted file mode 100644 index 20f669a..0000000 --- a/tests/CommandTest.php +++ /dev/null @@ -1,59 +0,0 @@ -createTempFiles([ - 'en' => [ - 'user' => " 'Name', 'age' => 'Age'];", - ], - ]); - - try { - (new AllCommand(app('auto-translate')))->handle(); - } catch (\Throwable $th) { - } - - $translations = AutoTranslateFacade::getTranslations('de'); - - $this->assertEquals([ - 'user' => [ - 'name' => 'Name', - 'age' => 'Alter', - ], - ], $translations); - } - - public function test_MissingCommand() - { - $this->createTempFiles([ - 'en' => [ - 'user' => " 'Name', 'age' => 'Age'];", - ], - 'de' => [ - 'user' => " 'Name'];", - ], - ]); - - try { - (new MissingCommand(app('auto-translate')))->handle(); - } catch (\Throwable $th) { - } - - $translations = AutoTranslateFacade::getTranslations('de'); - - $this->assertEquals([ - 'user' => [ - 'name' => 'Name', - 'age' => 'Alter', - ], - ], $translations); - } -}