2

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
mate64
  • 9,876
  • 17
  • 64
  • 96
  • 1
    how about using translation placeholders? {{'I am%br% Joe Shmoe'|trans({%br%:''})}} – joe42 Jan 29 '14 at 16:46
  • @joe42 +1 Very smooth *injection*, but it's too complicated to implement this within 500+ language vars. – mate64 Jan 29 '14 at 17:10
  • You can just do `
    `, I guess the xliff format is not that good implemented in Symfony (only the basic functions)
    – Wouter J Jan 29 '14 at 17:55
  • `` isn't deprecated in the XLIFF 1.2 specification (http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#x) which most of the translation agencies we work with still use as their standard format. According to http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#Struct_InLine your `ctype` should be `x-html-br` though. – flu Aug 24 '16 at 11:19

3 Answers3

14

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'/>

Community
  • 1
  • 1
greg0ire
  • 22,714
  • 16
  • 72
  • 101
  • Thank you for your answer. According to the [**XLIFF 1.2 specification**](http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#x), `` is a valid tag and represents a *placeholder*. – mate64 Jan 29 '14 at 21:19
  • Thank but I dont think if this is a best practice to put those odd CDATA characters into translation files for every single translation. There should be some other implementations like `nl2br` for Xliff – Peyman Mohamadpour Apr 22 '17 at 13:50
  • @Trix that will work for `
    `, but not for anything else. "those odd CDATA characters" are basic XML for escaping something, which is precisely what we want here. It is very much the best practice IMO
    – greg0ire Apr 26 '17 at 09:47
  • If the content is showing as a tooltip, `data-html="true"` attribute should be added. ` – Dipu Raj Jan 17 '23 at 08:41
5

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>
1

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>
Chase T
  • 116
  • 5