8000 [Translation] Make http requests synchronous when reading the Loco API by Kocal · Pull Request #44416 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Make http requests synchronous when reading the Loco API #44416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 22 additions & 32 deletions src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
0