Skip to content

Commit

Permalink
feature #23890 [Translation] Adding the ability do dump <notes> in xl…
Browse files Browse the repository at this point in the history
…iff2.0 (Nyholm)

This PR was squashed before being merged into the 3.4 branch (closes #23890).

Discussion
----------

[Translation] Adding the ability do dump <notes> in xliff2.0

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        | n/a

The Xliff2.0 dumper do not dump notes. The note section is the only place (as far as I can see) you can add arbitrary data. See [specification](http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html).

This will be useful when you want to add data like "approved", "status" etc. I can see from the test code that a previous author intends to use attributes to the <target> for such data. That is [not allowed](http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#target), not even with a custom namespace.

If you want to validate my test fixture, here is the validator: http://okapi-lynx.appspot.com/validation

Commits
-------

c4dd11c [Translation] Adding the ability do dump <notes> in xliff2.0
  • Loading branch information
nicolas-grekas committed Aug 22, 2017
2 parents 3db39f4 + c4dd11c commit b2fea88
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Added `TranslationDumperPass`
* Added `TranslationExtractorPass`
* Added `TranslatorPass`
* Added <notes> section to the Xliff 2.0 dumper.

3.2.0
-----
Expand Down
18 changes: 17 additions & 1 deletion src/Symfony/Component/Translation/Dumper/XliffFileDumper.php
Expand Up @@ -146,6 +146,23 @@ private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain,
foreach ($messages->all($domain) as $source => $target) {
$translation = $dom->createElement('unit');
$translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
$metadata = $messages->getMetadata($source, $domain);

// Add notes section
if ($this->hasMetadataArrayInfo('notes', $metadata)) {
$notesElement = $dom->createElement('notes');
foreach ($metadata['notes'] as $note) {
$n = $dom->createElement('note');
$n->appendChild($dom->createTextNode(isset($note['content']) ? $note['content'] : ''));
unset($note['content']);

foreach ($note as $name => $value) {
$n->setAttribute($name, $value);
}
$notesElement->appendChild($n);
}
$translation->appendChild($notesElement);
}

$segment = $translation->appendChild($dom->createElement('segment'));

Expand All @@ -156,7 +173,6 @@ private function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain,
$text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode($target);

$targetElement = $dom->createElement('target');
$metadata = $messages->getMetadata($source, $domain);
if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
foreach ($metadata['target-attributes'] as $name => $value) {
$targetElement->setAttribute($name, $value);
Expand Down
Expand Up @@ -87,4 +87,24 @@ public function testFormatCatalogueWithTargetAttributesMetadata()
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR'))
);
}

public function testFormatCatalogueWithNotesMetadata()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
'foo' => 'bar',
));
$catalogue->setMetadata('foo', array('notes' => array(
array('category' => 'state', 'content' => 'new'),
array('category' => 'approved', 'content' => 'true'),
array('category' => 'section', 'content' => 'user login', 'priority' => '1'),
)));

$dumper = new XliffFileDumper();

$this->assertStringEqualsFile(
__DIR__.'/../fixtures/resources-notes-meta.xlf',
$dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR', 'xliff_version' => '2.0'))
);
}
}
@@ -0,0 +1,16 @@
<?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>foo</source>
<target>bar</target>
</segment>
</unit>
</file>
</xliff>

0 comments on commit b2fea88

Please sign in to comment.