Problem/Motivation
#2289999: Add an easy way to create HTML on the fly without having to create a theme function / template introduced not one but two ways to put inline TWIG templates in Drupal code. Either may be used to add translatable text and would need to somehow parse them. Not yet sure how, we need to rely on the TWIG parser to parse the inline text AFAIS...
Proposed resolution
1. Figure out what are the supported forms.
2. Build support.
3. Add tests.
Remaining tasks
Do it.
Comment | File | Size | Author |
---|---|---|---|
#11 | potx-process-inline-template-2315329-11.patch | 4.8 KB | herom |
#11 | interdiff-2315329-8-11.txt | 2.02 KB | herom |
Comments
Comment #1
Gábor HojtsyLooks like both the inline_template and the renderInline() would need specific support in potx. We are not looking at PHP files for Twig translatable text of course. So we need reliable ways to identify these template fragments and then parse them as Twig. The change notice explains two forms: https://www.drupal.org/node/2311123, can we rely on such specific things as an array defined all at once with type key before the template key? Ie. if you define it the other way around it will not be translatable? Consider we need to statically parse this PHP code to then statically parse the Twig code.
Comment #2
Fabianx CreditAttribution: Fabianx commentedComment #3
Fabianx CreditAttribution: Fabianx commentedOnce #2308215: Create a script to compile all twig template files and inlines is solved, this one can re-use that work as the compiled template can be parsed for t() as usual. Consider co-ordinating that effort with chx.
Comment #4
Gábor Hojtsy@Fabianx: potx does not currently compile the template, we essentially do this:
Probably much quicker than making TWIG do the lexing, code generation and then read back the file from somewhere temp. We could theoretically use a copy of part of the logic from #2308215: Create a script to compile all twig template files and inlines.
Comment #5
herom CreditAttribution: herom commentedjust noting that we should be careful not to skip too much stuff while parsing here.
t()
calls might come in-between the render array items.example from #2309215: HTML double-escaping in revision messages:
Comment #6
Gábor HojtsyRight, we already find those t()s I believe. Those should not be an issue or need any more special treatment. Maybe we want to add some tests for them, but I'm not sure it is necessary even.
Comment #7
herom CreditAttribution: herom commentedHere is a patch.
I have implemented this in the simplest form: it searches for the
'#template' => '...'
pattern, and feeds the template string to the potx twig parser.Running this inline template parser on Drupal 8 core generated 6 string, which I have also attached here.
Comment #8
herom CreditAttribution: herom commentedAdding support for
['#template'] = '...'
was also simple to do. I think that should be enough to do. There is also tests for both formats.Comment #9
Fabianx CreditAttribution: Fabianx commentedI would RTBC that, but I did not test, but only reviewed the code.
So just a RTBC + 1 from me.
Comment #10
Gábor HojtsyLooks good except:
I suspect this would not work with "#template" (double quotes as opposed to single quotes). It is true our code style suggest single quotes, but should we not recognise the double quote variant at all?
Comment #11
herom CreditAttribution: herom commentedYeah, we should support "#template" too, but I had missed it. Here's a new patch.
Thanks for the reviews.
Comment #12
Gábor HojtsyLooks good to me :) Except:
Let's add a comment something like "Normalise template key token to support both quotes". Or something along those lines.
Marking RTBC, feel free to commit with that fix to both branches :)
Comment #15
herom CreditAttribution: herom commentedAdded this one-line comment suggested by #12, and committed.