Skip to content

[Translation] [Loco] Generate id parameter instead of letting Loco do it #43990

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
Nov 16, 2021
Merged
Show file tree
Hide file tree
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
19 changes: 16 additions & 3 deletions src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Exception\ProviderException;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Provider\ProviderInterface;
use Symfony\Component\Translation\TranslatorBag;
use Symfony\Component\Translation\TranslatorBagInterface;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function write(TranslatorBagInterface $translatorBag): void
}

foreach ($catalogue->all() as $domain => $messages) {
$createdIds = $this->createAssets(array_keys($messages));
$createdIds = $this->createAssets(array_keys($messages), $domain);
if ($createdIds) {
$this->tagsAssets($createdIds, $domain);
}
Expand Down Expand Up @@ -117,7 +118,18 @@ public function read(array $domains, array $locales): TranslatorBag
throw new ProviderException('Unable to read the Loco response: '.$responseContent, $response);
}

$translatorBag->addCatalogue($this->loader->load($responseContent, $locale, $domain));
$locoCatalogue = $this->loader->load($responseContent, $locale, $domain);
$catalogue = new MessageCatalogue($locale);

foreach ($locoCatalogue->all($domain) as $key => $message) {
if (str_starts_with($key, $domain.'__')) {
$key = substr($key, \strlen($domain) + 2);
}

$catalogue->set($key, $message, $domain);
}

$translatorBag->addCatalogue($catalogue);
}

return $translatorBag;
Expand Down Expand Up @@ -166,13 +178,14 @@ private function getAssetsIds(string $domain): array
}, $response->toArray(false));
}

private function createAssets(array $keys): array
private function createAssets(array $keys, string $domain): array
{
$responses = $createdIds = [];

foreach ($keys as $key) {
$responses[$key] = $this->client->request('POST', 'assets', [
'body' => [
'id' => $domain.'__'.$key, // must be globally unique, not only per domain
'text' => $key,
'type' => 'text',
'default' => 'untranslated',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function testCompleteWriteProcess()
$responses = [
'createAsset1' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$expectedBody = http_build_query([
'id' => 'messages__a',
'text' => 'a',
'type' => 'text',
'default' => 'untranslated',
Expand All @@ -72,7 +73,7 @@ public function testCompleteWriteProcess()
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
$this->assertSame($expectedBody, $options['body']);

return new MockResponse('{"id": "1337"}', ['http_code' => 201]);
return new MockResponse('{"id": "messages__a"}', ['http_code' => 201]);
},
'getTags1' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$this->assertSame('GET', $method);
Expand All @@ -93,12 +94,13 @@ public function testCompleteWriteProcess()
$this->assertSame('POST', $method);
$this->assertSame('https://localise.biz/api/tags/messages.json', $url);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
$this->assertSame('1337', $options['body']);
$this->assertSame('messages__a', $options['body']);

return new MockResponse();
},
'createAsset2' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$expectedBody = http_build_query([
'id' => 'validators__post.num_comments',
'text' => 'post.num_comments',
'type' => 'text',
'default' => 'untranslated',
Expand All @@ -108,7 +110,7 @@ public function testCompleteWriteProcess()
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
$this->assertSame($expectedBody, $options['body']);

return new MockResponse('{"id": "1234"}', ['http_code' => 201]);
return new MockResponse('{"id": "validators__post.num_comments"}', ['http_code' => 201]);
},
'getTags2' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$this->assertSame('GET', $method);
Expand All @@ -129,7 +131,7 @@ public function testCompleteWriteProcess()
$this->assertSame('POST', $method);
$this->assertSame('https://localise.biz/api/tags/validators.json', $url);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
$this->assertSame('1234', $options['body']);
$this->assertSame('validators__post.num_comments', $options['body']);

return new MockResponse();
},
Expand All @@ -146,11 +148,11 @@ public function testCompleteWriteProcess()
$this->assertSame(['filter' => 'messages'], $options['query']);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);

return new MockResponse('[{"id":"1337"}]');
return new MockResponse('[{"id":"messages__a"}]');
},
'translateAsset1' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://localise.biz/api/translations/1337/en', $url);
$this->assertSame('https://localise.biz/api/translations/messages__a/en', $url);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
$this->assertSame('trans_en_a', $options['body']);

Expand All @@ -162,11 +164,11 @@ public function testCompleteWriteProcess()
$this->assertSame(['filter' => 'validators'], $options['query']);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);

return new MockResponse('[{"id":"1234"}]');
return new MockResponse('[{"id":"validators__post.num_comments"}]');
},
'translateAsset2' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://localise.biz/api/translations/1234/en', $url);
$this->assertSame('https://localise.biz/api/translations/validators__post.num_comments/en', $url);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
$this->assertSame('{count, plural, one {# comment} other {# comments}}', $options['body']);

Expand All @@ -193,11 +195,11 @@ public function testCompleteWriteProcess()
$this->assertSame(['filter' => 'messages'], $options['query']);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);

return new MockResponse('[{"id":"1337"}]');
return new MockResponse('[{"id":"messages__a"}]');
},
'translateAsset3' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://localise.biz/api/translations/1337/fr', $url);
$this->assertSame('https://localise.biz/api/translations/messages__a/fr', $url);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
$this->assertSame('trans_fr_a', $options['body']);

Expand All @@ -209,11 +211,11 @@ public function testCompleteWriteProcess()
$this->assertSame(['filter' => 'validators'], $options['query']);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);

return new MockResponse('[{"id":"1234"}]');
return new MockResponse('[{"id":"validators__post.num_comments"}]');
},
'translateAsset4' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://localise.biz/api/translations/1234/fr', $url);
$this->assertSame('https://localise.biz/api/translations/validators__post.num_comments/fr', $url);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
$this->assertSame('{count, plural, one {# commentaire} other {# commentaires}}', $options['body']);

Expand Down Expand Up @@ -321,11 +323,11 @@ function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('https://localise.biz/api/assets?filter=messages', $url);
$this->assertSame(['filter' => 'messages'], $options['query']);

return new MockResponse('[{"id":"1337"}]');
return new MockResponse('[{"id":"messages__a"}]');
},
function (string $method, string $url): MockResponse {
$this->assertSame('DELETE', $method);
$this->assertSame('https://localise.biz/api/assets/1337.json', $url);
$this->assertSame('https://localise.biz/api/assets/messages__a.json', $url);

return new MockResponse();
},
Expand All @@ -334,11 +336,11 @@ function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('https://localise.biz/api/assets?filter=validators', $url);
$this->assertSame(['filter' => 'validators'], $options['query']);

return new MockResponse('[{"id":"1234"}]');
return new MockResponse('[{"id":"validators__post.num_comments"}]');
},
function (string $method, string $url): MockResponse {
$this->assertSame('DELETE', $method);
$this->assertSame('https://localise.biz/api/assets/1234.json', $url);
$this->assertSame('https://localise.biz/api/assets/validators__post.num_comments.json', $url);

return new MockResponse();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"php": ">=7.2.5",
"symfony/http-client": "^5.3",
"symfony/config": "^5.3",
"symfony/polyfill-php80": "^1.23",
"symfony/translation": "^5.3"
},
"autoload": {
Expand Down