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

Skip to content

Commit 9fc1da6

Browse files
committed
add XliffVersion 2.0
1 parent 97b096b commit 9fc1da6

File tree

4 files changed

+132
-30
lines changed

4 files changed

+132
-30
lines changed

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

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,29 @@ public function load($resource, $locale, $domain = 'messages')
4141
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
4242
}
4343

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

4856
$catalogue = new MessageCatalogue($locale);
4957
$version->extract($dom, $catalogue, $domain);
58+
5059
$catalogue->addResource(new FileResource($resource));
5160

5261
return $catalogue;
5362
}
5463

55-
/**
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-
7764
/**
7865
* @param \DOMDocument $dom
79-
* @param string $schema source of the schema
66+
* @param string $schema source of the schema
8067
*
8168
* @throws InvalidResourceException
8269
*/
@@ -126,19 +113,23 @@ private function getXmlErrors($internalErrors)
126113
*
127114
* @param \DOMDocument $dom
128115
*
129-
* @throws InvalidResourceException
116+
* @throws \InvalidArgumentException
130117
*
131118
* @return XliffVersion\AbstractXliffVersion
132119
*/
133120
private function getVersion(\DOMDocument $dom)
134121
{
135122
$versionNumber = $this->getVersionNumber($dom);
136123

137-
if ('1.2' === $versionNumber) {
138-
return new XliffVersion\XliffVersion12();
124+
switch ($versionNumber) {
125+
case '1.2':
126+
return new XliffVersion\XliffVersion12();
127+
128+
case '2.0':
129+
return new XliffVersion\XliffVersion20();
139130
}
140131

141-
throw new InvalidResourceException(sprintf(
132+
throw new \InvalidArgumentException(sprintf(
142133
'No support implemented for loading XLIFF version "%s".',
143134
$versionNumber
144135
));
@@ -150,6 +141,8 @@ private function getVersion(\DOMDocument $dom)
150141
*
151142
* @param \DOMDocument $dom
152143
*
144+
* @throws \InvalidArgumentException
145+
*
153146
* @return string
154147
*/
155148
private function getVersionNumber(\DOMDocument $dom)
@@ -160,6 +153,18 @@ private function getVersionNumber(\DOMDocument $dom)
160153
if ($version) {
161154
return $version->nodeValue;
162155
}
156+
157+
$namespace = $xliff->attributes->getNamedItem('xmlns');
158+
if ($namespace) {
159+
if (substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34) !== 0) {
160+
throw new \InvalidArgumentException(sprintf(
161+
'Not a valid XLIFF namespace "%s"',
162+
$namespace
163+
));
164+
}
165+
166+
return substr($namespace, 34);
167+
}
163168
}
164169

165170
// Falls back to v1.2
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 Symfony\Component\Translation\MessageCatalogue;
15+
16+
/**
17+
* XliffVersion20 loads XLIFF files identified with version 2.0
18+
*
19+
* @author Berny Cantos <[email protected]>
20+
*/
21+
class XliffVersion20 extends AbstractXliffVersion
22+
{
23+
/**
24+
* @return string
25+
*/
26+
public function getSchema()
27+
{
28+
$source = file_get_contents(__DIR__ . '/../schema/dic/xliff-core/xliff-core-2.0.xsd');
29+
30+
return $this->fixXmlLocation($source, 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd');
31+
}
32+
33+
/**
34+
* @param \DOMDocument $dom
35+
* @param MessageCatalogue $catalogue
36+
* @param string $domain
37+
*/
38+
public function extract(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
39+
{
40+
$xml = simplexml_import_dom($dom);
41+
$encoding = strtoupper($dom->encoding);
42+
43+
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');
44+
45+
foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) {
46+
$source = $segment->source;
47+
48+
// If the xlf file has another encoding specified, try to convert it because
49+
// simple_xml will always return utf-8 encoded values
50+
$target = $this->utf8ToCharset((string) $segment->target, $encoding);
51+
52+
$catalogue->set((string) $source, $target, $domain);
53+
}
54+
}
55+
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Symfony\Component\Translation\Tests\Loader;
1313

14-
use Symfony\Component\Translation\Loader\XliffFileLoader;
1514
use Symfony\Component\Config\Resource\FileResource;
15+
use Symfony\Component\Translation\Loader\XliffFileLoader;
1616

1717
class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
1818
{
@@ -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)