Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Load original file metadata when loading Xliff 1.2 files #29148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.3.0
-----

* Improved Xliff 1.2 loader to load the original file's metadata

4.2.0
-----

Expand Down
60 changes: 36 additions & 24 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,38 +82,50 @@ private function extractXliff1(\DOMDocument $dom, MessageCatalogue $catalogue, s
$xml = simplexml_import_dom($dom);
$encoding = strtoupper($dom->encoding);

$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
$attributes = $translation->attributes();
$namespace = 'urn:oasis:names:tc:xliff:document:1.2';
$xml->registerXPathNamespace('xliff', $namespace);

if (!(isset($attributes['resname']) || isset($translation->source))) {
continue;
}
foreach ($xml->xpath('//xliff:file') as $file) {
$fileAttributes = $file->attributes();

$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
// If the xlf file has another encoding specified, try to convert it because
// simple_xml will always return utf-8 encoded values
$target = $this->utf8ToCharset((string) (isset($translation->target) ? $translation->target : $translation->source), $encoding);
$file->registerXPathNamespace('xliff', $namespace);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to register the namespace again. The registration on the root element should already be enough.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I recall having namespace issues without it.


$catalogue->set((string) $source, $target, $domain);
foreach ($file->xpath('.//xliff:trans-unit') as $translation) {
$attributes = $translation->attributes();

$metadata = array();
if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
$metadata['notes'] = $notes;
}
if (!(isset($attributes['resname']) || isset($translation->source))) {
continue;
}

if (isset($translation->target) && $translation->target->attributes()) {
$metadata['target-attributes'] = array();
foreach ($translation->target->attributes() as $key => $value) {
$metadata['target-attributes'][$key] = (string) $value;
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
// If the xlf file has another encoding specified, try to convert it because
// simple_xml will always return utf-8 encoded values
$target = $this->utf8ToCharset((string) ($translation->target ?? $translation->source), $encoding);

$catalogue->set((string) $source, $target, $domain);

$metadata = array(
'file' => array(
'original' => (string) $fileAttributes['original'],
),
);
if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
$metadata['notes'] = $notes;
}
}

if (isset($attributes['id'])) {
$metadata['id'] = (string) $attributes['id'];
}
if (isset($translation->target) && $translation->target->attributes()) {
$metadata['target-attributes'] = array();
foreach ($translation->target->attributes() as $key => $value) {
$metadata['target-attributes'][$key] = (string) $value;
}
}

$catalogue->setMetadata((string) $source, $metadata, $domain);
if (isset($attributes['id'])) {
$metadata['id'] = (string) $attributes['id'];
}

$catalogue->setMetadata((string) $source, $metadata, $domain);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,16 @@ public function testEncoding()

$this->assertEquals(utf8_decode('föö'), $catalogue->get('bar', 'domain1'));
$this->assertEquals(utf8_decode('bär'), $catalogue->get('foo', 'domain1'));
$this->assertEquals(array('notes' => array(array('content' => utf8_decode('bäz'))), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1'));
$this->assertEquals(
array(
'notes' => array(array('content' => utf8_decode('bäz'))),
'id' => '1',
'file' => array(
'original' => 'file.ext',
),
),
$catalogue->getMetadata('foo', 'domain1')
);
}

public function testTargetAttributesAreStoredCorrectly()
Expand Down Expand Up @@ -164,11 +173,41 @@ public function testLoadNotes()
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/withnote.xlf', 'en', 'domain1');

$this->assertEquals(array('notes' => array(array('priority' => 1, 'content' => 'foo')), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1'));
$this->assertEquals(
array(
'notes' => array(array('priority' => 1, 'content' => 'foo')),
'id' => '1',
'file' => array(
'original' => 'file.ext',
),
),
$catalogue->getMetadata('foo', 'domain1')
);
// message without target
$this->assertEquals(array('notes' => array(array('content' => 'bar', 'from' => 'foo')), 'id' => '2'), $catalogue->getMetadata('extra', 'domain1'));
$this->assertEquals(
array(
'notes' => array(array('content' => 'bar', 'from' => 'foo')),
'id' => '2',
'file' => array(
'original' => 'file.ext',
),
),
$catalogue->getMetadata('extra', 'domain1')
);
// message with empty target
$this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux')), 'id' => '123'), $catalogue->getMetadata('key', 'domain1'));
$this->assertEquals(
array(
'notes' => array(
array('content' => 'baz'),
array('priority' => 2, 'from' => 'bar', 'content' => 'qux'),
),
'id' => '123',
'file' => array(
'original' => 'file.ext',
),
),
$catalogue->getMetadata('key', 'domain1')
);
}

public function testLoadVersion2()
Expand Down Expand Up @@ -257,4 +296,30 @@ public function testLoadVersion2WithMultiSegmentUnit()
$this->assertSame('processed', $metadata['notes'][0]['category']);
$this->assertSame('true', $metadata['notes'][0]['content']);
}

public function testLoadWithMultipleFileNodes()
{
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resources-multi-files.xlf', 'en', 'domain1');

$this->assertEquals(
array(
'id' => '1',
'file' => array(
'original' => 'file.ext',
),
),
$catalogue->getMetadata('foo', 'domain1')
);
$this->assertEquals(
array(
'notes' => array(array('content' => 'note')),
'id' => '4',
'file' => array(
'original' => 'otherfile.ext',
),
),
$catalogue->getMetadata('test', 'domain1')
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>foo</source>
<target>bar</target>
</trans-unit>
</body>
</file>
<file source-language="en" datatype="plaintext" original="otherfile.ext">
<body>
<trans-unit id="2">
<source>extra</source>
</trans-unit>
<trans-unit id="3">
<source>key</source>
<target></target>
</trans-unit>
<trans-unit id="4">
<source>test</source>
<target>with</target>
<note>note</note>
</trans-unit>
</body>
</file>
</xliff>