-
-
Notifications
You must be signed in to change notification settings - Fork 136
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
WIP: Twig AST extractor for Timber #195
Conversation
Current Twig parser operate by 1. Transforming Twig as PHP code = tokenize() + parse() + render() 2. Using a PhpCode extractor (and its specific configuration about function name) Disadvantage: * Twig rendered PHP code can be transformed/wrapped in call_user_func() making that gettext functions undetectable by PhpCode extractor. (See for example timber/timber#1753). * Can't handle templates making use of custom Twig extensions (very common) This patch offer an extractor that: * Parse Twig generated AST tree (= tokenize()+parse()) * Recursively iterate over node three to find function gettext calls. Advantages: * Operating sooner, at the AST level, Twig expressions like `{{ __("foo", "domain") }}` aren't yet wrapped. * More robust because it directly iterates over the AST from Twig parser instead of looking at PHP source-code. * Supports initialized `$twig` environment, thus supporting user-defined extensions? * Possibly more efficient. Ref: wp-cli/i18n-command#59
…ions). Todos: 1. `$options['twig']` being a static variable, we can NOT initialize it in a row with different parameters. Would get a `LogicException: Unable to add function "action" as extensions have already been initialized.` Solution: don't use static variables for this. 2. Translation/Notes comments are not yet supported. 3. Timber needs a tweak to avoid a fatal error: Add, to lib/Timber.php ```php function add_action() { } function apply_filters() {} ``` The issue is that when loaded through composer Timber constructor forcefully calls init() which make use of WP functions.
@oscarotero Extracting over AST without parsing and rendering is imo a huge advantage and they way to go. Especially all the custom Twig extensions are a pain to deal with ...
This would allow us in Wordpress with Twig land to finally automate the extraction process for all our projects and integrate it in our CI pipelines. |
@rubas Yes, I like the idea of extracting gettext entries from Twig templates using its AST, instead the generated php code. I just have some questions: I would rather not add more features to gettext 4, so would you consider migrating to gettext v5? The new version is splited in different packages so we could create a |
Good to know. |
I'm not fully familiarized with Twig and Timber, but the advantages of having multiple packages is that we can create scanners for specific needs. The ideal solution would be having a generic |
You’re right, and I agree. I’d first make a Timber brings together WordPress and Twig. And it is not the only Twig player in WordPress land. There are quite a few plugins that use Twig templates, mainly for admin views. I guess they would also benefit from a |
@gchtr Great. You can take the others scanners as a guide, for example PHP-Scanner:
I think the key here is creating a TwigFunctionsScanner that can parse and return all gettext entries of a twig template. Then, we can create other classes using the same TwigFunctionsScanner but with different configuration. It's really easy to create a |
One thing to keep in mind is that Twig comments (for prefixed translations "notes") are unlikely to be supported because Twig tokenizer does not (currently) create a dedicated token for them that would be available in the AST. |
Ok, I think we can live without comments for now, until Twig add tokens for that. |
Ok. Initial implementation there : https://github.com/drzraf/Twig-Scanner/ but it first needs a tweak wrt #255. |
@drzraf Great! What do you think about transfering that repository into |
I guess you meant |
@drzraf yes, sorry, I mean |
Yes, that's what I tried. And indeed it says:
and trying results in |
Ok, I just send you an invitation to join to the organization |
Closed this and keep working at php-gettext/Twig-Scanner#1 |
Current Twig parser operates by
The disadvantages are:
(See for example Don't overwrap WordPress i18n functions timber/timber#1753).
This patchset implements an extractor that:
Advantages:
{{ __("foo", "domain") }}
aren't yet wrapped.$twig
environment, thus supporting user-defined extensions?ToDos:
$options['twig']
being a static variable, we can NOT initialize it in a row with different parameters.Would get a
LogicException: Unable to add function "action" as extensions have already been initialized.
Solution: don't use static variables for this.
Translation/Notes comments are not yet supported.
Timber needs a tweak to avoid a fatal error:
Add, to lib/Timber.php
The issue is that when loaded through composer Timber constructor forcefully calls init()
which make use of WP functions.