You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
or "how to add a custom translation domain to urls starting with /admin?"
or "how to add a translation domain node to an existing template in twig?"
or "how to add a custom node to a twig template with a custom NodeVisitor?
the core problem is that it's not possible to define a global translation domain to a base twig file and the domain inherits to all its subtemplates and included templates. the current way is to add {% trans_default_domain 'admin' %} to all admin templates (all of them, each twig file which is used in the admin section).
after some googling (see references below) my conclusio was to use a NodeVisitor to add it to all templates (ModuleNode) where the template path starts with admin/.
is this the way to go or is a NodeVisitor the wrong approach? are there better options? with the node visitor i can't get it to work, what am i doing wrong?
my current node visitor looks like that:
useSymfony\Bridge\Twig\Node\TransDefaultDomainNode;
useTwig\Environment;
useTwig\Node\Expression\ConstantExpression;
useTwig\Node\Node;
useTwig\NodeVisitor\AbstractNodeVisitor;
useTwig\Node\ModuleNode;
finalclassMyNodeVisitorextendsAbstractNodeVisitor
{
privateconstADMIN_BASE_URL = 'admin/';
protectedfunctiondoEnterNode(Node$node, Environment$env): Node
{
//can be `null` if i manipulate the nodes with `getNode` and `setNode`$templateName = $node->getTemplateName();
if ($templateName === null) {
return$node;
}
if (!$node instanceof ModuleNode || !str_starts_with($templateName, self::ADMIN_BASE_URL)) {
return$node;
}
$translationDomainNode = $this->buildTranslationDomainNode();
// add trans domain like that?$node->getNode('constructor_end')->setNode('auto_message_domain_amdin1', $translationDomainNode);
// or like that?//$node->setNode('auto_message_domain_amdin2', $translationDomainNode);return$node;
}
privatefunctionbuildTranslationDomainNode()
{
$expression = newConstantExpression('admin', $node->getTemplateLine());
returnnewTransDefaultDomainNode($expression, $node->getTemplateLine(), null);
}
protectedfunctiondoLeaveNode(Node$node, Environment$env): Node
{
return$node;
}
publicfunctiongetPriority():int
{
return -50;
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
NodeVisitor
?the core problem is that it's not possible to define a global translation domain to a base twig file and the domain inherits to all its subtemplates and included templates. the current way is to add
{% trans_default_domain 'admin' %}
to all admin templates (all of them, each twig file which is used in the admin section).after some googling (see references below) my conclusio was to use a
NodeVisitor
to add it to all templates (ModuleNode
) where the template path starts withadmin/
.is this the way to go or is a NodeVisitor the wrong approach? are there better options? with the node visitor i can't get it to work, what am i doing wrong?
my current node visitor looks like that:
references:
Beta Was this translation helpful? Give feedback.
All reactions