Skip to content

Commit 80a9c9f

Browse files
committed
[Translation][Loco] Make http requests synchronous when reading the Loco API
1 parent 3fe93d6 commit 80a9c9f

File tree

1 file changed

+22
-32
lines changed

1 file changed

+22
-32
lines changed

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

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -92,47 +92,37 @@ public function read(array $domains, array $locales): TranslatorBag
9292
{
9393
$domains = $domains ?: ['*'];
9494
$translatorBag = new TranslatorBag();
95-
$responses = [];
9695

9796
foreach ($locales as $locale) {
9897
foreach ($domains as $domain) {
99-
$responses[] = [
100-
'response' => $this->client->request('GET', sprintf('export/locale/%s.xlf', rawurlencode($locale)), [
101-
'query' => [
102-
'filter' => $domain,
103-
'status' => 'translated',
104-
],
105-
]),
106-
'locale' => $locale,
107-
'domain' => $domain,
108-
];
109-
}
110-
}
111-
112-
foreach ($responses as $response) {
113-
$locale = $response['locale'];
114-
$domain = $response['domain'];
115-
$response = $response['response'];
98+
// Loco forbids concurrent requests, so the requests must be synchronous in order to prevent "429 Too Many Requests" errors.
99+
$response = $this->client->request('GET', sprintf('export/locale/%s.xlf', rawurlencode($locale)), [
100+
'query' => [
101+
'filter' => $domain,
102+
'status' => 'translated',
103+
],
104+
]);
105+
106+
if (404 === $response->getStatusCode()) {
107+
$this->logger->warning(sprintf('Locale "%s" for domain "%s" does not exist in Loco.', $locale, $domain));
108+
continue;
109+
}
116110

117-
if (404 === $response->getStatusCode()) {
118-
$this->logger->warning(sprintf('Locale "%s" for domain "%s" does not exist in Loco.', $locale, $domain));
119-
continue;
120-
}
111+
$responseContent = $response->getContent(false);
121112

122-
$responseContent = $response->getContent(false);
113+
if (200 !== $response->getStatusCode()) {
114+
throw new ProviderException('Unable to read the Loco response: '.$responseContent, $response);
115+
}
123116

124-
if (200 !== $response->getStatusCode()) {
125-
throw new ProviderException('Unable to read the Loco response: '.$responseContent, $response);
126-
}
117+
$locoCatalogue = $this->loader->load($responseContent, $locale, $domain);
118+
$catalogue = new MessageCatalogue($locale);
127119

128-
$locoCatalogue = $this->loader->load($responseContent, $locale, $domain);
129-
$catalogue = new MessageCatalogue($locale);
120+
foreach ($locoCatalogue->all($domain) as $key => $message) {
121+
$catalogue->set($this->retrieveKeyFromId($key, $domain), $message, $domain);
122+
}
130123

131-
foreach ($locoCatalogue->all($domain) as $key => $message) {
132-
$catalogue->set($this->retrieveKeyFromId($key, $domain), $message, $domain);
124+
$translatorBag->addCatalogue($catalogue);
133125
}
134-
135-
$translatorBag->addCatalogue($catalogue);
136126
}
137127

138128
return $translatorBag;

0 commit comments

Comments
 (0)