From 80a9c9fa403ebf7bdb3a87d0e4b69d4c1ee05763 Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Thu, 2 Dec 2021 13:32:05 +0100 Subject: [PATCH] [Translation][Loco] Make http requests synchronous when reading the Loco API --- .../Translation/Bridge/Loco/LocoProvider.php | 54 ++++++++----------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php b/src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php index 3882176d8d54a..cea4121f7364e 100644 --- a/src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php +++ b/src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php @@ -92,47 +92,37 @@ public function read(array $domains, array $locales): TranslatorBag { $domains = $domains ?: ['*']; $translatorBag = new TranslatorBag(); - $responses = []; foreach ($locales as $locale) { foreach ($domains as $domain) { - $responses[] = [ - 'response' => $this->client->request('GET', sprintf('export/locale/%s.xlf', rawurlencode($locale)), [ - 'query' => [ - 'filter' => $domain, - 'status' => 'translated', - ], - ]), - 'locale' => $locale, - 'domain' => $domain, - ]; - } - } - - foreach ($responses as $response) { - $locale = $response['locale']; - $domain = $response['domain']; - $response = $response['response']; + // Loco forbids concurrent requests, so the requests must be synchronous in order to prevent "429 Too Many Requests" errors. + $response = $this->client->request('GET', sprintf('export/locale/%s.xlf', rawurlencode($locale)), [ + 'query' => [ + 'filter' => $domain, + 'status' => 'translated', + ], + ]); + + if (404 === $response->getStatusCode()) { + $this->logger->warning(sprintf('Locale "%s" for domain "%s" does not exist in Loco.', $locale, $domain)); + continue; + } - if (404 === $response->getStatusCode()) { - $this->logger->warning(sprintf('Locale "%s" for domain "%s" does not exist in Loco.', $locale, $domain)); - continue; - } + $responseContent = $response->getContent(false); - $responseContent = $response->getContent(false); + if (200 !== $response->getStatusCode()) { + throw new ProviderException('Unable to read the Loco response: '.$responseContent, $response); + } - if (200 !== $response->getStatusCode()) { - throw new ProviderException('Unable to read the Loco response: '.$responseContent, $response); - } + $locoCatalogue = $this->loader->load($responseContent, $locale, $domain); + $catalogue = new MessageCatalogue($locale); - $locoCatalogue = $this->loader->load($responseContent, $locale, $domain); - $catalogue = new MessageCatalogue($locale); + foreach ($locoCatalogue->all($domain) as $key => $message) { + $catalogue->set($this->retrieveKeyFromId($key, $domain), $message, $domain); + } - foreach ($locoCatalogue->all($domain) as $key => $message) { - $catalogue->set($this->retrieveKeyFromId($key, $domain), $message, $domain); + $translatorBag->addCatalogue($catalogue); } - - $translatorBag->addCatalogue($catalogue); } return $translatorBag;