|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Elegantly\Translator\Commands; |
| 4 | + |
| 5 | +use Elegantly\Translator\Facades\Translator; |
| 6 | +use Elegantly\Translator\Services\Grammar\GrammarServiceInterface; |
| 7 | +use Elegantly\Translator\Services\Translate\TranslateServiceInterface; |
| 8 | +use Elegantly\Translator\TranslatorServiceProvider; |
| 9 | + |
| 10 | +use function Laravel\Prompts\multiselect; |
| 11 | +use function Laravel\Prompts\select; |
| 12 | + |
| 13 | +trait TranslatorCommandTrait |
| 14 | +{ |
| 15 | + public function getLocales( |
| 16 | + mixed $option, |
| 17 | + ?string $label = null |
| 18 | + ): array { |
| 19 | + $availableLocales = Translator::getLanguages(); |
| 20 | + |
| 21 | + if ($option) { |
| 22 | + if (is_array($option)) { |
| 23 | + return array_intersect($availableLocales, $option); |
| 24 | + } |
| 25 | + if (is_string($option)) { |
| 26 | + return array_intersect($availableLocales, explode(',', $option)); |
| 27 | + } |
| 28 | + |
| 29 | + return $availableLocales; |
| 30 | + } |
| 31 | + |
| 32 | + return multiselect( |
| 33 | + label: $label ?? 'In what locales?', |
| 34 | + options: $availableLocales, |
| 35 | + default: $availableLocales, |
| 36 | + required: true, |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + public function getTranslateService( |
| 41 | + mixed $option, |
| 42 | + ?string $label = null, |
| 43 | + ): TranslateServiceInterface { |
| 44 | + |
| 45 | + if ($option && is_string($option)) { |
| 46 | + return TranslatorServiceProvider::getTranslateServiceFromConfig($option); |
| 47 | + } |
| 48 | + |
| 49 | + $serviceName = select( |
| 50 | + label: $label ?? 'What service would you like to use?', |
| 51 | + options: array_keys(config('translator.translate.services')), |
| 52 | + default: config('translator.translate.service'), |
| 53 | + required: true, |
| 54 | + ); |
| 55 | + |
| 56 | + return TranslatorServiceProvider::getTranslateServiceFromConfig($serviceName); |
| 57 | + } |
| 58 | + |
| 59 | + public function getGrammarService( |
| 60 | + mixed $option, |
| 61 | + ?string $label = null, |
| 62 | + ): GrammarServiceInterface { |
| 63 | + |
| 64 | + if ($option && is_string($option)) { |
| 65 | + return TranslatorServiceProvider::getGrammarServiceFromConfig($option); |
| 66 | + } |
| 67 | + |
| 68 | + $serviceName = select( |
| 69 | + label: $label ?? 'What service would you like to use?', |
| 70 | + options: array_keys(config('translator.grammar.services')), |
| 71 | + default: config('translator.grammar.service'), |
| 72 | + required: true, |
| 73 | + ); |
| 74 | + |
| 75 | + return TranslatorServiceProvider::getGrammarServiceFromConfig($serviceName); |
| 76 | + } |
| 77 | +} |
0 commit comments