generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05763dc
commit 7343aa4
Showing
5 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Elegantly\Translator\Commands; | ||
|
||
use Elegantly\Translator\Facades\Translator; | ||
use Illuminate\Console\Command; | ||
|
||
class TranslateTranslationsCommand extends Command | ||
{ | ||
public $signature = 'translator:translate {--from=} {--to=} {--all}'; | ||
|
||
public $description = 'Translate translations from the given locale to the target one.'; | ||
|
||
public function handle(): int | ||
{ | ||
$from = (string) $this->argument('from'); | ||
$to = $this->argument('to'); | ||
$all = (bool) $this->argument('all'); | ||
|
||
$namespaces = Translator::getNamespaces($from); | ||
|
||
$targets = collect($to ? [(string) $to] : Translator::getLanguages())->filter(fn ($locale) => $locale !== $from); | ||
|
||
foreach ($targets as $target) { | ||
|
||
$this->info("Translating from {$from} to {$target}:"); | ||
|
||
foreach ($namespaces as $namespace) { | ||
|
||
if ($all) { | ||
$keys = Translator::getTranslations($from, $namespace)->dot()->keys()->toArray(); | ||
} else { | ||
$keys = Translator::getMissingTranslations($from, $target, $namespace); | ||
} | ||
|
||
if (! $this->confirm(count($keys)." found for {$target}.{$namespace}, would you like to continue?")) { | ||
continue; | ||
} | ||
|
||
$translations = Translator::translateTranslations( | ||
$from, | ||
$target, | ||
$namespace, | ||
$keys | ||
); | ||
|
||
$this->table( | ||
headers: ['key', 'Translation'], | ||
rows: $translations->dot()->map(fn ($value, $key) => ["{$namespace}.{$key}", $value]) | ||
); | ||
} | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters