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

Skip to content

[Translation][Loco] Loco Provider pull keys with hyphen notation instead of dot notation #43976

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

Closed
welcoMattic opened this issue Nov 9, 2021 · 3 comments

Comments

@welcoMattic
Copy link
Member

welcoMattic commented Nov 9, 2021

Symfony version(s) affected

5.3

Description

Following #43967, it seems that the bug is more serious than expected.

I seem to have inadvertently used a deprecated feature of the Loco API. As we were using it in php-translation loco adapter I haven't checked if it was already ok to use it.

The result is that now, we must pass globally unique id for each translation to Loco. The problem is that translation keys are unique by domain in Symfony.

How to reproduce

See #42395 (comment)

Possible Solution

One possible solution is to set id like:

$id = sprintf('%s_%s', $domain, $key);

And at the fetch time, iterate over all received messages, substract $domain . '_' from the id to set the right key:

$locoCatalogue = $this->loader->load($responseContent, $locale, $domain);
$catalogue = new MessageCatalogue($locale);

foreach ($locoCatalogue->all($domain) as $key => $message) {
    if (str_starts_with($key, $domain . '_')) {
        $key = str_replace($domain . '_', '', $key);
    }

    $catalogue->set($key, $message, $domain);
}

$translatorBag->addCatalogue($catalogue);

It could break something like the batch creation of assets (because local keys are not equals to the Loco ids now), but I think we can solve it.

Additional Context

this bug seems to not happen on Loco projects with an asset alias named name, which is the old and deprecated name of text parameter. It seems the value of name is auto generated by Loco with dot notation.

@welcoMattic welcoMattic added the Bug label Nov 9, 2021
@welcoMattic welcoMattic changed the title [Loco] Loco Provider pull keys with hyphen notation instead of dot notation [Translation][Loco] Loco Provider pull keys with hyphen notation instead of dot notation Nov 9, 2021
@stof
Copy link
Member

stof commented Nov 9, 2021

The only thing I would do is to use a different separator than _, as _ is a common character both in domains and keys, and so won't ensure uniqueness in those cases (otherwise, use at least multiple _ as a sequence to reduce the risk)

@welcoMattic
Copy link
Member Author

welcoMattic commented Nov 9, 2021

Of course, it can be __ or something else like @@

@fabpot
Copy link
Member

fabpot commented Nov 9, 2021

Let's use __

@fabpot fabpot closed this as completed Nov 16, 2021
fabpot added a commit that referenced this issue Nov 16, 2021
…ing Loco do it (welcoMattic)

This PR was merged into the 5.3 branch.

Discussion
----------

[Translation] [Loco] Generate id parameter instead of letting Loco do it

| 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 #43976
| License       | MIT
| Doc PR        |

With this PR we get rid of the auto generated id from Loco (which generate id with dash notation). The counterpart is that we have to iterate over the fetched catalogues to transform the received translation keys, I'm not 100% sure about the performance impact on very large catalogues.

Commits
-------

1a44526 Fix Loco Provider
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants