Question

I'm looking for a working solution, to use line-breaks (<br />) in symfony's XLIFF i18n files.

Unfortunately, the default tag <x id='1' ctype='lb'/> seems to get stripped by Twig and/or symfony's XLIFF implementation. The XLIFF format is the recommended format for symfony2, so I'm wondering that there is no single line about line-breaks in symfony's cookbooks ?

<!-- messages.fr.xliff -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="1">
                <source>Je suis Joe Schmoe</source>
                <target>I am<x id="1" ctype="lb"/>Joe Schmoe</target>
            </trans-unit>
        </body>
    </file>
</xliff>

Generic placeholder <x/> (deprecated)

The <x/> element is used to replace any code of the original document. The required id attribute is used to reference the replaced code in the skeleton file. The optional ctype attribute allows you to specify what kind of attribute the placeholder represents;

Source: XLIFF 1.2 specification

Line 1<x id='1' ctype='lb'/>line 2

Edit 2:

The <x/> element is deprecated - you can find this information in the documentation now.

Was it helpful?

Solution

You must use a CDATA section make sure your html tags are not misinterpreted as xliff :

<!-- messages.fr.xliff -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="1">
                <source>Je suis Joe Schmoe</source>
                <target><![CDATA[I am<br />Joe Schmoe]]></target>
            </trans-unit>
        </body>
    </file>
</xliff>

After that, in your template, make sure the html is not escaped, use the raw tag :

{{'Je suis Joe Schmoe' | trans | raw }}

By the way, what the hell is this supposed to be ? <x id='1' ctype='lb'/>

OTHER TIPS

If you only want to use the BR tag

<br />

then just use this:

{{ 'Je suis Joe Schmoe' | trans | nl2br }}

So you can write your translation without html.

<!-- messages.fr.xliff -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="1">
                <source>Je suis Joe Schmoe</source>
                <target>
                  I am
                  Joe Schmoe
                </target>
            </trans-unit>
        </body>
    </file>
</xliff>

I'm not familiar with the XLIFF support in symfony, only XLIFF in general. However, many translation tools that process XLIFF will not handle HTML tags embedded in CDATA well. It is also likely that the <x> tag is being stripped because it's not present in the <source> content. (Many XLIFF tools enforce source/target alignment of inline tags.)

Using literal line breaks may work, although some tools require you to add xml:space="preserve" for this to work, eg:

<trans-unit id="1">
  <source>Je suis Joe Schmoe</source>
  <target xml:space="preserve">
              I am
              Joe Schmoe
  </target>
</trans-unit>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top