-
Notifications
You must be signed in to change notification settings - Fork 7
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
i18n extract from .twig-files #91
Comments
And since I don't just want to point my finger, I did some testing. 1. Using Twig directlyI 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
will result in
It will ignore parameters that use concatenation or other expression, like |
2. Overwriting the functions & loading the viewI 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. |
3. Don't careYou all got your pitchforks and torches? For those who dare: https://onlinephp.io/c/fca0b |
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. |
Just noticed I didn't answer all your questions.
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. |
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. |
Oh thank you! 😄 I came across an issue when using a standalone extract command: it's very likely that a 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 |
Merging pot files should be possible as message strings act as identifiers.
Not yet. |
It could lead to old, unused strings still being part of the |
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. |
I'm not quiet sure if this belongs here or if it should be part of the cakephp repo.
Currently cakephp's
i18n extract
-command only looks for and reads files with thephp
-extension (https://github.com/cakephp/cakephp/blob/8b5d4b65ca63478bb525042eb95071d6749b5c30/src/Command/I18nExtractCommand.php#L839)This leads to missing translatable strings from
.twig
-files when using thistwig-view
-package.Simply adding the
twig
-extension in the lookup is not sufficient because the_extractTokens()
-method of that command uses php's token_get_all()-function (see https://github.com/cakephp/cakephp/blob/8b5d4b65ca63478bb525042eb95071d6749b5c30/src/Command/I18nExtractCommand.php#L431)The two main questions I have:
Should this package provie a standalone extract command? Would it be possible to extend cakephp's
I18nExtractCommand
?The text was updated successfully, but these errors were encountered: