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

[Translation] [Loco] Fix idempotency of LocoProvider write method #44187

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 29, 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
26 changes: 19 additions & 7 deletions 26 src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ public function write(TranslatorBagInterface $translatorBag): void
}

foreach ($catalogue->all() as $domain => $messages) {
$ids = $this->getAssetsIds($domain);
$this->translateAssets(array_combine($ids, array_values($messages)), $locale);
$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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if some keys don't have an asset id ? This would fail in array_combine AFAICT due to the different number of items.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case could not happen, Loco generate an asset for every asset.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we just create assets for new keys, they mandatory have an asset id

}
}
}
Expand Down Expand Up @@ -122,11 +129,7 @@ public function read(array $domains, array $locales): TranslatorBag
$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);
$catalogue->set($this->retrieveKeyFromId($key, $domain), $message, $domain);
}

$translatorBag->addCatalogue($catalogue);
Expand Down Expand Up @@ -289,4 +292,13 @@ private function getLocales(): array
return $carry;
}, []);
}

private function retrieveKeyFromId(string $id, string $domain): string
{
if (str_starts_with($id, $domain.'__')) {
return substr($id, \strlen($domain) + 2);
}

return $id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function testCompleteWriteProcess()
$this->assertSame(['filter' => 'messages'], $options['query']);
$this->assertSame($expectedAuthHeader, $options['normalized_headers']['authorization'][0]);

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

return new MockResponse('[{"id":"validators__post.num_comments"}]');
return new MockResponse('[{"id":"validators__foo.existing_key"},{"id":"validators__post.num_comments"}]');
},
'translateAsset2' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$this->assertSame('POST', $method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
namespace Symfony\Component\Translation\Tests\Command;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
Expand All @@ -18,7 +18,6 @@
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\Provider\ProviderInterface;
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\Tests\Command\TranslationProviderTestCase;
use Symfony\Component\Translation\TranslatorBag;

/**
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.