-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation][TwigBridge] Implementing variables extraction in "translation:update" #38884
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
Conversation
35713fe
to
8ac5e2a
Compare
I think that this PR may be ready for review, please take a look at the required feedback section on top of description. Thank you for your time. |
Hi @jderusse, I'm sorry to bother you. I just wanted to ask if there's anything that I can do to have the feedback I'm missing in the main PR message in order to finalize the development of this feature. It's not much stuff, but I would like to complete this in order to leave you a better PR to review. Thank you for your time. |
74cba18
to
c0a6eff
Compare
Hi @yceruto, my PR is having conflicts with your #40050 since I was facing the same issue while implementing the feature. May I ask you if you would suggest resolving the conflict keeping your changes or using mine? (I think they are almost the same except for a more explicit variable naming on my side, but I don't know if that's considered too verbose) Thank you for your time. |
Hi @liarco, keep your changes (the new naming looks good to me) while tests passed it's ok. |
e2a0f8d
to
be7c114
Compare
Cool. Can we focus on finishing this PR this week or next? @liarco, could you rebase your PR and make sure Travis (2/3) and Fabbot.io is green? |
Oops. Sorry. I see your comment about the CI... Good. Dont make fabbot green =) Could you rebase please? |
be7c114
to
2a10f37
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done. Here are some small things I noticed.
Thank you, it would be a pleasure! :) I'm going to update the repo and check CI (I hope nothing broke badly, but I'll take care of that). Please feel free to tell me if you see anything that can be fixed/improved. |
/cc @welcoMattic would you have some time to check this one please? Maybe it conflicts with your PR related to using an AST? Or maybe you're just the right person to review? 👼 |
// Update old variables note (if any) | ||
if (isset($metadata['notes'])) { | ||
foreach ($metadata['notes'] as $index => $note) { | ||
if (isset($note['category']) && MessageCatalogue::METADATA_AVAILABLE_VARIABLES_KEY === $note['category']) { | ||
// Keep the highest variables count | ||
if (\count($variables) > substr_count($note['content'], ',')) { | ||
$metadata['notes'][$index] = $variablesNote; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} else { | ||
$metadata['notes'][] = $variablesNote; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@liarco this code is repeated several times, shouldn't we factorize it in a separate method, in a new file?
Hi @liarco, thanks a lot for this PR. I've some notes to share,
About these tests, they were fixed in 2 more recent commits (84b48fe#diff-b554ece6142fcdb8b04a4afee7ad8e95400ec5bbc8dd65e5bda8e0196a79f64fR74 and 1802488#diff-b554ece6142fcdb8b04a4afee7ad8e95400ec5bbc8dd65e5bda8e0196a79f64fR75), now I guess it fixes the confusion. Can you rebase your branch please? Beside this, your PR is adding variables extraction feature in A last thing, I think we have to add more tests on Dumpers, to ensure variables notes are correctly written in translation files (not sure that |
@liarco Hi, any news on this feature? As we now have a brand new |
@liarco Any news here? I like this feature, coupled with the new |
Closing as there is no more activity. |
To do:
t()
syntaxes now are broken when domain is set withtrans_default_domain
)Required feedback:
array()
to[]
inside the tests fixtures")About this PR
This PR adds available variables to each translation extracted using
translation:update
. This can be useful to both developers and translators when editing key-based translations directly from the extracted files.Here is an output example:
PHP code:
XLIFF 2.0 output:
Example with Poedit:

New features
t()
functions when used without|trans()
filter (e.g. as variable value, see example below)How to test it
I created a demo project at liarco/translation-variables-extractor-demo.
You can clone it and clone this PR, then run:
The latest command will generate translations from the source files:
src/Controller/TestController.php
: php syntax examples (with and without explicit domain)templates/test/index.html.twig
: twig syntax examples (without explicit domain)templates/test/domain.html.twig
: twig syntax examples (withtrans_default_domain
)You can check the translations running:
symfony serve # now check out `/` and `/domain` to see all translation results...
Strange behaviors in current Symfony code
While working on this PR I found some strange behaviors that I think may be bugs in the current code. I think I fixed them with my code, but they need to be checked. I'm gonna update this section with documentation about the stuff I found.
If my new feature will be rejected I would be happy to revert it while keeping the bug fixes (if there is any).
Possibly broken
--clean
If you test my demo project with original Symfony code:
Update 2021-02-16: my fix was the same as #40050, I merged it to resolve conflicts.
Test that I didn't understand, maybe there's an error?
While I think this test can be useful (I copied it to
MergeOperationTest.php
) I can't understand why it should override the domain while keeping the old message. This would mean loading the old value using a different standard from the one which the translation was written for.This test was introduced in b72b7d3, but I think that it is based on the wrong behavior of
--clean
that I explained above.Current Symfony code doesn't support all syntaxesRunning the extraction tool on the demo project extracts less keys compared to my PR.I was testing this using
5.1
in demo project (t()
function was not supported) so it couldn't extract those keys, but5.x
works fine. Now I updated the demo and added more syntax examples.Update 2020-11-01: I added support for
t()
functions used without|trans()
filter which are not supported by the original implementation.Some test cases have been broken by CS fixes
Original tests were performed against both
array()
and[]
syntaxes: 78c0ec5#diff-296e65f0a4669683ef3d74c37eb9983fcf963a0900e1111ce0d497e3f7e0d065Then they have been changed to fix CS: a23330b#diff-296e65f0a4669683ef3d74c37eb9983fcf963a0900e1111ce0d497e3f7e0d065
Now only the short syntax is tested, but the entries are still there... I reimplemented them.
Fabbot sill wants me to change
array()
to[]
inside the tests fixturessymfony/.php_cs.dist
Line 25 in 176f52d
It seems like the
fixtures
folder should be capitalized to be filtered properly by CS Fixer. But I tried to rename the folder locally and the->notPath('#/Fixtures/#')
line is ignored anyway... I don't know why. (Side note: changing it to->notPath('#Fixtures#')
skips the folder successfully, I know that it can't be a solution, but it means that the method is not completely ignored)