Skip to content

Commit

Permalink
use prompt in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jun 20, 2024
1 parent c0a6c9f commit c809521
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 21 deletions.
18 changes: 8 additions & 10 deletions src/Commands/FixGrammarTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@
namespace Elegantly\Translator\Commands;

use Elegantly\Translator\Facades\Translator;
use Elegantly\Translator\TranslatorServiceProvider;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class FixGrammarTranslationsCommand extends Command
{
use TranslatorCommandTrait;

public $signature = 'translator:grammar {--locales=} {--service=} ';

public $description = 'Translate translations from the given locale to the target one.';

public function handle(): int
{
$serviceArg = $this->option('service');
$localesArg = $this->option('locales');

$service = TranslatorServiceProvider::getGrammarServiceFromConfig($serviceArg);

$locales = collect(Translator::getLanguages())
->when($localesArg, fn (Collection $items) => $items->intersect(explode(',', $localesArg)));
$service = $this->getGrammarService($this->option('service'));
$locales = $this->getLocales(
option: $this->option('locales'),
label: 'In what locales would you like to translate?'
);

foreach ($locales as $locale) {

$this->info("Fixing grammar in '{$locale}' locale:");
$this->info("Fixing grammar in '/{$locale}':");
$this->line('Using service :'.get_class($service));

$namespaces = Translator::getNamespaces($locale);
Expand Down
12 changes: 8 additions & 4 deletions src/Commands/SortAllTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

class SortAllTranslationsCommand extends Command
{
use TranslatorCommandTrait;

public $signature = 'translator:sort {--locales=} {--namespaces=}';

public $description = 'Sort all translations using natural order';

public function handle(): int
{
$namespacesArg = $this->option('namespaces');
$localesArg = $this->option('locales');

$locales = collect(Translator::getLanguages())
->when($localesArg, fn (Collection $items) => $items->intersect(explode(',', $localesArg)));
$locales = $this->getLocales(
option: $this->option('locales'),
label: 'What locales would you like to sort?'
);

$namespacesArg = $this->option('namespaces');

foreach ($locales as $locale) {
$this->newLine();
Expand Down
15 changes: 8 additions & 7 deletions src/Commands/TranslateTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@
namespace Elegantly\Translator\Commands;

use Elegantly\Translator\Facades\Translator;
use Elegantly\Translator\TranslatorServiceProvider;
use Illuminate\Console\Command;

class TranslateTranslationsCommand extends Command
{
use TranslatorCommandTrait;

public $signature = 'translator:translate {--from=} {--to=} {--service=} {--all} ';

public $description = 'Translate translations from the given locale to the target one.';

public function handle(): int
{
$from = (string) $this->option('from');
$to = $this->option('to');
$all = (bool) $this->option('all');
$serviceName = $this->option('service');
$from = (string) $this->option('from');

$service = TranslatorServiceProvider::getTranslateServiceFromConfig($serviceName);
$service = $this->getTranslateService($this->option('service'));
$targets = $this->getLocales(
option: $this->option('to'),
label: 'In what locales would you like to translate?'
);

$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}':");
Expand Down
77 changes: 77 additions & 0 deletions src/Commands/TranslatorCommandTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Elegantly\Translator\Commands;

use Elegantly\Translator\Facades\Translator;
use Elegantly\Translator\Services\Grammar\GrammarServiceInterface;
use Elegantly\Translator\Services\Translate\TranslateServiceInterface;
use Elegantly\Translator\TranslatorServiceProvider;

use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\select;

trait TranslatorCommandTrait
{
public function getLocales(
mixed $option,
?string $label = null
): array {
$availableLocales = Translator::getLanguages();

if ($option) {
if (is_array($option)) {
return array_intersect($availableLocales, $option);
}
if (is_string($option)) {
return array_intersect($availableLocales, explode(',', $option));
}

return $availableLocales;
}

return multiselect(
label: $label ?? 'In what locales?',
options: $availableLocales,
default: $availableLocales,
required: true,
);
}

public function getTranslateService(
mixed $option,
?string $label = null,
): TranslateServiceInterface {

if ($option && is_string($option)) {
return TranslatorServiceProvider::getTranslateServiceFromConfig($option);
}

$serviceName = select(
label: $label ?? 'What service would you like to use?',
options: array_keys(config('translator.translate.services')),
default: config('translator.translate.service'),
required: true,
);

return TranslatorServiceProvider::getTranslateServiceFromConfig($serviceName);
}

public function getGrammarService(
mixed $option,
?string $label = null,
): GrammarServiceInterface {

if ($option && is_string($option)) {
return TranslatorServiceProvider::getGrammarServiceFromConfig($option);
}

$serviceName = select(
label: $label ?? 'What service would you like to use?',
options: array_keys(config('translator.grammar.services')),
default: config('translator.grammar.service'),
required: true,
);

return TranslatorServiceProvider::getGrammarServiceFromConfig($serviceName);
}
}
2 changes: 2 additions & 0 deletions src/TranslatorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elegantly\Translator;

use Elegantly\Translator\Commands\FixGrammarTranslationsCommand;
use Elegantly\Translator\Commands\ShowMissingTranslationsCommand;
use Elegantly\Translator\Commands\SortAllTranslationsCommand;
use Elegantly\Translator\Commands\TranslateTranslationsCommand;
Expand Down Expand Up @@ -30,6 +31,7 @@ public function configurePackage(Package $package): void
ShowMissingTranslationsCommand::class,
SortAllTranslationsCommand::class,
TranslateTranslationsCommand::class,
FixGrammarTranslationsCommand::class,
]);
}

Expand Down

0 comments on commit c809521

Please sign in to comment.