[Translation] Process multiple segments within a single unit.
This commit is contained in:
parent
4183e93825
commit
ac5d01f0b5
@ -128,36 +128,37 @@ class XliffFileLoader implements LoaderInterface
|
||||
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
|
||||
|
||||
foreach ($xml->xpath('//xliff:unit') as $unit) {
|
||||
$segment = $unit->segment;
|
||||
$source = $segment->source;
|
||||
foreach ($unit->segment as $segment) {
|
||||
$source = $segment->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($segment->target) ? $segment->target : $source), $encoding);
|
||||
// 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($segment->target) ? $segment->target : $source), $encoding);
|
||||
|
||||
$catalogue->set((string) $source, $target, $domain);
|
||||
$catalogue->set((string) $source, $target, $domain);
|
||||
|
||||
$metadata = array();
|
||||
if (isset($segment->target) && $segment->target->attributes()) {
|
||||
$metadata['target-attributes'] = array();
|
||||
foreach ($segment->target->attributes() as $key => $value) {
|
||||
$metadata['target-attributes'][$key] = (string) $value;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($unit->notes)) {
|
||||
$metadata['notes'] = array();
|
||||
foreach ($unit->notes->note as $noteNode) {
|
||||
$note = array();
|
||||
foreach ($noteNode->attributes() as $key => $value) {
|
||||
$note[$key] = (string) $value;
|
||||
$metadata = array();
|
||||
if (isset($segment->target) && $segment->target->attributes()) {
|
||||
$metadata['target-attributes'] = array();
|
||||
foreach ($segment->target->attributes() as $key => $value) {
|
||||
$metadata['target-attributes'][$key] = (string) $value;
|
||||
}
|
||||
$note['content'] = (string) $noteNode;
|
||||
$metadata['notes'][] = $note;
|
||||
}
|
||||
}
|
||||
|
||||
$catalogue->setMetadata((string) $source, $metadata, $domain);
|
||||
if (isset($unit->notes)) {
|
||||
$metadata['notes'] = array();
|
||||
foreach ($unit->notes->note as $noteNode) {
|
||||
$note = array();
|
||||
foreach ($noteNode->attributes() as $key => $value) {
|
||||
$note[$key] = (string) $value;
|
||||
}
|
||||
$note['content'] = (string) $noteNode;
|
||||
$metadata['notes'][] = $note;
|
||||
}
|
||||
}
|
||||
|
||||
$catalogue->setMetadata((string) $source, $metadata, $domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -228,4 +228,33 @@ class XliffFileLoaderTest extends TestCase
|
||||
$this->assertEquals('quality', $metadata['notes'][1]['category']);
|
||||
$this->assertEquals('Fuzzy', $metadata['notes'][1]['content']);
|
||||
}
|
||||
|
||||
public function testLoadVersion2WithMultiSegmentUnit()
|
||||
{
|
||||
$loader = new XliffFileLoader();
|
||||
$resource = __DIR__.'/../fixtures/resources-2.0-multi-segment-unit.xlf';
|
||||
$catalog = $loader->load($resource, 'en', 'domain1');
|
||||
|
||||
$this->assertSame('en', $catalog->getLocale());
|
||||
$this->assertEquals(array(new FileResource($resource)), $catalog->getResources());
|
||||
$this->assertFalse(libxml_get_last_error());
|
||||
|
||||
// test for "foo" metadata
|
||||
$this->assertTrue($catalog->defines('foo', 'domain1'));
|
||||
$metadata = $catalog->getMetadata('foo', 'domain1');
|
||||
$this->assertNotEmpty($metadata);
|
||||
$this->assertCount(1, $metadata['notes']);
|
||||
|
||||
$this->assertSame('processed', $metadata['notes'][0]['category']);
|
||||
$this->assertSame('true', $metadata['notes'][0]['content']);
|
||||
|
||||
// test for "bar" metadata
|
||||
$this->assertTrue($catalog->defines('bar', 'domain1'));
|
||||
$metadata = $catalog->getMetadata('bar', 'domain1');
|
||||
$this->assertNotEmpty($metadata);
|
||||
$this->assertCount(1, $metadata['notes']);
|
||||
|
||||
$this->assertSame('processed', $metadata['notes'][0]['category']);
|
||||
$this->assertSame('true', $metadata['notes'][0]['content']);
|
||||
}
|
||||
}
|
||||
|
17
src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-multi-segment-unit.xlf
vendored
Normal file
17
src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-multi-segment-unit.xlf
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US" trgLang="en-US">
|
||||
<file id="f1">
|
||||
<unit id="1">
|
||||
<notes>
|
||||
<note category="processed">true</note>
|
||||
</notes>
|
||||
<segment id="id-foo">
|
||||
<source>foo</source>
|
||||
<target>foo (translated)</target>
|
||||
</segment>
|
||||
<segment id="id-bar">
|
||||
<source>bar</source>
|
||||
<target>bar (translated)</target>
|
||||
</segment>
|
||||
</unit>
|
||||
</file>
|
||||
</xliff>
|
Reference in New Issue
Block a user