Symfony2 translation of long texts

Andrea picture Andrea · Mar 26, 2012 · Viewed 8.6k times · Source

Since a few weeks I started playing with Symfony2. It seems a very powerful framework, but there are some things I cannot still understand.

In documentation I see that i18n (i.e. Translations) is managed by the Translator service. If I correctly understood, the main way to have a website translated is to put the collection of messages I want to translate inside the different files messages.XX.yml (XX=en,fr,it,etc...), one for each language.

This could be perfect for short texts, which possibly do not include any HTML markup. But how do you deal with long text? For instance, how can I manage the translation of a Terms Of Service or an About page?

I guess I should include different templates for each locale I want to use. Am I right?

Thanks for your help!

Answer

Kosta picture Kosta · Mar 27, 2012

You can have long texts in .yml translation file as well as html tags. Put your Terms Of Service text in messages.xx.yml file like this:

TermsOfServiceText: >
  <p>Here goes my Terms of service code</p>
  <p>It can be put in several lines and <strong>can include html tags!</strong></p>
  <p>It can also include <a href="http://symfony.com/doc/current/book/translation.html" rel="nofollow">links</a></p>
  <p>Just make sure that you put '>' sign after your translation keyword like in the first line of this example code 
  and start your message in next line with double space indentation</p>

Now, in your twig template call translation with this:

{{ 'TermsOfServiceText'|trans|raw }}

raw is used to skip escaping html tags.