Comments (10)
Thanks for putting together a proposal and doing the homework. It is greatly appreciated. 👏 While the tokenization and parsing solution is complex it is the most durable long term solution, as Twig's tokenization API is quite stable. If you put together a pull request with how far you've gotten we can add tests and get a collection of usage scenarios with tested support. If we miss a scenario in the future it can be fixed without regressions.
from twig-view.
Just noticed I didn't answer all your questions.
I'm not quiet sure if this belongs here or if it should be part of the cakephp repo.
I think this repository is a better fit as it has the twig dependency. I think having a separate command name is reasonable given this is a plugin.
from twig-view.
Oh thank you! 😄
I will try to provide a PR!
I came across an issue when using a standalone extract command: it's very likely that a domain.pot
-file already exists (created by the main i18n extract
-command).
Simply overwriting it will make people lose translatable strings from their controllers. Merging/appending isn't really an option, is it?
In my head, the best case scenario would be to have the base command offer some way to add additional parser classes, which would return items to be used in _addTranslation
.
This could be used by other plugins to provide extractors for other filetypes as well.
Does something similar exist anywhere in cakephp already?
from twig-view.
It could lead to old, unused strings still being part of the *.pot-files. Should I care about that?
I don't think so. Users can remove old pot strings manually, or regenerate the pot files from scratch if they want to remove old content.
from twig-view.
And since I don't just want to point my finger, I did some testing.
1. Using Twig directly
I tinkered a bit with Twig since there are already multiple token parsers that could give a good result. But it turns out that this might be a little bit too complicated.
$filePath = '/path/to/a/twig/file';
/** @var \Cake\TwigView\View\TwigView $view */
$view = $this->viewBuilder()->build();
$twig = $view->getTwig();
$templateWrapper = $twig->load($filePath);
$moduleNode = $twig->parse($twig->tokenize($templateWrapper->getSourceContext()));
function listFunctionCalls ($node, array &$list, $twig) {
if (!$node) {
return;
}
if ($node instanceof \Twig\Node\Expression\FunctionExpression) {
$name = $node->getAttribute('name');
if (in_array($name, ['__', '__d'])) {
$parameters = [];
$list[] = [
'name' => $name,
'parameters' => listFunctionParameters($node, $parameters),
];
}
}
foreach ($node as $child) {
listFunctionCalls($child, $list, $twig);
}
}
function listFunctionParameters($node, &$arguments) {
/** @var \Twig\Node\Node $child */
foreach ($node as $child) {
if ($child instanceof \Twig\Node\Expression\ConstantExpression) {
$arguments[] = $child->getAttribute('value');
}
if ($child::class === 'Twig\Node\Node') {
listFunctionParameters($child, $arguments);
}
}
return $arguments;
}
$functions = [];
listFunctionCalls($moduleNode, $functions, $twig);
dd($functions);
(based on https://stackoverflow.com/questions/32614432/how-can-i-analyze-twig-templates-without-rendering-them)
A file with contents
{{ __('Übersetzung von "Erstellen"') }}
{{ __d('menus', 'Übersetzung von "Erstellen" in Domain "menus"') }}
will result in
[
0 => [
'name' => '__',
'parameters' => [
0 => 'Übersetzung von "Erstellen"',
],
],
1 => [
'name' => '__d',
'parameters' => [
0 => 'menus',
1 => 'Übersetzung von "Erstellen" in Domain "menus"',
],
],
]
It will ignore parameters that use concatenation or other expression, like {{ __d('menus', 'Übersetzung von "Erstellen" ' ~ variable ~ ' in Domain "menus"') }}
. This could lead to issues and/or unexpected results.
from twig-view.
2. Overwriting the functions & loading the view
I also tried this approach where I use a special view class
class I18nView extends TwigView {
protected array $functions = [
'__',
'__d',
];
public function initialize (): void {
parent::initialize();
$twig = $this->getTwig();
foreach ($this->functions AS $functionName) {
$twigFunction = new \Twig\TwigFunction($functionName, [$this, $functionName]);
$twig->addFunction($twigFunction);
}
}
public function __ (string $singular, ...$args) {
//Do something with $singular
}
public function __d (string $domain, string $msg, ...$args) {
//Do something with $domain and $msg
}
}
Those methods could be used to remember all passed arguments so that another method, used in the extract command, could access them.
I first thought that would be smart approach but this has too many potential issues.
from twig-view.
3. Don't care
You all got your pitchforks and torches?
Get the file contents and just replace {{
and {%
with <?php
and to the same for the closing tags. After that, just run token_get_all
like before. It works! :D
(I'm sorry)
For those who dare: https://onlinephp.io/c/fca0b
from twig-view.
It will ignore parameters that use concatenation or other expression..
This limitation exists for the parsing of PHP templates too, so I don't it's a problem. Accounting for expressions would be too complicated.
from twig-view.
Simply overwriting it will make people lose translatable strings from their controllers. Merging/appending isn't really an option, is it?
Merging pot files should be possible as message strings act as identifiers.
This could be used by other plugins to provide extractors for other filetypes as well.
Does something similar exist anywhere in cakephp already?
Not yet.
from twig-view.
Merging pot files should be possible as message strings act as identifiers.
It could lead to old, unused strings still being part of the *.pot
-files. Should I care about that?
from twig-view.
Related Issues (16)
- 3.3.0 Enhancement: Fetch extension from one central source
- 5.x Implement Tags for CakePHP View Template Block Builder
- Add missing tests for #233 HOT 2
- Convert TwigTemplateTask & CompileShell to commands
- Beta HOT 3
- Improve helper integration so templates don't need raw HOT 9
- Use function notation for elements and cells HOT 4
- ProfilerExtension is missing DebugTimer::end() call
- Document issues when using View::assign/start/end HOT 7
- Support twig loading templates from plugins and without extensions
- Unknown "getVars" function. HOT 4
- Twig's native format filter is being overwritten HOT 3
- Static Twig environment issue HOT 4
- Link to twig docs broken
- Twig returns error when doing a date filter on a Cake\I18n\Date HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twig-view.