Skip to content

Commit bc3b158

Browse files
committed
fix command prompting
1 parent c9300aa commit bc3b158

File tree

4 files changed

+110
-58
lines changed

4 files changed

+110
-58
lines changed

src/Commands/FixGrammarTranslationsCommand.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Illuminate\Console\Command;
88
use Illuminate\Contracts\Console\PromptsForMissingInput;
99
use Laravel\Prompts\Progress;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
1012

1113
use function Laravel\Prompts\multiselect;
1214
use function Laravel\Prompts\progress;
@@ -63,24 +65,34 @@ public function promptForMissingArgumentsUsing()
6365
required: true,
6466
);
6567
},
66-
'service' => function () {
67-
return select(
68-
label: 'What service would you like to use?',
69-
options: array_keys(config('translator.grammar.services')),
70-
default: config('translator.grammar.service'),
71-
required: true,
72-
);
73-
},
74-
'namespaces' => function () {
75-
$options = Translator::getNamespaces($this->argument('locale'));
7668

77-
return multiselect(
78-
label: 'What namespaces would you like to fix?',
79-
options: $options,
80-
default: $options,
81-
required: true,
82-
);
83-
},
8469
];
8570
}
71+
72+
function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
73+
{
74+
if ($this->didReceiveOptions($input)) {
75+
return;
76+
}
77+
78+
if (empty($input->getOption('namespaces'))) {
79+
$options = Translator::getNamespaces($this->argument('locale'));
80+
81+
$input->setOption('namespaces', multiselect(
82+
label: 'What namespaces would you like to fix?',
83+
options: $options,
84+
default: $options,
85+
required: true,
86+
));
87+
}
88+
89+
if ($input->getOption('service') === null) {
90+
$input->setOption('service', select(
91+
label: 'What service would you like to use?',
92+
options: array_keys(config('translator.grammar.services')),
93+
default: config('translator.grammar.service'),
94+
required: true,
95+
));
96+
}
97+
}
8698
}

src/Commands/ShowMissingTranslationsCommand.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
use Elegantly\Translator\Facades\Translator;
66
use Illuminate\Console\Command;
7+
use Illuminate\Contracts\Console\PromptsForMissingInput;
78
use Symfony\Component\Console\Helper\TableSeparator;
89

9-
class ShowMissingTranslationsCommand extends Command
10+
class ShowMissingTranslationsCommand extends Command implements PromptsForMissingInput
1011
{
11-
public $signature = 'translator:missing {locale}';
12+
public $signature = 'translator:missing {locale : The locale of reference}';
1213

13-
public $description = "Show all missing translations taking present in 'locale' but not in the others languages.";
14+
public $description = "Show all missing translations present in the locale of reference but not in the others languages.";
1415

1516
public function handle(): int
1617
{
17-
$reference = (string) $this->argument('locale');
18+
$reference = $this->argument('locale');
1819

1920
$rows = collect(Translator::getAllMissingTranslations($reference))
2021
->flatMap(
@@ -37,4 +38,19 @@ public function handle(): int
3738

3839
return self::SUCCESS;
3940
}
41+
42+
public function promptForMissingArgumentsUsing()
43+
{
44+
return [
45+
'locale' => function () {
46+
return select(
47+
label: 'What is the locale of reference?',
48+
options: Translator::getLocales(),
49+
default: config('app.locale'),
50+
required: true,
51+
);
52+
},
53+
54+
];
55+
}
4056
}

src/Commands/SortAllTranslationsCommand.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Elegantly\Translator\Facades\Translator;
66
use Illuminate\Console\Command;
77
use Illuminate\Contracts\Console\PromptsForMissingInput;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
810

911
use function Laravel\Prompts\multiselect;
1012
use function Laravel\Prompts\progress;
@@ -52,19 +54,29 @@ public function promptForMissingArgumentsUsing()
5254
required: true,
5355
);
5456
},
55-
'namespaces' => function () {
56-
$options = collect($this->argument('locales'))
57-
->flatMap(fn (string $locale) => Translator::getNamespaces($locale))
58-
->unique()
59-
->toArray();
60-
61-
return multiselect(
62-
label: 'What namespaces would you like to sort?',
63-
options: $options,
64-
default: $options,
65-
required: true,
66-
);
67-
},
6857
];
6958
}
59+
60+
function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
61+
{
62+
if ($this->didReceiveOptions($input)) {
63+
return;
64+
}
65+
66+
if (empty($input->getOption('namespaces'))) {
67+
68+
$options = collect($input->getArgument('locales'))
69+
->flatMap(fn (string $locale) => Translator::getNamespaces($locale))
70+
->unique()
71+
->toArray();
72+
73+
74+
$input->setOption('namespaces', multiselect(
75+
label: 'What namespaces would you like to sort?',
76+
options: $options,
77+
default: $options,
78+
required: true,
79+
));
80+
}
81+
}
7082
}

src/Commands/TranslateTranslationsCommand.php

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Elegantly\Translator\TranslatorServiceProvider;
77
use Illuminate\Console\Command;
88
use Illuminate\Contracts\Console\PromptsForMissingInput;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
911

1012
use function Laravel\Prompts\confirm;
1113
use function Laravel\Prompts\multiselect;
@@ -83,30 +85,40 @@ public function promptForMissingArgumentsUsing()
8385
required: true,
8486
);
8587
},
86-
'service' => function () {
87-
return select(
88-
label: 'What service would you like to use?',
89-
options: array_keys(config('translator.translate.services')),
90-
default: config('translator.translate.service'),
91-
required: true,
92-
);
93-
},
94-
'all' => function () {
95-
return confirm(
96-
label: 'Only translate missing keys?',
97-
no: 'No, translate all keys'
98-
);
99-
},
100-
'namespaces' => function () {
101-
$options = Translator::getNamespaces($this->argument('from'));
102-
103-
return multiselect(
104-
label: 'What namespaces would you like to translate?',
105-
options: $options,
106-
default: $options,
107-
required: true,
108-
);
109-
},
11088
];
11189
}
90+
91+
function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
92+
{
93+
if ($this->didReceiveOptions($input)) {
94+
return;
95+
}
96+
97+
if (empty($input->getOption('namespaces'))) {
98+
$options = Translator::getNamespaces($input->getArgument('from'));
99+
100+
$input->setOption('namespaces', multiselect(
101+
label: 'What namespaces would you like to translate?',
102+
options: $options,
103+
default: $options,
104+
required: true,
105+
));
106+
}
107+
108+
if ($input->getOption('service') === null) {
109+
$input->setOption('service', select(
110+
label: 'What service would you like to use?',
111+
options: array_keys(config('translator.translate.services')),
112+
default: config('translator.translate.service'),
113+
required: true,
114+
));
115+
}
116+
117+
if ($input->getOption('all') === false) {
118+
$input->setOption('all', !confirm(
119+
label: 'Only translate missing keys?',
120+
no: 'No, translate all keys'
121+
));
122+
}
123+
}
112124
}

0 commit comments

Comments
 (0)