1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Translation\PlatformAdapter\PhraseApp; |
4
|
|
|
|
5
|
|
|
use FAPI\PhraseApp\PhraseAppClient; |
6
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
7
|
|
|
use Translation\Common\Exception\StorageException; |
8
|
|
|
use Translation\Common\Model\Message; |
9
|
|
|
use Translation\Common\Storage; |
10
|
|
|
use Translation\Common\TransferableStorage; |
11
|
|
|
use Translation\SymfonyStorage\XliffConverter; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Sascha-Oliver Prolic <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class PhraseApp implements Storage, TransferableStorage |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var PhraseAppClient |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $projectId; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $localeToIdMapping; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $domains; |
37
|
|
|
|
38
|
|
|
public function __construct(PhraseAppClient $client, string $projectId, array $localeToIdMapping, array $domains) |
39
|
|
|
{ |
40
|
|
|
$this->client = $client; |
41
|
|
|
$this->projectId = $projectId; |
42
|
|
|
$this->localeToIdMapping = $localeToIdMapping; |
43
|
|
|
$this->domains = $domains; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function get($locale, $domain, $key) |
47
|
|
|
{ |
48
|
|
|
throw new \BadMethodCallException('Not implemented'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function create(Message $message) |
52
|
|
|
{ |
53
|
|
|
throw new \BadMethodCallException('Not implemented'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function update(Message $message) |
57
|
|
|
{ |
58
|
|
|
throw new \BadMethodCallException('Not implemented'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function delete($locale, $domain, $key) |
62
|
|
|
{ |
63
|
|
|
throw new \BadMethodCallException('Not implemented'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function export(MessageCatalogueInterface $catalogue) |
70
|
|
|
{ |
71
|
|
|
$locale = $catalogue->getLocale(); |
72
|
|
|
$localeId = $this->getLocaleId($locale); |
73
|
|
|
|
74
|
|
|
foreach ($this->domains as $domain) { |
75
|
|
|
try { |
76
|
|
|
$response = $this->client->export()->locale($this->projectId, $localeId, 'symfony_xliff', [ |
77
|
|
|
'tag' => $domain |
78
|
|
|
]); |
79
|
|
|
} catch (\Throwable $e) { |
80
|
|
|
throw new StorageException($e->getMessage()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$catalogue->addCatalogue(XliffConverter::contentToCatalogue($response, $locale, $domain)); |
85
|
|
|
} catch (\Throwable $e) { |
86
|
|
|
// ignore empty translation files |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $catalogue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function import(MessageCatalogueInterface $catalogue) |
97
|
|
|
{ |
98
|
|
|
$locale = $catalogue->getLocale(); |
99
|
|
|
$localeId = $this->getLocaleId($locale); |
100
|
|
|
|
101
|
|
|
foreach ($this->domains as $domain) { |
102
|
|
|
$data = XliffConverter::catalogueToContent($catalogue, $domain); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$file = sys_get_temp_dir() . '/' . $domain . '.' . $locale . '.xlf'; |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
file_put_contents($file, $data); |
108
|
|
|
|
109
|
|
|
$this->client->import()->import($this->projectId, 'symfony_xliff', $file, [ |
110
|
|
|
'locale_id' => $localeId, |
111
|
|
|
'tags' => $domain, |
112
|
|
|
]); |
113
|
|
|
} catch (\Throwable $e) { |
114
|
|
|
throw new StorageException($e->getMessage()); |
115
|
|
|
} finally { |
116
|
|
|
unlink($file); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function getLocaleId(string $locale): string |
122
|
|
|
{ |
123
|
|
|
if (isset($this->localeToIdMapping[$locale])) { |
124
|
|
|
return $this->localeToIdMapping[$locale]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw new StorageException(sprintf('Id for locale "%s" has not been configured.', $locale)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.