6
Wack
127d

Any Symfony expert here? I've got quite a tricky question, that I'd love to disqus with another dev.

It's about twig within symfony. I'd like to add a custom node parser, but not for an own tag, but to set some value on each template if it's not set (which can't be done with globals in this case!). I've thought about using a visitor, but from my understanding it gets executed to late, as it requires (probably) to modify the twig AST at compile/parse time.

Comments
  • 0
    Can you describe your use case? Maybe its possible to solve it in another way? Such hacks usually mean that you doing sth wrong
  • 0
    Use th3 process class. (Think that's what it's called) it's for executing commands. I've seen it used to call a node script before
  • 1
    Sorry I only find time to reply now.

    @bashleigh I'm not trying to call an external (node js) scrips but to parse nodes in a parse tree.

    @gintko so we've got a symfony app deployed, let's say it's currently at foo.baz
    We've been asked to put a second one up at bar.baz that is almost a clone. (Different logo, different text, some functionality deactivated, but others should be there, just minor things. Even 99% of the things in the db have to be synced).
    We've therefore decided to use the same codebase and db.
    In the `app/config/parameters.yml` file we added an entry `page` which either holds the value `'foo'` or `'bar'`. This value is then passed to twig as a global variable for some if ... else ... In the frontend.
    The trouble however is with the translations.
    What we'd like to have would be `messages_foo.<lang>.xliff` and `messages_bar.<lang>.xliff`.
    To achieve this we've both tried to set the `default_translation_domain` in a if else, which didn't work. It only generated one even when changing the parameter and clearing the cache it still only generated the same one.
    Since it's not possible to set a default translation domain, as far as I know the only way would be to have a modified twig parser injecting nodes if there's no default translation node in a template. That would have to run before the extraction command, so at compile level.
    Afaik a twig visitor wouldn't work here as it's executed to late (correct me if I'm wrong).

    Our current (suboptimal) setup:
    Develop in branch feature_x, pullrequest master <- feature_x, then two pull requests foo <- master and bar <- master. Adjust translations on foo.com and bar.com, commit the translations to their branches. (Improvements to this procedure very welcome!)

    Edit: some more information: https://github.com/symfony/symfony/...
  • 1
    @Wack do you really need to use tag then?

    What about, if you create twig extension that will pass global variable to translation function? So you have lets say |t filter instead of |trans.

    Even |trans has domain argument, so you can make macro which will pass correct domain automatically.
  • 1
    @gintko would be an option. Damn. Why haven't I thought about that? Do you know if I can extend the current trans filter and tags? Or do I need to have a new one?
  • 0
    @Wack i am not sure, you should check source code how its initialized. Also if creating new filters is not an option, you could try to extend one of the existing translation loaders and prefix domain there, more:
    https://api.symfony.com/4.0/...
  • 1
    @gintko awesome. I'll try that! Thanks a lot for your help!
Your Job Suck?
Get a Better Job
Add Comment