Ensure that Symfony Translation always works reliably with Twig templates#1908
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1908 +/- ##
============================================
- Coverage 70.38% 70.32% -0.07%
Complexity 1658 1658
============================================
Files 34 34
Lines 4356 4364 +8
============================================
+ Hits 3066 3069 +3
- Misses 1290 1295 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
2a20ee5 to
9e7e10d
Compare
|
|
In my view, the problem is not that extraction from Twig templates doesn't work at all but that the extraction is unreliable. It works for some Twig templates but not for others, and I can't figure out what makes the difference. If I run the command it shows a number of messages as unused, including these: yet they are clearly used in Twig templates: This PR makes no difference to the behavior. These two messages, and many others, are still listed as "unused" even though they clearly are being used and translated in the Twig templates. |
How to verify Symfony Translation limitationsSkosmos uses custom Twig filters (global_url, link_url) and a "non-standard" template structure: src/view/ instead of templates/. While GitHub: symfony/symfony#29085 @xabbuh (Symfony Member) on Nov 5, 2018: @nicolas-grekas (Symfony Member) on Nov 6, 2018: These are not configuration errors. They are architectural limitations in Symfony's translation tools (at least in the version 6.4 we are using). The following tests you can follow/repeat aims to prove limitations:1. Custom Twig ExtensionsTwigExtractor fails to parse templates containing custom filter expressions skipping all subsequent translations. Extract translations
Simple template: All 3 translations found (A, B, C)
2. Template Inheritance + Custom Filters
Find templates using inheritance
Verify base-template.twig has custom filters and translationsConfirm extraction works (finds all translations)
But debug:translation shows false "unused" warnings
Expected result:
Why? 3. Non-Standard Template StructureSymfony propably expects templates directory but Skosmos uses src/view. Extraction requires explicit configuration. ! This could not be verified for 100 precent sure but the custom filters are still a problem. Check current configuration
Create test in "standard" locationBefore modifications: Attempt extraction
Result: templates/ directory not scanned, 0 translations found After the modifications: Edit the config/twig.yaml (add the "paths") Re-attempt extraction
templates directory scanned, translations found OK Explanation: Templates outside the "Symfony bundle" require explicit paths configuration. Maybe: Even with configuration, Does standard structure fix custom filter issues?Test if standard location + paths config helps with custom filters: Create template in standard location with custom filterExtract with paths configured
Expected result: Still 0 translations found Re-create template in standard location withOut custom filterExtract
WHY?: Custom filter parse failures occur regardless of template location. Standard structure with paths config does bot fix the custom filter limitation - they are separate issues. Cleanup: SummaryAll three limitations were at least partly verified (custom filter case 100 percently):
Summa summarum
Conclusion The false "unused" warnings in The recommended approach: rely on |



Reasons for creating this PR
Symfony extract did not work with Twig templates
Link to relevant issue(s), if any
Description of the changes in this PR
Added a paths setting to the configuration
How to review
Edit: src/view/about.twig
After
<h1 class="mb-5">{{"About" | trans }}</h1>add the following:
<p>{{ "This is a test translation key 1"|trans }}</p>and save
Use
bin/console debug:translation en | grep -i "test translation"to see that the key is used in the code but missing from the translation files.
Extract:
bin/extract-translations
Re-run the following
bin/console debug:translation en | grep -i "test translation"to see that it is not missing anymore
Verify:
grep 'This is a test translation key' resource/translations/messages.en.jsonKnown problems or uncertainties in this PR
Checklist
.sr-onlyclass, color contrast)