Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c49719

Browse files
committed
Fix idempotency of translation:push on Loco
1 parent 0f6b07e commit 2c49719

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ public function write(TranslatorBagInterface $translatorBag): void
7575
}
7676

7777
foreach ($catalogue->all() as $domain => $messages) {
78-
$ids = $this->getAssetsIds($domain);
78+
$keysFromLoco = array_map(function ($id) use($domain) {
79+
return $this->retrieveKeyFromId($id, $domain);
80+
}, $this->getAssetsIds($domain));
81+
82+
$ids = array_intersect($keysFromLoco, array_keys($messages));
83+
7984
$this->translateAssets(array_combine($ids, array_values($messages)), $locale);
8085
}
8186
}
@@ -122,11 +127,7 @@ public function read(array $domains, array $locales): TranslatorBag
122127
$catalogue = new MessageCatalogue($locale);
123128

124129
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+
$catalogue->set($this->retrieveKeyFromId($key, $domain), $message, $domain);
130131
}
131132

132133
$translatorBag->addCatalogue($catalogue);
@@ -289,4 +290,13 @@ private function getLocales(): array
289290
return $carry;
290291
}, []);
291292
}
293+
294+
private function retrieveKeyFromId(string $id, string $domain): string
295+
{
296+
if (str_starts_with($id, $domain.'__')) {
297+
return substr($id, \strlen($domain) + 2);
298+
}
299+
300+
return $id;
301+
}
292302
}

0 commit comments

Comments
 (0)