Skip to content

Commit fa19900

Browse files
authored
Adding Xliff Loader for meta (#13)
* Adding Xliff Loader for meta * Applied changes from StyleCI * Moved fixtures
1 parent 47c6f09 commit fa19900

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

src/Loader/XliffLoader.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,53 @@ public function extractFromContent($content, MessageCatalogue $catalogue, $domai
4949
}
5050

5151
if ('2.0' === $xliffVersion) {
52-
NSA::invokeMethod($this, 'extractXliff2', $dom, $catalogue, $domain);
52+
$this->extractXliff2($dom, $catalogue, $domain);
53+
}
54+
}
55+
56+
/**
57+
* @param \DOMDocument $dom
58+
* @param MessageCatalogue $catalogue
59+
* @param string $domain
60+
*/
61+
private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
62+
{
63+
$xml = simplexml_import_dom($dom);
64+
$encoding = strtoupper($dom->encoding);
65+
66+
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
67+
68+
foreach ($xml->xpath('//xliff:unit') as $unit) {
69+
$segment = $unit->segment;
70+
$source = $segment->source;
71+
72+
// If the xlf file has another encoding specified, try to convert it because
73+
// simple_xml will always return utf-8 encoded values
74+
$target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
75+
76+
$catalogue->set((string) $source, $target, $domain);
77+
78+
$metadata = [];
79+
if (isset($segment->target) && $segment->target->attributes()) {
80+
$metadata['target-attributes'] = [];
81+
foreach ($segment->target->attributes() as $key => $value) {
82+
$metadata['target-attributes'][$key] = (string) $value;
83+
}
84+
}
85+
86+
if (isset($unit->notes)) {
87+
$metadata['notes'] = [];
88+
foreach ($unit->notes->note as $noteNode) {
89+
$note = [];
90+
foreach ($noteNode->attributes() as $key => $value) {
91+
$note[$key] = (string) $value;
92+
}
93+
$note['content'] = (string) $noteNode;
94+
$metadata['notes'][] = $note;
95+
}
96+
}
97+
98+
$catalogue->setMetadata((string) $source, $metadata, $domain);
5399
}
54100
}
55101

@@ -121,4 +167,21 @@ private function getXmlErrors($internalErrors)
121167

122168
return $errors;
123169
}
170+
171+
/**
172+
* Convert a UTF8 string to the specified encoding.
173+
*
174+
* @param string $content String to decode
175+
* @param string $encoding Target encoding
176+
*
177+
* @return string
178+
*/
179+
private function utf8ToCharset($content, $encoding = null)
180+
{
181+
if ('UTF-8' !== $encoding && !empty($encoding)) {
182+
return mb_convert_encoding($content, $encoding, 'UTF-8');
183+
}
184+
185+
return $content;
186+
}
124187
}

tests/Fixtures/meta.en.xlf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
3+
<file id="messages.en_US">
4+
<unit id="LCa0a2j">
5+
<notes>
6+
<note category="state">new</note>
7+
<note category="approved">true</note>
8+
<note category="section" priority="1">user login</note>
9+
</notes>
10+
<segment>
11+
<source>foo</source>
12+
<target>bar</target>
13+
</segment>
14+
</unit>
15+
</file>
16+
</xliff>

tests/Unit/Loader/XliffLoaderTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,30 @@ public function testXliff20()
9292
$this->assertTrue($catalogue->defines('key0'));
9393
$this->assertTrue($catalogue->defines('key1'));
9494
}
95+
96+
public function testXliff20Meta()
97+
{
98+
if (Kernel::VERSION_ID < 20800) {
99+
$this->markTestSkipped('Symfony <2.8 is not supported. ');
100+
}
101+
102+
$content = file_get_contents(__DIR__.'/../../Fixtures/meta.en.xlf');
103+
104+
$catalogue = new MessageCatalogue('en');
105+
(new XliffLoader())->extractFromContent($content, $catalogue, 'messages');
106+
$this->assertTrue($catalogue->defines('foo'));
107+
$metadata = $catalogue->getMetadata('foo');
108+
$this->assertNotEmpty($metadata);
109+
$this->assertCount(3, $metadata['notes']);
110+
111+
$this->assertEquals('state', $metadata['notes'][0]['category']);
112+
$this->assertEquals('new', $metadata['notes'][0]['content']);
113+
114+
$this->assertEquals('approved', $metadata['notes'][1]['category']);
115+
$this->assertEquals('true', $metadata['notes'][1]['content']);
116+
117+
$this->assertEquals('section', $metadata['notes'][2]['category']);
118+
$this->assertEquals('1', $metadata['notes'][2]['priority']);
119+
$this->assertEquals('user login', $metadata['notes'][2]['content']);
120+
}
95121
}

0 commit comments

Comments
 (0)