Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf7023b

Browse files
committed
add XliffVersion 2.0
1 parent ead7485 commit bf7023b

File tree

4 files changed

+139
-34
lines changed

4 files changed

+139
-34
lines changed

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

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Translation\Loader;
1313

14+
use DOMDocument;
15+
use InvalidArgumentException;
1416
use Symfony\Component\Config\Util\XmlUtils;
1517
use Symfony\Component\Translation\MessageCatalogue;
1618
use Symfony\Component\Translation\Exception\InvalidResourceException;
@@ -41,46 +43,33 @@ public function load($resource, $locale, $domain = 'messages')
4143
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
4244
}
4345

44-
$dom = $this->parseFile($resource);
45-
$version = $this->getVersion($dom);
46+
try {
47+
$dom = XmlUtils::loadFile($resource);
48+
$version = $this->getVersion($dom);
49+
50+
} catch (InvalidArgumentException $e) {
51+
$message = sprintf('Unable to load "%s": %s', $resource, $e->getMessage());
52+
53+
throw new InvalidResourceException($message, $e->getCode(), $e);
54+
}
55+
4656
$this->validateSchema($dom, $version->getSchema());
4757

4858
$catalogue = new MessageCatalogue($locale);
4959
$version->extract($dom, $catalogue, $domain);
60+
5061
$catalogue->addResource(new FileResource($resource));
5162

5263
return $catalogue;
5364
}
5465

