Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2610da7

Browse filesBrowse files
committed
bug #26247 [Translation] Process multiple segments within a single unit. (timewasted)
This PR was submitted for the master branch but it was squashed and merged into the 3.4 branch instead (closes #26247). Discussion ---------- [Translation] Process multiple segments within a single unit. | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Xliff 2.0 supports multiple segments within a single unit, but the loader was only processing the first segment. The loader will now correctly process all segments within a unit. Commits ------- ac5d01f [Translation] Process multiple segments within a single unit.
2 parents 4183e93 + ac5d01f commit 2610da7
Copy full SHA for 2610da7

File tree

3 files changed

+69
-22
lines changed
Filter options

3 files changed

+69
-22
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Loader/XliffFileLoader.php
+23-22Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php
+29Lines 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
}
+17Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.