Skip to content

Don't overwrap WordPress i18n functions#1753

Merged
jarednova merged 2 commits intotimber:masterfrom
drzraf:direct-twig-func
Jul 26, 2018
Merged

Don't overwrap WordPress i18n functions#1753
jarednova merged 2 commits intotimber:masterfrom
drzraf:direct-twig-func

Conversation

@drzraf
Copy link

@drzraf drzraf commented Jul 20, 2018

Ticket: #

Issue

Normally, when the argument of a Twig Function is a Callable, Twig creates such call (see Twig's compileCallable()).
Eg:

{{__("foo")}}
=> echo twig_escape_filter($this->env, call_user_func_array($this->env->getFunction('__')->getCallable(), array("foo")), "html", null, true)

This has the side effect that resulting template loose the xgettext friendly syntax.

Solution

By feeding string to Twig Function, the result is clearer (to xgettext):

{{__("foo")}}
=> echo twig_escape_filter($this->env, __("foo"), "html", null, true);

Impact

Since wrapping these functions is of no use (same arguments), and since compiled templates containing these functions may very well have to be looked into for gettext*() function calls, it's better to avoid it.
No impact is expected (other than reducing function calls)

Usage Changes

no

Considerations

Testing

no

Normally, when the argument of a Twig Function is a Callable, Twig creates such call (see Twig's `compileCallable()`).
Eg:
```
{{__("foo")}}
=> echo twig_escape_filter($this->env, call_user_func_array($this->env->getFunction('__')->getCallable(), array("foo")), "html", null, true)
```

This has the side effect that effect resulting template loose the xgettext friendly syntax.

By using feeding string to Twig Function, the result is clearer (to xgettext):
```
{{__("foo")}}
=> echo twig_escape_filter($this->env, __("foo"), "html", null, true);
````

Since wrapping these functions is of no use (same arguments), and since compiled templates containing these functions
 may very well have to be looked into for gettext*() function calls, it's better to avoid it.
@coveralls
Copy link

coveralls commented Jul 20, 2018

Coverage Status

Coverage increased (+0.1%) to 94.302% when pulling 9c77cd6 on drzraf:direct-twig-func into 5568143 on timber:master.

@codecov
Copy link

codecov bot commented Jul 20, 2018

Codecov Report

Merging #1753 into master will increase coverage by 0.07%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1753      +/-   ##
============================================
+ Coverage     94.83%   94.91%   +0.07%     
+ Complexity     1536     1525      -11     
============================================
  Files            48       48              
  Lines          3604     3582      -22     
============================================
- Hits           3418     3400      -18     
+ Misses          186      182       -4
Impacted Files Coverage Δ Complexity Δ
lib/Twig.php 99.43% <100%> (+1.95%) 83 <0> (-11) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5568143...9c77cd6. Read the comment docs.

jarednova added a commit that referenced this pull request Jul 26, 2018
@jarednova jarednova mentioned this pull request Jul 26, 2018
@jarednova jarednova merged commit 9c77cd6 into timber:master Jul 26, 2018
@jarednova
Copy link
Member

WOW is that cleaner. I added a test in #1759 to confirm everything still works — and it does. Merged!

drzraf pushed a commit to drzraf/Gettext that referenced this pull request Oct 28, 2018
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants