Tobias Nyholm
Contributed by Tobias Nyholm in #23890 and #23947

The Symfony Translation component supports lots of formats (YAML, JSON, Qt, CSV, PO/MO, etc.) but it officially recommends to use XLIFF. Symfony implements a subset of the full XLIFF specification and in Symfony 3.4 we added support for another of its features: XLIFF notes.

According to the XLIFF 2.0 specification, notes are "a collection of comments used to store end user readable information and annotations". They are useful for example to store metadata such as the status of a translation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0"
       srcLang="fr-FR" trgLang="en-US">
  <file id="messages.en_US">
    <unit id="LCa0a2j">
      <notes>
        <note category="state">new</note>
        <note category="approved">true</note>
        <note category="section" priority="1">user login</note>
      </notes>
      <segment>
        <source>original-content</source>
        <target>translated-content</target>
      </segment>
    </unit>
  </file>
</xliff>

When using the Symfony framework, translation notes are automatically loaded and saved back. When using the standalone Translation component, call the setMetadata() method of the catalogue and pass the notes as arrays. This is for example the code needed to generate the previous XLIFF file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$catalogue = new MessageCatalogue('en_US');
$catalogue->add([
    'original-content' => 'translated-content',
]);
$catalogue->setMetadata('original-content', ['notes' => [
    ['category' => 'state', 'content' => 'new'],
    ['category' => 'approved', 'content' => 'true'],
    ['category' => 'section', 'content' => 'user login', 'priority' => '1'],
]]);

$dumper = new XliffFileDumper();
$dumper->formatCatalogue($catalogue, 'messages', [
    'default_locale' => 'fr_FR',
    'xliff_version' => '2.0'
]);
Published in #Living on the edge