8000 feature #23947 [Translation] Adding the ability do load <notes> in xl… · symfony/symfony@3b6442f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b6442f

Browse files
committed
feature #23947 [Translation] Adding the ability do load <notes> in xliff2.0 (Nyholm)
This PR was squashed before being merged into the 3.4 branch (closes #23947). Discussion ---------- [Translation] Adding the ability do load <notes> in xliff2.0 | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | ~~~~yes~~~ no (sorry) | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Since #23890 we can dump `<notes>`. We should also have the ability to load them back. Commits ------- b0cdb53 [Translation] Adding the ability do load <notes> in xliff2.0
2 parents 67abb80 + b0cdb53 commit 3b6442f

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Added `TranslationExtractorPass`
99
* Added `TranslatorPass`
1010
* Added <notes> section to the Xliff 2.0 dumper.
11+
* Improved Xliff 2.0 loader to load <notes> section.
1112
* Added `TranslationWriterInterface`
1213
* Deprecated `TranslationWriter::writeTranslations` in favor of `TranslationWriter::write`
1314

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $
127127

128128
$xml->registerXPathNamespace('xliff< 8000 /span>', 'urn:oasis:names:tc:xliff:document:2.0');
129129

130-
foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) {
130+
foreach ($xml->xpath('//xliff:unit') as $unit) {
131+
$segment = $unit->segment;
131132
$source = $segment->source;
132133

133134
// If the xlf file has another encoding specified, try to convert it because
@@ -144,6 +145,18 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $
144145
}
145146
}
146147

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;
157+
}
158+
}
159+
147160
$catalogue->setMetadata((string) $source, $metadata, $domain);
148161
}
149162
}

src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumperTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,17 @@ public function testFormatCatalogueWithNotesMetadata()
9393
$catalogue = new MessageCatalogue('en_US');
9494
$catalogue->add(array(
9595
'foo' => 'bar',
96+
'baz' => 'biz',
9697
));
9798
$catalogue->setMetadata('foo', array('notes' => array(
9899
array('category' => 'state', 'content' => 'new'),
99100
array('category' => 'approved', 'content' => 'true'),
100101
array('category' => 'section', 'content' => 'user login', 'priority' => '1'),
101102
)));
103+
$catalogue->setMetadata('baz', array('notes' => array(
104+
array('id' => 'x', 'content' => 'x_content'),
105+
array('appliesTo' => 'target', 'category' => 'quality', 'content' => 'Fuzzy'),
106+
)));
102107

103108
$dumper = new XliffFileDumper();
104109

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,44 @@ public function testLoadVersion2()
188188
// target attributes
189189
$this->assertEquals(array('target-attributes' => array('order' => 1)), $catalogue->getMetadata('bar', 'domain1'));
190190
}
191+
192+
public function testLoadVersion2WithNoteMeta()
193+
{
194+
$loader = new XliffFileLoader();
195+
$resource = __DIR__.'/../fixtures/resources-notes-meta.xlf';
196+
$catalogue = $loader->load($resource, 'en', 'domain1');
197+
198+
$this->assertEquals('en', $catalogue->getLocale());
199+
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
200+
$this->assertSame(array(), libxml_get_errors());
201+
202+
// test for "foo" metadata
203+
$this->assertTrue($catalogue->defines('foo', 'domain1'));
204+
$metadata = $catalogue->getMetadata('foo', 'domain1');
205+
$this->assertNotEmpty($metadata);
206+
$this->assertCount(3, $metadata['notes']);
207+
208+
$this->assertEquals('state', $metadata['notes'][0]['category']);
209+
$this->assertEquals('new', $metadata['notes'][0]['content']);
210+
211+
$this->assertEquals('approved', $metadata['notes'][1]['category']);
212+
$this->assertEquals('true', $metadata['notes'][1]['content']);
213+
214+
$this->assertEquals('section', $metadata['notes'][2]['category']);
215+
$this->assertEquals('1', $metadata['notes'][2]['priority']);
216+
$this->assertEquals('user login', $metadata['notes'][2]['content']);
217+
218+
// test for "baz" metadata
219+
$this->assertTrue($catalogue->defines('baz', 'domain1'));
220+
$metadata = $catalogue->getMetadata('baz', 'domain1');
221+
$this->assertNotEmpty($metadata);
222+
$this->assertCount(2, $metadata['notes']);
223+
224+
$this->assertEquals('x', $metadata['notes'][0]['id']);
225+
$this->assertEquals('x_content', $metadata['notes'][0]['content']);
226+
227+
$this->assertEquals('target', $metadata['notes'][1]['appliesTo']);
228+
$this->assertEquals('quality', $metadata['notes'][1]['category']);
229+
$this->assertEquals('Fuzzy', $metadata['notes'][1]['content']);
230+
}
191231
}

src/Symfony/Component/Translation/Tests/fixtures/resources-notes-meta.xlf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,15 @@
1212
<target>bar</target>
1313
</segment>
1414
</unit>
15+
<unit id="uqWglk0">
16+
<notes>
17+
<note id="x">x_content</note>
18+
<note appliesTo="target" category="quality">Fuzzy</note>
19+
</notes>
20+
<segment>
21+
<source>baz</source>
22+
<target>biz</target>
23+
</segment>
24+
</unit>
1525
</file>
1626
</xliff>

0 commit comments

Comments
 (0)
0