2

I want to add some HTML to Symfony 2 Translation. This way I will know which phrases in my application are translated and which not. I found in "Symfony\Component\Translation\Translator.php" function "trans". Now i want to add something in function return, for example "< /br>":

/**
 * {@inheritdoc}
 *
 * @api
 */
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
    if (null === $locale) {
        $locale = $this->getLocale();
    } else {
        $this->assertValidLocale($locale);
    }

    if (null === $domain) {
        $domain = 'messages';
    }

    if (!isset($this->catalogues[$locale])) {
        $this->loadCatalogue($locale);
    }

    return strtr($this->catalogues[$locale]->get((string) $id, $domain)."</br>", $parameters);
}

The issue is that when I run my application I'm getting for example "Tag< / b r>" (I have add spaces because in normal way it doesn't show here. HTML doesn't interprate this as HTML code but as a string. Is there any way to achieve what I want ? Maybe it is but in the other way ?

Michal Olszowski
  • 795
  • 1
  • 8
  • 25
  • 1
    maybe using raw filter when displaying. Ex : `{{ my_translated_var|raw }}` – Mario Radomanana Jul 17 '15 at 14:15
  • Translations are used not only in Twig but in Javascript and PHPs Controllers too. So this way would solve this problem only in some part and I would have to change it everywhere. I want to change that only in one function for Devoloper Environment. – Michal Olszowski Jul 17 '15 at 14:49

3 Answers3

1

This happens because you have the Twig Escaper extension active. That extension adds automatic output escaping to Twig, it defines the autoescape tag and the raw filter.

So I think the best option you got here is to define a new twig extension to let you translate your html strings without having to repeat myvar|raw each time.

To see how it is possible to create a new Twig extension please check the docs here.

Use the same extension when escaping for JS and there should be no need to use anything else especially in your PHP controllers. That's because the escaping is done at the Twig level. Just remember to declare your new Twig filter as safe to avoid automatic escaping again:

$filter = new Twig_SimpleFilter('nl2br', 'nl2br', array('is_safe' => array('html')));

If you need to do some extra processing with the requested data so that you can track what strings are being requested and what not then just declare a new service as a proxy to the Symfony translation one. Your Twig extension can use the same service. This way you can converge all the requests to one single service.

Here a few useful links for you:

Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
  • It looks very interesting. I will try to do it today, starting now. I will let you know what I will achieve ;-) – Michal Olszowski Jul 20 '15 at 08:23
  • I have a question. How my nl2br should look like, that's first one. And how I can declare twig filter to be used always without adding "|nl2br" part. – Michal Olszowski Jul 20 '15 at 08:36
  • I guess your `nl2br` should call a service you should create to handle the translations the way you need. Your new Twig extension will be a class after all so you can use the Dependency Injection pattern to handle the dependencies there and provide the service I mentioned. About avoiding the `|nl2br` part I don't think it's something doable with an extension. You can either disable the autoescaping feature http://twig.sensiolabs.org/doc/tags/autoescape.html or use a macro http://twig.sensiolabs.org/doc/tags/macro.html (template data returned by `macros` and `parent` are considered safe) – Francesco Casula Jul 20 '15 at 09:26
  • I have done in it in a little different way but your answer helped me a lot. – Michal Olszowski Jul 20 '15 at 10:20
0

I'll suggest you simply to use Markdown in your translation.
Then you can parse your translated message with a Markdown parser.

Example in Twig: 'my.message'|trans|markdown (I suppose you have a Markdown filter, there is KnpMarkdownBundle)

Yassine Guedidi
  • 1,695
  • 11
  • 12
  • As far as I understand it will work only for Twig translates. So this way would solve this problem only in some part and I would have to change it everywhere. I want to change that only in one function for Devoloper Environment and I don't want to change anything in my application. – Michal Olszowski Jul 19 '15 at 18:41
0

MY SOLUTION:

I have achieved what I wanted in few steps:

  1. Override Translator.php and change translator.class in parameters.yml

    public function trans($id, $parameters, $domain, $locale) { $return = parent::trans($id, $parameters, $domain, $locale); return "".$return.''); }

  2. Set translation class in your css.

  3. Set autoescape option to false in parameters.yml

    twig: autoescape: false

Michal Olszowski
  • 795
  • 1
  • 8
  • 25