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

Skip to content

[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

Conversation

welcoMattic
Copy link
Member

@welcoMattic welcoMattic commented Nov 21, 2021

Q A
Branch? 5.3
Bug fix? yes
New feature? no
Deprecations? no
Tickets Fix #43953
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.

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:

  • Make some real tests to be 100% sure there is no BC
  • Add tests to guarantee the fix

@welcoMattic welcoMattic changed the title [Translation] [Loco] Fix idempotency of translation:push on Loco [Translation] [Loco] Fix idempotency of LocoProvider write method Nov 26, 2021
@welcoMattic
Copy link
Member Author

Ready for review /cc @nicolas-grekas @OskarStark


$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

@nicolas-grekas
Copy link
Member

Thank you @welcoMattic.

@nicolas-grekas nicolas-grekas merged commit c19bc94 into symfony:5.3 Nov 29, 2021
This was referenced Nov 29, 2021
@fabpot fabpot mentioned this pull request Dec 29, 2021
@welcoMattic welcoMattic deleted the fix/loco-provider branch July 6, 2022 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants