8000 bug #43990 [Translation] [Loco] Generate id parameter instead of lett… · symfony/symfony@56798bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 56798bd

Browse files
committed
bug #43990 [Translation] [Loco] Generate id parameter instead of letting Loco do it (welcoMattic)
This PR was merged into the 5.3 branch. Discussion ---------- [Translation] [Loco] Generate id parameter instead of letting Loco do it | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #43976 | License | MIT | Doc PR | With this PR we get rid of the auto generated id from Loco (which generate id with dash notation). The counterpart is that we have to iterate over the fetched catalogues to transform the received translation keys, I'm not 100% sure about the performance impact on very large catalogues. Commits ------- 1a44526 Fix Loco Provider
2 parents a6bc801 + 1a44526 commit 56798bd

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Translation\Exception\ProviderException;
1616
use Symfony\Component\Translation\Loader\LoaderInterface;
17+
use Symfony\Component\Translation\MessageCatalogue;
1718
use Symfony\Component\Translation\Provider\ProviderInterface;
1819
use Symfony\Component\Translation\TranslatorBag;
1920
use Symfony\Component\Translation\TranslatorBagInterface;
@@ -60,7 +61,7 @@ public function write(TranslatorBagInterface $translatorBag): void
6061
}
6162

6263
foreach ($catalogue->all() as $domain => $messages) {
63-
$createdIds = $this->createAssets(array_keys($messages));
64+
$createdIds = $this->createAssets(array_keys($messages), $domain);
6465
if ($createdIds) {
6566
$this->tagsAssets($createdIds, $domain);
6667
}
@@ -117,7 +118,18 @@ public function read(array $domains, array $locales): TranslatorBag
117118
throw new ProviderException('Unable to read the Loco response: '.$responseContent, $response);
118119
}
119120

120-
$translatorBag->addCatalogue($this->loader->load($responseContent, $locale, $domain));
121+
$locoCatalogue = $this->loader->load($responseContent, $locale, $domain);
122+
$catalogue = new MessageCatalogue($locale);
123+
124+
foreach ($locoCatalogue->all($domain) as $key => $message) {
125+
if (str_starts_with($key, $domain.'__')) {
126+
$key = substr($key, \strlen($domain) + 2);
127+
}
128+
129+
$catalogue->set($key, $message, $domain);
130+
}
131+
132+
$translatorBag->addCatalogue($catalogue);
121133
}
122134

123135
return $translatorBag;
@@ -166,13 +178,14 @@ private function getAssetsIds(string $domain): array
166178
}, $response->toArray(false));
167179
}
168180

169-
private function createAssets(array $keys): array
181+
private function createAssets(array $keys, string $domain): array
170182
{
171183
$responses = $createdIds = [];
172184

173185
foreach ($keys as $key) {
174186
$responses[$key] = $this->client->request('POST', 'assets', [
175187
'body' => [
188+
'id' => $domain.'__'.$key, // must be globally unique, not only per domain
176189
'text' => $key,
177190
'type' => 'text',
178191
'default' => 'untranslated',

src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderTest.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function testCompleteWriteProcess()
6363
$responses = [
6464
'createAsset1' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
6565
$expectedBody = http_build_query([
66+
'id' => 'messages__a',
6667
'text' => 'a',
6768
'type' => 'text',
6869
'default' => 'untranslated',
@@ -72,7 +73,7 @@ public function testCompleteWriteProcess()
7273
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
7374
$this->assertSame($expectedBody, $options['body']);
7475

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

9899
return new MockResponse();
99100
},
100101
'createAsset2' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
101102
$expectedBody = http_build_query([
103+
'id' => 'validators__post.num_comments',
102104
'text' => 'post.num_comments',
103105
'type' => 'text',
104106
'default' => 'untranslated',
@@ -108,7 +110,7 @@ public function testCompleteWriteProcess()
108110
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
109111
$this->assertSame($expectedBody, $options['body']);
110112

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

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

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

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

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

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

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

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

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

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

324-
return new MockResponse('[{"id":"1337"}]');
326+
return new MockResponse('[{"id":"messages__a"}]');
325327
},
326328
function (string $method, string $url): MockResponse {
327329
$this->assertSame('DELETE', $method);
328-
$this->assertSame('https://localise.biz/api/assets/1337.json', $url);
330+
$this->assertSame('https://localise.biz/api/assets/messages__a.json', $url);
329331

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

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

343345
return new MockResponse();
344346
},

src/Symfony/Component/Translation/Bridge/Loco/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"php": ">=7.2.5",
2020
"symfony/http-client": "^5.3",
2121
"symfony/config": "^5.3",
22+
"symfony/polyfill-php80": "^1.23",
2223
"symfony/translation": "^5.3"
2324
},
2425
"autoload": {

0 commit comments

Comments
 (0)
0