Load original file metadata when loading Xliff 1.2 files
This commit is contained in:
parent
006dacd18d
commit
4073319d0f
@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
4.3.0
|
||||
-----
|
||||
|
||||
* Improved Xliff 1.2 loader to load the original file's metadata
|
||||
|
||||
4.2.0
|
||||
-----
|
||||
|
||||
|
@ -82,8 +82,15 @@ class XliffFileLoader implements LoaderInterface
|
||||
$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) {
|
||||
$namespace = 'urn:oasis:names:tc:xliff:document:1.2';
|
||||
$xml->registerXPathNamespace('xliff', $namespace);
|
||||
|
||||
foreach ($xml->xpath('//xliff:file') as $file) {
|
||||
$fileAttributes = $file->attributes();
|
||||
|
||||
$file->registerXPathNamespace('xliff', $namespace);
|
||||
|
||||
foreach ($file->xpath('.//xliff:trans-unit') as $translation) {
|
||||
$attributes = $translation->attributes();
|
||||
|
||||
if (!(isset($attributes['resname']) || isset($translation->source))) {
|
||||
@ -93,11 +100,15 @@ class XliffFileLoader implements LoaderInterface
|
||||
$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);
|
||||
$target = $this->utf8ToCharset((string) ($translation->target ?? $translation->source), $encoding);
|
||||
|
||||
$catalogue->set((string) $source, $target, $domain);
|
||||
|
||||
$metadata = array();
|
||||
$metadata = array(
|
||||
'file' => array(
|
||||
'original' => (string) $fileAttributes['original'],
|
||||
),
|
||||
);
|
||||
if ($notes = $this->parseNotesMetadata($translation->note, $encoding)) {
|
||||
$metadata['notes'] = $notes;
|
||||
}
|
||||
@ -116,6 +127,7 @@ class XliffFileLoader implements LoaderInterface
|
||||
$catalogue->setMetadata((string) $source, $metadata, $domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, string $domain)
|
||||
{
|
||||
|
@ -84,7 +84,16 @@ class XliffFileLoaderTest extends TestCase
|
||||
|
||||
$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()
|
||||
@ -164,11 +173,41 @@ class XliffFileLoaderTest extends TestCase
|
||||
$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()
|
||||
@ -257,4 +296,30 @@ class XliffFileLoaderTest extends TestCase
|
||||
$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')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
27
src/Symfony/Component/Translation/Tests/fixtures/resources-multi-files.xlf
vendored
Normal file
27
src/Symfony/Component/Translation/Tests/fixtures/resources-multi-files.xlf
vendored
Normal file
@ -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>
|
Reference in New Issue
Block a user