1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ProcyonWeb\TranslationGenerator; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Lokalise\LokaliseApiClient; |
7
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
8
|
|
|
use ZipArchive; |
9
|
|
|
|
10
|
|
|
class LokaliseClient |
11
|
|
|
{ |
12
|
|
|
/** @var LokaliseApiClient */ |
13
|
|
|
private $client; |
14
|
|
|
|
15
|
|
|
/** @var string|null */ |
16
|
|
|
private $projectId; |
17
|
|
|
/** |
18
|
|
|
* @var ConsoleOutput |
19
|
|
|
*/ |
20
|
|
|
private $output; |
21
|
|
|
|
22
|
|
|
public function __construct(ConsoleOutput $output) |
23
|
|
|
{ |
24
|
|
|
$this->output = $output; |
25
|
|
|
$apiToken = config('translation.lokalise.apiToken'); |
|
|
|
|
26
|
|
|
$projectId = config('translation.lokalise.projectId'); |
27
|
|
|
|
28
|
|
|
if ($apiToken && $projectId) { |
29
|
|
|
$this->client = new LokaliseApiClient($apiToken); |
30
|
|
|
$this->projectId = $projectId; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function isReady(): bool |
35
|
|
|
{ |
36
|
|
|
return $this->client !== null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function uploadFile(string $data, string $filename, string $langIso): bool |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
$response = $this->client->files->upload( |
43
|
|
|
$this->projectId, |
44
|
|
|
[ |
45
|
|
|
'data' => base64_encode($data), |
46
|
|
|
'filename' => $filename, |
47
|
|
|
'lang_iso' => $langIso, |
48
|
|
|
'slashn_to_linebreak' => true, |
49
|
|
|
] |
50
|
|
|
); |
51
|
|
|
$result = $response->getContent()['result']; |
52
|
|
|
$this->output->writeln( |
53
|
|
|
sprintf( |
54
|
|
|
'[LOKALISE] %s file uploaded. %d skipped, %d inserted, %d updated', |
55
|
|
|
$filename, |
56
|
|
|
$result['skipped'], |
57
|
|
|
$result['inserted'], |
58
|
|
|
$result['updated'] |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} catch (\Lokalise\Exceptions\LokaliseResponseException $e) { |
64
|
|
|
$this->output->writeln( |
65
|
|
|
sprintf( |
66
|
|
|
'[LOKALISE] Failed to upload file: %s. %s', |
67
|
|
|
$filename, |
68
|
|
|
$e->getMessage() |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function downloadFiles(): bool |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$response = $this->client->files->download( |
80
|
|
|
$this->projectId, |
81
|
|
|
[ |
82
|
|
|
'format' => 'json', |
83
|
|
|
'original_filenames' => false, |
84
|
|
|
'bundle_structure' => '%LANG_ISO%.%FORMAT%', |
85
|
|
|
'export_empty_as' => 'base', |
86
|
|
|
] |
87
|
|
|
); |
88
|
|
|
$result = $response->getContent()['result']; |
89
|
|
|
$bundleUrl = $result['bundle_url']; |
90
|
|
|
|
91
|
|
|
$this->output->writeln(sprintf('[LOKALISE] Downloading translations from bundle url: %s', $bundleUrl)); |
92
|
|
|
|
93
|
|
|
$path = storage_path('app').'locale.zip'; |
|
|
|
|
94
|
|
|
$file = fopen($path, 'wb'); |
95
|
|
|
$client = new Client(); |
96
|
|
|
$client->get($bundleUrl, ['save_to' => $file]); |
97
|
|
|
|
98
|
|
|
$zip = new ZipArchive; |
99
|
|
|
$zip->open($path); |
100
|
|
|
$zip->extractTo(resource_path('lang')); |
|
|
|
|
101
|
|
|
$zip->close(); |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} catch (\Throwable $e) { |
105
|
|
|
$this->output->writeln( |
106
|
|
|
sprintf( |
107
|
|
|
'[LOKALISE] Failed to download files. %s', |
108
|
|
|
$e->getMessage() |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
} |