Calling Developers!
We are reenergizing our code contribution process! Learn More

What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Any tips on having translation parameters rendered for form labels?

Options
UKGT7RC7P
UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
edited February 2020 in Slack General

Any tips on having translation parameters rendered for form labels?

Comments

  • Hey Daniel, do you ask about Symfony form class?

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    edited February 2020
    Options

    Basically yes (but in Spryker obvs)

  • You can use value of label as a glossary key:

    'label' => 'glossary.key.here'
    

    It should work

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    but with translation parameters

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    so

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    foo.bar => My translation with name "%name"

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    'label'=> 'foo.bar'

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    form.label|trans({"name":"Daniel"})

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    ok hacky solution was to pass the translation params in form_row(form.field, {'translation_parameters'})

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    those attributes are arbitrarily available in the form template blocks

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    so we could pass it to trans

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    edited February 2020
    Options

    Symfony Forms 4.3 supports this at the backend level: https://symfony.com/blog/new-in-symfony-4-3-improved-form-translation

  • UKHD8KTMF
    UKHD8KTMF Posts: 393 🧑🏻‍🚀 - Cadet
    Options

    Stupid question, why don't you just concatenate or string replace it

  • UKHD8KTMF
    UKHD8KTMF Posts: 393 🧑🏻‍🚀 - Cadet
    Options

    at least for the name example

  • UKHD8KTMF
    UKHD8KTMF Posts: 393 🧑🏻‍🚀 - Cadet
    Options

    or write your own twig function, it should be simple enough

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    edited February 2020
    Options

    the function(well, filter) is |trans(replacements)

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    it was just about getting some values to that function in the form templates

  • UKHD8KTMF
    UKHD8KTMF Posts: 393 🧑🏻‍🚀 - Cadet
    edited February 2020
    Options

    trick I used was to call trans and then do replace

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    that would work, still need to render the label though

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    edited February 2020
    Options

    and it's in the form theme?

  • UKHD8KTMF
    UKHD8KTMF Posts: 393 🧑🏻‍🚀 - Cadet
    Options

    foo.bar => My translation with name "placeholder"

    foo.bar|trans|replace({'placeholder': "Dan"})

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    yeah - assume you are rendering the form manually in location?

  • UKGT7RC7P
    UKGT7RC7P Posts: 538 🧑🏻‍🚀 - Cadet
    Options

    in your example you can just as well foo.bar|trans({'__placeholder__': "Dan"}) I think 🙂

  • UKHD8KTMF
    UKHD8KTMF Posts: 393 🧑🏻‍🚀 - Cadet
    Options

    probably, it is twig 🙂 and I did not do anything advanced for years now