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\AutoTranslators\JsonTranslator; |
9
|
|
|
use Ben182\AutoTranslate\AutoTranslators\PhpTranslator; |
10
|
|
|
|
11
|
|
|
class MissingCommand extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name and signature of the console command. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'autotrans:missing'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Translates all source translations that are not set in your target translations'; |
26
|
|
|
|
27
|
|
|
protected $phpTranslator; |
28
|
|
|
protected $jsonTranslator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new command instance. |
32
|
|
|
* |
33
|
|
|
* @return void |
|
|
|
|
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function __construct(PhpTranslator $phpTranslator, JsonTranslator $jsonTranslator) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
parent::__construct(); |
38
|
|
|
$this->phpTranslator = $phpTranslator; |
39
|
|
|
$this->jsonTranslator = $jsonTranslator; |
40
|
|
|
$this->targetLanguages = Arr::wrap(config('auto-translate.target_language')); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute the console command. |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function handle() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$numTargetLanguages = count($this->targetLanguages); |
51
|
|
|
|
52
|
|
|
$this->line(implode(' ', [ |
53
|
|
|
'Found', |
54
|
|
|
$numTargetLanguages, |
55
|
|
|
Str::plural('language', $numTargetLanguages), |
56
|
|
|
'to translate', |
57
|
|
|
])); |
58
|
|
|
|
59
|
|
|
$this->translatePhpFiles(); |
60
|
|
|
$this->translateJsonFiles(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function translatePhpFiles() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$this->line('Translating PHP files'); |
66
|
|
|
|
67
|
|
|
$missingCount = 0; |
68
|
|
|
foreach ($this->targetLanguages as $targetLanguage) { |
69
|
|
|
$missingTranslations = $this->phpTranslator->getMissingTranslations($targetLanguage); |
70
|
|
|
$missingCount += count($missingTranslations); |
71
|
|
|
|
72
|
|
|
$this->line(implode(' ', [ |
73
|
|
|
'Found', |
74
|
|
|
count($missingTranslations), |
75
|
|
|
'missing keys in', |
76
|
|
|
$targetLanguage, |
77
|
|
|
])); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$bar = $this->output->createProgressBar($missingCount); |
81
|
|
|
$bar->start(); |
82
|
|
|
|
83
|
|
|
$this->phpTranslator->translateMissing($this->targetLanguages, function () use ($bar) { |
84
|
|
|
$bar->advance(); |
85
|
|
|
}); |
86
|
|
|
|
87
|
|
|
$bar->finish(); |
88
|
|
|
|
89
|
|
|
$this->info("\nTranslated ".$missingCount.' missing language keys.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
public function translateJsonFiles() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$this->line('Translating JSON files'); |
95
|
|
|
|
96
|
|
|
$missingCount = 0; |
97
|
|
|
foreach ($this->targetLanguages as $targetLanguage) { |
98
|
|
|
$missingTranslations = $this->jsonTranslator->getMissingTranslations($targetLanguage); |
99
|
|
|
$missingCount += count($missingTranslations); |
100
|
|
|
|
101
|
|
|
$this->line(implode(' ', [ |
102
|
|
|
'Found', |
103
|
|
|
count($missingTranslations), |
104
|
|
|
'missing keys in', |
105
|
|
|
$targetLanguage, |
106
|
|
|
])); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$bar = $this->output->createProgressBar($missingCount); |
110
|
|
|
$bar->start(); |
111
|
|
|
|
112
|
|
|
$this->jsonTranslator->translateMissing($this->targetLanguages, function () use ($bar) { |
113
|
|
|
$bar->advance(); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
$bar->finish(); |
117
|
|
|
|
118
|
|
|
$this->info("\nTranslated ".$missingCount.' missing language keys.'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.