0

I tried the answers of this question, but it's not working. It's weird and I cannot reproduce the behavior:

  • I cleared the cache
  • Concat works (it output's the right string)
  • The concated string as a variable is not translateable
  • The concated string pasted as a string is translateable
  • Edit: obj2arr casts the object to an array, to make it iterable. prepareForTwig is just using trim(), etc. - the string is outputted correctly.
  • Edit 2: {% set transVar = (key|prepareForTwig) %} (without prefix) doesn't work as well.

yml:

# Resources/translations/messages.en.yml
my:
   keywords:
       keyword1: K1
       keyword2: K2
       # ...

twig:

{# my.twig.html #}
{% for key, value in data|obj2arr %}
    {% set transVar = 'my.keywords.' ~ (key|prepareForTwig)) %}

    {{ transVar }}<br/>                    {# output, e.g.: my.keywords.keyword1 #}
    {{ transVar|trans}}<br/>               {# output, e.g.: my.keywords.keyword1 #}  
    {{ 'my.keywords.keyword1'|trans }}     {# output: K1 #}
{% endfor %}

EDIT:

CustomTwigExtension.php

class CustomTwigExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('obj2arr', array($this, 'obj2arrFilter')),
            new \Twig_SimpleFilter('prepareForTwig', array($this, 'prepareForTwigFilter')),
        );
    }

    public function obj2arrFilter($obj)
    {
        return (array)$obj;
    }

    public function prepareForTwigFilter($str) {
        $str = trim($str);
        $str = strtolower($str);
        $str = substr($str, 2, strlen($str)); // obj2arr() prefixes "*"

        return $str;
    }

    public function getName()
    {
        return 'custom_twig_extension';
    }
}

Thanks in advance!

Community
  • 1
  • 1
Mr. B.
  • 8,041
  • 14
  • 67
  • 117
  • 1
    Did you tried using parentesis, like `{{ (transVar) | trans }}`? – Artamiel Oct 19 '15 at 08:50
  • @Artamiel Yes, I tried, thanks. Unfortunately same result. – Mr. B. Oct 19 '15 at 08:55
  • 1
    Be sure to check twice. I myself tried it right now with `{{ ('hello.world.' ~ myVar) | trans }}` where `myVar` is set to `'message'` and it got translated correctly. – Artamiel Oct 19 '15 at 09:02
  • @Artamiel The problem is not the syntax, I guess. If I paste the concated string, it works. Maybe `obj2arr` and `prepareForTwig` are responsible for the weird issue. But I have no idea how this could affect the translation. The outputted, concated string is right. – Mr. B. Oct 19 '15 at 09:09
  • 1
    The issue happens because you are trying to have twig evaluate a variable and then use it as a twig construct. This is usally not available, but you can make it work using Twig's template_from_string function: http://twig.sensiolabs.org/doc/functions/template_from_string.html – Carlos Granados Oct 19 '15 at 09:13
  • @CarlosGranados I tried `{{ include(template_from_string("{{ transVar|trans }}")) }}` - same result as above. Did you mean it this way? – Mr. B. Oct 19 '15 at 09:21
  • 1
    What happens if you try it this way - `{% set transVar = key | prepareForTwig %}` and then output it like `{{ 'my.keywords.' ~ transVar | trans }}` – Artamiel Oct 19 '15 at 09:23
  • 1
    Use {{ include(template_from_string("{{ '" ~ transVar ~ "'|trans }}")) }} – Carlos Granados Oct 19 '15 at 09:25
  • @Artamiel same result. There must be any black magic bug. – Mr. B. Oct 19 '15 at 09:27
  • @CarlosGranados same result as well. – Mr. B. Oct 19 '15 at 09:28
  • 1
    This last solution should work. Have you copied it exactly as I input it? Notice that there is a ' character within the " character, it is not easy to see. – Carlos Granados Oct 19 '15 at 09:33
  • 1
    Also, did you activate the template_from_string function? It is not active by default – Carlos Granados Oct 19 '15 at 09:34
  • 1
    You custom twig extension has nothing to do with this problem – Carlos Granados Oct 19 '15 at 09:35
  • @CarlosGranados Yes, I copied your code and activated the function. I just added the code of my custom twig extension. Maybe there's anything wrong (unlikely). – Mr. B. Oct 19 '15 at 09:36

2 Answers2

5

Others examples didn't seem to work in my case, this is what I ended up using:

{{ "my.prefix.#{ myVariable }.my.postfix" | trans }}

The translation string has to be between double quotes.

Rmy5
  • 527
  • 8
  • 21
1

Every answer of outputting concated translationstrings was right, example:

{% 'my.prefix.' ~ extendByKeyword | trans }}

The problem was a weird generated space:

  • (array) $obj added * (there are two spaces after the star) as a prefix to the key-variable.
  • I used substr() to get rid of the * (star + 1 space), but didn't notice/expect the second space.
  • After comparing the strings with strlen(), I found the cause.

Thanks to @Artamiel and @CarlosGranados for your help.

Mr. B.
  • 8,041
  • 14
  • 67
  • 117