1

How do I translate this in twig? I have this variable in twig that is an array:

$array = [
  ['number' => 7, 'name' => 'foo'],
  ['number' => 8, 'name' => 'bar'],
  ['number' => 10, 'name' => 'baz'],
  // ... and so on and so forth
]

Would like it to be something like this:

The variables are 7 times for foo, 8 times for bar, and 10 times for baz.

Or

The variables are 7 times for foo and 10 times for baz.

Or

The variable is 7 times for foo.

Tried with something like this:

    {% set last = array|last %}
    {% set array = array|slice(0, array.length - 1) %}

    {% trans %}
      <p>The variables are
        {% for i in array %}
          {{ i.number }} times {{ i.name }},
        {% endfor %}
        , and {{ last.number }} number of {{ last.name }}
      .</p>
    {% endtrans %}

The error I am getting is:

The text to be translated with "trans" can only contain references to simple variables

Could also use t() although I'm not sure how as well. Also tried with something like this:

{% for i in array %}
    {% trans %}{{ i.number }} times {{ i.name }},{% endtrans %}
{% endfor %}
{% trans %}, and {{ last.number }} number of {{ last.name }}{% endtrans %}

Although it doesn't look good from the "User interface translation" page as you would have to translate them separately.

Thanks!

0
0

I think it's better to split the content in translatable parts:

  <p>{% trans %}The variables are{% endtrans %}
    {% for i in array %}
      {% trans %}{{ i.number }} times {{ i.name }}{% endtrans %},
    {% endfor %}
    , {% trans %}and {{ last.number }} number of {{ last.name }}{% endtrans %}
  .</p>

Haven't tested this so I'm not sure if it works. Also, you could use trans as a filter, check this question.

5
  • Sorry, I edited my question above. Yeah, tried this one already and it does work although you need to translate them separately as well from the "User interface translation" page. – ancient_bonus Nov 9 '20 at 9:26
  • Alright, but is that a bad thing? Otherwise, the for loop would be in the translatable string exposing it to the user when creating the translation and we all know what happens when we expose such things to users. ;) – Patrick Scheffer Nov 9 '20 at 9:41
  • A sentence to translate should not be splitted into phrases, as users who translate them would miss the context for the translation. This is even worse when the same phrase is used in different sentences (contexts). That is why the general rule is avoiding splitting a sentence to translate. – kiamlaluno Nov 9 '20 at 9:45
  • Even if that sentence contain logics allowing the user to alter that? – Patrick Scheffer Nov 9 '20 at 11:20
  • Yeah, I think the thing is it shouldn't contain any logic inside it but at the same time make it easy for content editors to translate the sentence itself. – ancient_bonus Nov 9 '20 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.