Skip to content

[Translation] [LocoProvider] Use rawurlencode and separate tag setting #44816

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 28, 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
30 changes: 26 additions & 4 deletions src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function delete(TranslatorBagInterface $translatorBag): void

foreach (array_keys($catalogue->all()) as $domain) {
foreach ($this->getAssetsIds($domain) as $id) {
$responses[$id] = $this->client->request('DELETE', sprintf('assets/%s.json', $id));
$responses[$id] = $this->client->request('DELETE', sprintf('assets/%s.json', rawurlencode($id)));
}
}

Expand Down Expand Up @@ -202,7 +202,7 @@ private function translateAssets(array $translations, string $locale): void
$responses = [];

foreach ($translations as $id => $message) {
$responses[$id] = $this->client->request('POST', sprintf('translations/%s/%s', $id, $locale), [
$responses[$id] = $this->client->request('POST', sprintf('translations/%s/%s', rawurlencode($id), rawurlencode($locale)), [
'body' => $message,
]);
}
Expand All @@ -220,13 +220,35 @@ private function tagsAssets(array $ids, string $tag): void
$this->createTag($tag);
}

$response = $this->client->request('POST', sprintf('tags/%s.json', $tag), [
'body' => implode(',', $ids),
// Separate ids with and without comma.
$idsWithComma = $idsWithoutComma = [];
foreach ($ids as $id) {
if (false !== strpos($id, ',')) {
$idsWithComma[] = $id;
} else {
$idsWithoutComma[] = $id;
}
}

// Set tags for all ids without comma.
$response = $this->client->request('POST', sprintf('tags/%s.json', rawurlencode($tag)), [
'body' => implode(',', $idsWithoutComma),
]);

if (200 !== $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to tag assets with "%s" on Loco: "%s".', $tag, $response->getContent(false)));
}

// Set tags for each id with comma one by one.
foreach ($idsWithComma as $id) {
$response = $this->client->request('POST', sprintf('assets/%s/tags', rawurlencode($id)), [
'body' => ['name' => $tag],
]);

if (200 !== $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to tag asset "%s" with "%s" on Loco: "%s".', $id, $tag, $response->getContent(false)));
}
}
}

private function createTag(string $tag): void
Expand Down