|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ProcyonWeb\TranslationGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
|
|
7
|
|
|
class GenerateCommand extends Command |
|
8
|
|
|
{ |
|
9
|
|
|
protected $signature = 'translation:generate {lang=en} {--upload}'; |
|
10
|
|
|
|
|
11
|
|
|
protected $description = 'Generate missing translation strings in php and Vue files'; |
|
12
|
|
|
|
|
13
|
|
|
/** @var PhraseClient */ |
|
14
|
|
|
private $client; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(PhraseClient $client) |
|
17
|
|
|
{ |
|
18
|
|
|
parent::__construct(); |
|
19
|
|
|
$this->client = $client; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function handle(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$translatables = (new SearchService())->getTranslatableStrings(config('translation.generator.patterns')); |
|
|
|
|
|
|
25
|
|
|
$this->client->downloadFiles(); |
|
26
|
|
|
|
|
27
|
|
|
$lang = $this->argument('lang'); |
|
28
|
|
|
$fileName = 'resources/lang/' . $lang . '.json'; |
|
|
|
|
|
|
29
|
|
|
$jsonFile = file_get_contents($fileName); |
|
30
|
|
|
$translations = json_decode($jsonFile, true); |
|
31
|
|
|
|
|
32
|
|
|
foreach ($translatables as $translatable) { |
|
33
|
|
|
$translatable = str_replace('\\', '', $translatable); |
|
34
|
|
|
if (!array_key_exists($translatable, $translations)) { |
|
35
|
|
|
$translations[$translatable] = $translatable; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$content = json_encode($translations, JSON_PRETTY_PRINT + JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES); |
|
40
|
|
|
file_put_contents($fileName, $content); |
|
41
|
|
|
|
|
42
|
|
|
$isUpload = $this->option('upload'); |
|
43
|
|
|
if ($isUpload && $this->client->isReady()) { |
|
44
|
|
|
$this->client->uploadFile($content, $fileName, $lang); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |