8000 Refactored LocoProviderTest · symfony/symfony@40041db · GitHub
[go: up one dir, main page]

Skip to content

Commit 40041db

Browse files
committed
Refactored LocoProviderTest
1 parent e03ab57 commit 40041db

File tree

2 files changed

+365
-309
lines changed

2 files changed

+365
-309
lines changed

src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php

Lines changed: 101 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,23 @@ public function write(TranslatorBagInterface $translatorBag): void
5959
$catalogue = $translatorBag->getCatalogues()[0];
6060
}
6161

62-
// Create keys on Loco
6362
foreach ($catalogue->all() as $domain => $messages) {
64-
$ids = [];
65-
foreach ($messages as $id => $message) {
66-
$ids[] = $id;
67-
$this->createAsset($id);
68-
}
69-
if ($ids) {
70-
$this->tagsAssets($ids, $domain);
63+
$createdIds = $this->createAssets(array_keys($messages));
64+
if ($createdIds) {
65+
$this->tagsAssets($createdIds, $domain);
7166
}
7267
}
7368

74-
// Push translations in all locales and tag them with domain
7569
foreach ($translatorBag->getCatalogues() as $catalogue) {
7670
$locale = $catalogue->getLocale();
7771

7872
if (!\in_array($locale, $this->getLocales())) {
7973
$this->createLocale($locale);
8074
}
8175

82-
foreach ($catalogue->all() as $messages) {
83-
foreach ($messages as $id => $message) {
84-
$this->translateAsset($id, $message, $locale);
85-
}
76+
foreach ($catalogue->all() as $domain => $messages) {
77+
$ids = $this->getAssetsIds($domain);
78+
$this->translateAssets(array_combine($ids, array_values($messages)), $locale);
8679
}
8780
}
8881
}
@@ -91,92 +84,142 @@ public function read(array $domains, array $locales): TranslatorBag
9184
{
9285
$domains = $domains ?: ['*'];
9386
$translatorBag = new TranslatorBag();
87+
$responses = [];
9488

9589
foreach ($locales as $locale) {
9690
foreach ($domains as $domain) {
97-
$response = $this->client->request('GET', sprintf('export/locale/%s.xlf?filter=%s&status=translated', $locale, $domain));
91+
$responses[] = [
92+
'response' => $this->client->request('GET', sprintf('export/locale/%s.xlf', rawurlencode($locale)), [
93+
'query' => [
94+
'filter' => $domain,
95+
'status' => 'translated',
96+
],
97+
]),
98+
'locale' => $locale,
99+
'domain' => $domain,
100+
];
101+
}
102+
}
98103

99-
if (404 === $response->getStatusCode()) {
100-
$this->logger->error(sprintf('Locale "%s" for domain "%s" does not exist in Loco.', $locale, $domain));
101-
continue;
102-
}
104+
foreach ($responses as $response) {
105+
$locale = $response['locale'];
106+
$domain = $response['domain'];
107+
$response = $response['response'];
103108

104-
$responseContent = $response->getContent(false);
109+
if (404 === $response->getStatusCode()) {
110+
$this->logger->warning(sprintf('Locale "%s" for domain "%s" does not exist in Loco.', $locale, $domain));
111+
continue;
112+
}
105113

106-
if (200 !== $response->getStatusCode()) {
107-
throw new ProviderException('Unable to read the Loco response: '.$responseContent, $response);
108-
}
114+
$responseContent = $response->getContent(false);
109115

110-
$translatorBag->addCatalogue($this->loader->load($responseContent, $locale, $domain));
116+
if (200 !== $response->getStatusCode()) {
117+
throw new ProviderException('Unable to read the Loco response: '.$responseContent, $response);
111118
}
119+
120+
$translatorBag->addCatalogue($this->loader->load($responseContent, $locale, $domain));
112121
}
113122

114123
return $translatorBag;
115124
}
116125

117126
public function delete(TranslatorBagInterface $translatorBag): void
118127
{
119-
$deletedIds = [];
128+
$catalogue = $translatorBag->getCatalogue($this->defaultLocale);
120129

121-
foreach ($translatorBag->getCatalogues() as $catalogue) {
122-
foreach ($catalogue->all() as $messages) {
123-
foreach ($messages as $id => $message) {
124-
if (\in_array($id, $deletedIds, true)) {
125-
continue;
126-
}
127-
128-
$this->deleteAsset($id);
129-
$deletedIds[] = $id;
130-
}
130+
if (!$catalogue) {
131+
$catalogue = $translatorBag->getCatalogues()[0];
132+
}
133+
134+
$responses = [];
135+
136+
foreach (array_keys($catalogue->all()) as $domain) {
137+
foreach ($this->getAssetsIds($domain) as $id) {
138+
$responses[$id] = $this->client->request('DELETE', sprintf('assets/%s.json', $id));
139+
}
140+
}
141+
142+
foreach ($responses as $key => $response) {
143+
if (403 === $response->getStatusCode()) {
144+
$this->logger->error('The API key used does not have sufficient permissions to delete assets.');
145+
}
146+
147+
if (200 !== $response->getStatusCode() && 404 !== $response->getStatusCode()) {
148+
$this->logger->error(sprintf('Unable to delete translation key "%s" to Loco: "%s".', $key, $response->getContent(false)));
131149
}
132150
}
133151
}
134152

135-
private function createAsset(string $id): void
153+
/**
154+
* Returns array of internal Loco's unique ids.
155+
*/
156+
private function getAssetsIds(string $domain): array
136157
{
137-
$response = $this->client->request('POST', 'assets', [
138-
'body' => [
139-
'name' => $id,
140-
'id' => $id,
141-
'type' => 'text',
142-
'default' => 'untranslated',
143-
],
144-
]);
158+
$response = $this->client->request('GET', 'assets', ['query' => ['filter' => $domain]]);
145159

146-
if (409 === $response->getStatusCode()) {
147-
$this->logger->info(sprintf('Translation key "%s" already exists in Loco.', $id), [
148-
'id' => $id,
160+
if (200 !== $response->getStatusCode()) {
161+
$this->logger->error(sprintf('Unable to get assets from Loco: "%s".', $response->getContent(false)));
162+
}
163+
164+
return array_map(function ($asset) {
165+
return $asset['id'];
166+
}, $response->toArray(false));
167+
}
168+
169+
private function createAssets(array $keys): array
170+
{
171+
$responses = $createdIds = [];
172+
173+
foreach ($keys as $key) {
174+
$responses[$key] = $this->client->request('POST', 'assets', [
175+
'body' => [
176+
'text' => $key,
177+
'type' => 'text',
178+
'default' => 'untranslated',
179+
],
149180
]);
150-
} elseif (201 !== $response->getStatusCode()) {
151-
$this->logger->error(sprintf('Unable to add new translation key "%s" to Loco: (status code: "%s") "%s".', $id, $response->getStatusCode(), $response->getContent(false)));
152181
}
182+
183+
foreach ($responses as $key => $response) {
184+
if (201 !== $response->getStatusCode()) {
185+
$this->logger->error(sprintf('Unable to add new translation key "%s" to Loco: (status code: "%s") "%s".', $key, $response->getStatusCode(), $response->getContent(false)));
186+
} else {
187+
$createdIds[] = $response->toArray(false)['id'];
188+
}
189+
}
190+
191+
return $createdIds;
153192
}
154193

155-
private function translateAsset(string $id, string $message, string $locale): void
194+
private function translateAssets(array $translations, string $locale): void
156195
{
157-
$response = $this->client->request('POST', sprintf('translations/%s/%s', $id, $locale), [
158-
'body' => $message,
159-
]);
196+
$responses = [];
160197

161-
if (200 !== $response->getStatusCode()) {
162-
$this->logger->error(sprintf('Unable to add translation message "%s" (for key: "%s" in locale "%s") to Loco: "%s".', $message, $id, $locale, $response->getContent(false)));
198+
foreach ($translations as $id => $message) {
199+
$responses[$id] = $this->client->request('POST', sprintf('translations/%s/%s', $id, $locale), [
200+
'body' => $message,
201+
]);
202+
}
203+
204+
foreach ($responses as $id => $response) {
205+
if (200 !== $response->getStatusCode()) {
206+
$this->logger->error(sprintf('Unable to add translation for key "%s" in locale "%s" to Loco: "%s".', $id, $locale, $response->getContent(false)));
207+
}
163208
}
164209
}
165210

166211
private function tagsAssets(array $ids, string $tag): void
167212
{
168-
$idsAsString = implode(',', array_unique($ids));
169-
170213
if (!\in_array($tag, $this->getTags(), true)) {
171214
$this->createTag($tag);
172215
}
173216

174217
$response = $this->client->request('POST', sprintf('tags/%s.json', $tag), [
175-
'body' => $idsAsString,
218+
'body' => implode(',', $ids),
176219
]);
177220

178221
if (200 !== $response->getStatusCode()) {
179-
$this->logger->error(sprintf('Unable to add tag "%s" on translation keys "%s" to Loco: "%s".', $tag, $idsAsString, $response->getContent(false)));
222+
$this->logger->error(sprintf('Unable to tag assets with "%s" on Loco: "%s".', $tag, $response->getContent(false)));
180223
}
181224
}
182225

@@ -233,13 +276,4 @@ private function getLocales(): array
233276
return $carry;
234277
}, []);
235278
}
236-
237-
private function deleteAsset(string $id): void
238-
{
239-
$response = $this->client->request('DELETE', sprintf('assets/%s.json', $id));
240-
241-
if (200 !== $response->getStatusCode()) {
242-
$this->logger->error(sprintf('Unable to delete translation key "%s" to Loco: "%s".', $id, $response->getContent(false)));
243-
}
244-
}
245279
}

0 commit comments

Comments
 (0)
0