Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c19bc94

Browse filesBrowse files
bug #44187 [Translation] [Loco] Fix idempotency of LocoProvider write method (welcoMattic)
This PR was merged into the 5.3 branch. Discussion ---------- [Translation] [Loco] Fix idempotency of LocoProvider write method | 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 #43953 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | This PR fix the exception thrown when we push translations twice, with existing translations on Loco. Explanation of the fix: To translate an "asset" (a translation key in Loco vocabulary), we need its Loco `id`. But Loco does not allow us to retrieve an id from a key. So, we fetch all ids for the current domain. Then, we build a map with `key` as key, and `id` as value. After that, we can intersect the keys of this map and our messages array to get all Loco ids of the currently processed messages. ```php foreach ($catalogue->all() as $domain => $messages) { $keysIdsMap = []; foreach ($this->getAssetsIds($domain) as $id) { $keysIdsMap[$this->retrieveKeyFromId($id, $domain)] = $id; } $ids = array_intersect_key($keysIdsMap, $messages); $this->translateAssets(array_combine(array_values($ids), array_values($messages)), $locale); } ``` Todo: - [x] Make some real tests to be 100% sure there is no BC - [x] Add tests to guarantee the fix Commits ------- 66a3c03 Fix idempotency of LocoProvider write method
2 parents 7fd34c4 + 66a3c03 commit c19bc94
Copy full SHA for c19bc94

File tree

3 files changed

+22
-11
lines changed
Filter options

3 files changed

+22
-11
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php
+19-7Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,15 @@ public function write(TranslatorBagInterface $translatorBag): void
7575
}
7676

7777
foreach ($catalogue->all() as $domain => $messages) {
78-
$ids = $this->getAssetsIds($domain);
79-
$this->translateAssets(array_combine($ids, array_values($messages)), $locale);
78+
$keysIdsMap = [];
79+
80+
foreach ($this->getAssetsIds($domain) as $id) {
81+
$keysIdsMap[$this->retrieveKeyFromId($id, $domain)] = $id;
82+
}
83+
84+
$ids = array_intersect_key($keysIdsMap, $messages);
85+
86+
$this->translateAssets(array_combine(array_values($ids), array_values($messages)), $locale);
8087
}
8188
}
8289
}
@@ -122,11 +129,7 @@ public function read(array $domains, array $locales): TranslatorBag
122129
$catalogue = new MessageCatalogue($locale);
123130

124131
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);
132+
$catalogue->set($this->retrieveKeyFromId($key, $domain), $message, $domain);
130133
}
131134

132135
$translatorBag->addCatalogue($catalogue);
@@ -289,4 +292,13 @@ private function getLocales(): array
289292
return $carry;
290293
}, []);
291294
}
295+
296+
private function retrieveKeyFromId(string $id, string $domain): string
297+
{
298+
if (str_starts_with($id, $domain.'__')) {
299+
return substr($id, \strlen($domain) + 2);
300+
}
301+
302+
return $id;
303+
}
292304
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testCompleteWriteProcess()
148148
$this->assertSame(['filter' => 'messages'], $options['query']);
149149
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
150150

151-
return new MockResponse('[{"id":"messages__a"}]');
151+
return new MockResponse('[{"id":"messages__foo.existing_key"},{"id":"messages__a"}]');
152152
},
153153
'translateAsset1' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
154154
$this->assertSame('POST', $method);
@@ -164,7 +164,7 @@ public function testCompleteWriteProcess()
164164
$this->assertSame(['filter' => 'validators'], $options['query']);
165165
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);
166166

167-
return new MockResponse('[{"id":"validators__post.num_comments"}]');
167+
return new MockResponse('[{"id":"validators__foo.existing_key"},{"id":"validators__post.num_comments"}]');
168168
},
169169
'translateAsset2' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
170170
$this->assertSame('POST', $method);

‎src/Symfony/Component/Translation/Tests/Command/TranslationPushCommandTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Command/TranslationPushCommandTest.php
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
12+
namespace Symfony\Component\Translation\Tests\Command;
1313

1414
use Symfony\Component\Console\Application;
1515
use Symfony\Component\Console\Tester\CommandTester;
@@ -18,7 +18,6 @@
1818
use Symfony\Component\Translation\Loader\XliffFileLoader;
1919
use Symfony\Component\Translation\Provider\ProviderInterface;
2020
use Symfony\Component\Translation\Reader\TranslationReader;
21-
use Symfony\Component\Translation\Tests\Command\TranslationProviderTestCase;
2221
use Symfony\Component\Translation\TranslatorBag;
2322

2423
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.