10000 [Translation] Process multiple segments within a single unit. · symfony/symfony@ac5d01f · GitHub
[go: up one dir, main page]

Skip to content

Commit ac5d01f

Browse files
timewastedfabpot
authored andcommitted
[Translation] Process multiple segments within a single unit.
1 parent 4183e93 commit ac5d01f

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

src/Symfony/Component/Translation/Loader/XliffFileLoader.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,37 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $
128128
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
129129

130130
foreach ($xml->xpath('//xliff:unit') as $unit) {
131-
$segment = $unit->segment;
132-
$source = $segment->source;
131+
foreach ($unit->segment as $segment) {
132+
$source = $segment->source;
133133

134-
// If the xlf file has another encoding specified, try to convert it because
135-
// simple_xml will always return utf-8 encoded values
136-
$target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
134+
// If the xlf file has another encoding specified, try to convert it because
135+
// simple_xml will always return utf-8 encoded values
136+
$target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
137137

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

140-
$metadata = array();
141-
if (isset($segment->target) && $segment->target->attributes()) {
142-
$metadata['target-attributes'] = array();
143-
foreach ($segment->target->attributes() as $key => $value) {
144-
$metadata['target-attributes'][$key] = (string) $value;
140+
$metadata = array();
141+
if (isset($segment->target) && $segment->target->attributes()) {
142+
$metadata['target-attributes'] = array();
143+
foreach ($segment->target->attributes() as $key => $value) {
144+
$metadata['target-attributes'][$key] = (string) $value;
145+
}
145146
}
146-
}
147147

148-
if (isset($unit->notes)) {
149-
$metadata['notes'] = array();
150-
foreach ($unit->notes->note as $noteNode) {
151-
$note = array();
152-
foreach ($noteNode->attributes() as $key => $value) {
153-
$note[$key] = (string) $value;
148+
if (isset($unit->notes)) {
149+
$metadata['notes'] = array();
150+
foreach ($unit->notes->note as $noteNode) {
151+
$note = array();
152+
foreach ($noteNode->attributes() as $key => $value) {
153+
$note[$key] = (string) $value;
154+
}
155+
$note['content'] = (string) $noteNode;
156+
$metadata['notes'][] = $note;
154157
}
155-
$note['content'] = (string) $noteNode;
156-
$metadata['notes'][] = $note;
157158
}
158-
}
159159

160-
$catalogue->setMetadata((string) $source, $metadata, $domain);
160+
$catalogue->setMetadata((string) $source, $metadata, $domain);
161+
}
161162
}
162163
}
163164

src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,33 @@ public function testLoadVersion2WithNoteMeta()
228228
$this->assertEquals('quality', $metadata['notes'][1]['category']);
229229
$this->assertEquals('Fuzzy', $metadata['notes'][1]['content']);
230230
}
231+
232+
public function testLoadVersion2WithMultiSegmentUnit()
233+
{
234+
$loader = new XliffFileLoader();
235+
$resource = __DIR__.'/../fixtures/resources-2.0-multi-segment-unit.xlf';
236+
$catalog = $loader->load($resource, 'en', 'domain1');
237+
238+
$this->assertSame('en', $catalog->getLocale());
239+
$this->assertEquals(array(new FileResource($resource)), $catalog->getResources());
240+
$this->assertFalse(libxml_get_last_error());
241+
242+
// test for "foo" metadata
243+
$this->assertTrue($catalog->defines('foo', 'domain1'));
244+
$metadata = $catalog->getMetadata('foo', 'domain1');
245+
$this->assertNotEmpty($metadata);
246+
$this->assertCount(1, $metadata['notes']);
247+
248+
$this->assertSame('processed', $metadata['notes'][0]['category']);
249+
$this->assertSame('true', $metadata['notes'][0]['content']);
250+
251+
// test for "bar" metadata
252+
$this->assertTrue($catalog->defines('bar', 'domain1'));
253+
$metadata = $catalog->getMetadata('bar', 'domain1');
254+
$this->assertNotEmpty($metadata);
255+
$this->assertCount(1, $metadata['notes']);
256+
257+
$this->assertSame('processed', $metadata['notes'][0]['category']);
258+
$this->assertSame('true', $metadata['notes'][0]['content']);
259+
}
231260
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US" trgLang="en-US">
2+
<file id="f1">
3+
<unit id="1">
4+
<notes>
5+
<note category="processed">true</note>
6+
</notes>
7+
<segment id="id-foo">
8+
<source>foo</source>
9+
<target>foo (translated)</target>
10+
</segment>
11+
<segment id="id-bar">
12+
<source>bar</source>
13+
<target>bar (translated)</target>
14+
</segment>
15+
</unit>
16+
</file>
17+
</xliff>

0 commit comments

Comments
 (0)
0