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 MissingCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'autotrans:missing'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Translates all source translations that are not set in your 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
|
|
|
$missingCount = 0; |
52
|
|
|
$strLen = 0; |
53
|
|
|
foreach ($targetLanguages as $targetLanguage) { |
54
|
|
|
$missing = $this->autoTranslator->getMissingTranslations($targetLanguage); |
55
|
|
|
$missingCount += $missing->count(); |
56
|
|
|
$strLen += $missing->map(function ($value) { |
57
|
|
|
return strlen($value); |
58
|
|
|
})->sum(); |
59
|
|
|
$this->line('Found '.$missing->count().' missing keys in '.$targetLanguage); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($missingCount === 0) { |
63
|
|
|
$this->line('0 missing keys found...aborting'); |
64
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->line($strLen.' characters will be translated'); |
69
|
|
|
|
70
|
|
|
if (! $this->confirm('Continue?', true)) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$bar = $this->output->createProgressBar($missingCount); |
75
|
|
|
$bar->start(); |
76
|
|
|
|
77
|
|
View Code Duplication |
foreach ($targetLanguages as $targetLanguage) { |
|
|
|
|
78
|
|
|
$missing = $this->autoTranslator->getMissingTranslations($targetLanguage); |
79
|
|
|
|
80
|
|
|
$translated = $this->autoTranslator->translate($targetLanguage, $missing, function () use ($bar) { |
81
|
|
|
$bar->advance(); |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
$this->autoTranslator->fillLanguageFiles($targetLanguage, $translated); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$bar->finish(); |
88
|
|
|
|
89
|
|
|
$this->info("\nTranslated ".$missingCount.' missing language keys.'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.