1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ben182\AutoTranslate\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Ben182\AutoTranslate\AutoTranslate; |
9
|
|
|
|
10
|
|
|
class AllCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'autotrans:all'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Translates all source translations to target translations'; |
25
|
|
|
|
26
|
|
|
protected $autoTranslator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a new command instance. |
30
|
|
|
* |
31
|
|
|
* @return void |
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
public function __construct(AutoTranslate $autoTranslator) |
34
|
|
|
{ |
35
|
|
|
parent::__construct(); |
36
|
|
|
$this->autoTranslator = $autoTranslator; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Execute the console command. |
41
|
|
|
* |
42
|
|
|
* @return mixed |
43
|
|
|
*/ |
44
|
|
|
public function handle() |
45
|
|
|
{ |
46
|
|
|
$targetLanguages = Arr::wrap(config('auto-translate.target_language')); |
47
|
|
|
|
48
|
|
|
$foundLanguages = count($targetLanguages); |
49
|
|
|
$this->line('Found '.$foundLanguages.' '.Str::plural('language', $foundLanguages).' to translate'); |
50
|
|
|
|
51
|
|
|
$availableTranslations = 0; |
|
|
|
|
52
|
|
|
$sourceTranslations = $this->autoTranslator->getSourceTranslations(); |
53
|
|
|
$availableTranslations = count($dottedTranslations = Arr::dot($sourceTranslations)) * count($targetLanguages); |
54
|
|
|
|
55
|
|
|
if (empty($dottedTranslations)) { |
56
|
|
|
$this->line('0 keys found...aborting'); |
57
|
|
|
|
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$strLen = collect($dottedTranslations)->map(function ($value) { |
62
|
|
|
return strlen($value); |
63
|
|
|
})->sum() * count($targetLanguages); |
64
|
|
|
|
65
|
|
|
$this->line($strLen.' characters will be translated'); |
66
|
|
|
|
67
|
|
|
if (! $this->confirm('Continue?', true)) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$bar = $this->output->createProgressBar($availableTranslations); |
72
|
|
|
$bar->start(); |
73
|
|
|
|
74
|
|
View Code Duplication |
foreach ($targetLanguages as $targetLanguage) { |
|
|
|
|
75
|
|
|
$dottedSource = Arr::dot($sourceTranslations); |
76
|
|
|
|
77
|
|
|
$translated = $this->autoTranslator->translate($targetLanguage, $dottedSource, function () use ($bar) { |
78
|
|
|
$bar->advance(); |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
$this->autoTranslator->fillLanguageFiles($targetLanguage, $translated); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$bar->finish(); |
85
|
|
|
|
86
|
|
|
$this->info("\nTranslated ".$availableTranslations.' language keys.'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.