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