5566
/**
56-
* Parses the given file into a DOMDocument
57-
*
58-
* @param string $file
59-
*
60-
* @throws \RuntimeException
61-
*
62-
* @return \DOMDocument
63-
*
64-
* @throws InvalidResourceException
65-
*/
66-
private function parseFile($file)
67-
{
68-
try {
69-
$dom = XmlUtils::loadFile($file);
70-
} catch (\InvalidArgumentException $e) {
71-
throw new InvalidResourceException(sprintf('Unable to load "%s": %s', $file, $e->getMessage()), $e->getCode(), $e);
72-
}
73-
74-
return $dom;
75-
}
76-
77-
/**
78-
* @param \DOMDocument $dom
67+
* @param DOMDocument $dom
7968
* @param string $schema source of the schema
8069
*
8170
* @throws InvalidResourceException
8271
*/
83-
private function validateSchema(\DOMDocument $dom, $schema)
72+
private function validateSchema(DOMDocument $dom, $schema)
8473
{
8574
$internalErrors = libxml_use_internal_errors(true);
8675

@@ -124,21 +113,25 @@ private function getXmlErrors($internalErrors)
124113
/**
125114
* Detects xliff version from file
126115
*
127-
* @param \DOMDocument $dom
116+
* @param DOMDocument $dom
128117
*
129-
* @throws InvalidResourceException
118+
* @throws InvalidArgumentException
130119
*
131120
* @return XliffVersion\AbstractXliffVersion
132121
*/
133-
private function getVersion(\DOMDocument $dom)
122+
private function getVersion(DOMDocument $dom)
134123
{
135124
$versionNumber = $this->getVersionNumber($dom);
136125

137-
if ('1.2' === $versionNumber) {
138-
return new XliffVersion\XliffVersion12();
126+
switch ($versionNumber) {
127+
case '1.2':
128+
return new XliffVersion\XliffVersion12();
129+
130+
case '2.0':
131+
return new XliffVersion\XliffVersion20();
139132
}
140133

141-
throw new InvalidResourceException(sprintf(
134+
throw new InvalidArgumentException(sprintf(
142135
'No support implemented for loading XLIFF version "%s".',
143136
$versionNumber
144137
));
@@ -148,18 +141,32 @@ private function getVersion(\DOMDocument $dom)
148141
* Gets xliff file version based on the root "version" attribute.
149142
* Defaults to 1.2 for backwards compatibility
150143
*
151-
* @param \DOMDocument $dom
144+
* @param DOMDocument $dom
145+
*
146+
* @throws InvalidArgumentException
152147
*
153148
* @return string
154149
*/
155-
private function getVersionNumber(\DOMDocument $dom)
150+
private function getVersionNumber(DOMDocument $dom)
156151
{
157152
/** @var \DOMNode $xliff */
158153
foreach ($dom->getElementsByTagName('xliff') as $xliff) {
159154
$version = $xliff->attributes->getNamedItem('version');
160155
if ($version) {
161156
return $version->nodeValue;
162157
}
158+
159+
$namespace = $xliff->attributes->getNamedItem('xmlns');
160+
if ($namespace) {
161+
if (substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34) !== 0) {
162+
throw new InvalidArgumentException(sprintf(
163+
'Not a valid XLIFF namespace "%s"',
164+
$namespace
165+
));
166+
}
167+
168+
return substr($namespace, 34);
169+
}
163170
}
164171

165172
// Falls back to v1.2
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Loader\XliffVersion;
13+
14+
use DOMDocument;
15+
use Symfony\Component\Translation\MessageCatalogue;
16+
17+
/**
18+
* XliffVersion20 loads XLIFF files identified with version 2.0
19+
*
20+
* @author Berny Cantos <[email protected]>
21+
*/
22+
class XliffVersion20 extends AbstractXliffVersion
23+
{
24+
/**
25+
* @return string
26+
*/
27+
public function getSchema()
28+
{
29+
$source = file_get_contents(__DIR__ . '/../schema/dic/xliff-core/xliff-core-2.0.xsd');
30+
31+
return $this->fixXmlLocation($source, 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd');
32+
}
33+
34+
/**
35+
* @param DOMDocument $dom
36+
* @param MessageCatalogue $catalogue
37+
* @param string $domain
38+
*/
39+
public function extract(DOMDocument $dom, MessageCatalogue $catalogue, $domain)
40+
{
41+
$xml = simplexml_import_dom($dom);
42+
$encoding = strtoupper($dom->encoding);
43+
44+
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
45+
46+
foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) {
47+
$source = $segment->source;
48+
49+
// If the xlf file has another encoding specified, try to convert it because
50+
// simple_xml will always return utf-8 encoded values
51+
$target = $this->utf8ToCharset((string) $segment->target, $encoding);
52+
53+
$catalogue->set((string) $source, $target, $domain);
54+
}
55+
}
56+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,21 @@ public function testLoadNotes()
139139
$this->assertNull($catalogue->getMetadata('extra', 'domain1'));
140140
$this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux'))), $catalogue->getMetadata('key', 'domain1'));
141141
}
142+
143+
public function testLoadVersion2()
144+
{
145+
$loader = new XliffFileLoader();
146+
$resource = __DIR__.'/../fixtures/resources-2.0.xlf';
147+
$catalogue = $loader->load($resource, 'en', 'domain1');
148+
149+
$this->assertEquals('en', $catalogue->getLocale());
150+
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
151+
$this->assertSame(array(), libxml_get_errors());
152+
153+
$domains = $catalogue->all();
154+
$this->assertCount(3, $domains['domain1']);
155+
156+
// Notes aren't assigned to specific segments, but to whole units, so there's no way to do a mapping
157+
$this->assertEmpty($catalogue->getMetadata());
158+
}
142159
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US" trgLang="ja-JP">
2+
<file id="f1" original="Graphic Example.psd">
3+
<skeleton href="Graphic Example.psd.skl"/>
4+
<unit id="1">
5+
<segment>
6+
<source>Quetzal</source>
7+
<target>Quetzal</target>
8+
</segment>
9+
</unit>
10+
<group id="1">
11+
<unit id="2">
12+
<segment>
13+
<source>An application to manipulate and process XLIFF documents</source>
14+
<target>XLIFF 文書を編集、または処理 するアプリケーションです。</target>
15+
</segment>
16+
</unit>
17+
<unit id="3">
18+
<segment>
19+
<source>XLIFF Data Manager</source>
20+
<target>XLIFF データ・マネージャ</target>
21+
</segment>
22+
</unit>
23+
</group>
24+
</file>
25+
</xliff>

0 commit comments

Comments
 (0)