8000 Fix idempotency of LocoProvider write method · symfony/symfony@66a3c03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66a3c03

Browse files
committed
Fix idempotency of LocoProvider write method
1 parent 0f6b07e commit 66a3c03

File tree

3 files changed

+22
-11
lines changed

3 files changed< 8000 /h2>
+22
-11
lines changed

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

Lines 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

Lines 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

Lines 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