8000 [Translation] XLIFF 2.0 read support by xphere · Pull Request #12853 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] XLIFF 2.0 read support #12853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension 8000

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add XliffVersion 2.0
  • Loading branch information
xphere committed Jan 9, 2015
commit 0971c65d89b57e2046ed8013954b3f95e03cf5ac
62 changes: 33 additions & 29 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,28 @@ public function load($resource, $locale, $domain = 'messages')
throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}

$dom = $this->parseFile($resource);
$version = $this->getVersion($dom);
try {
$dom = XmlUtils::loadFile($resource);
$version = $this->getVersion($dom);
} catch (\InvalidArgumentException $e) {
$message = sprintf('Unable to load "%s": %s', $resource, $e->getMessage());

throw new InvalidResourceException($message, $e->getCode(), $e);
}

$this->validateSchema($dom, $version->getSchema());

$catalogue = new MessageCatalogue($locale);
$version->extract($dom, $catalogue, $domain);

$catalogue->addResource(new FileResource($resource));

return $catalogue;
}

/**
* Parses the given file into a DOMDocument.
*
* @param string $file
*
* @throws \RuntimeException
*
* @return \DOMDocument
*
* @throws InvalidResourceException
*/
private function parseFile($file)
{
try {
$dom = XmlUtils::loadFile($file);
} catch (\InvalidArgumentException $e) {
throw new InvalidResourceException(sprintf('Unable to load "%s": %s', $file, $e->getMessage()), $e->getCode(), $e);
}

return $dom;
}

/**
* @param \DOMDocument $dom
* @param string $schema source of the schema
* @param string $schema source of the schema
*
* @throws InvalidResourceException
*/
Expand Down Expand Up @@ -126,19 +112,23 @@ private function getXmlErrors($internalErrors)
*
* @param \DOMDocument $dom
*
* @throws InvalidResourceException
* @throws \InvalidArgumentException
*
* @return XliffVersion\AbstractXliffVersion
*/
private function getVersion(\DOMDocument $dom)
{
$versionNumber = $this->getVersionNumber($dom);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't handle an UnexpectedValueException that might be thrown in getVersionNumber() neither here or in the load() method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, my bad!
This should throw a InvalidArgumentException so we can catch on load method for better message readability.
Easy fix. Thank you!


if ('1.2' === $versionNumber) {
return new XliffVersion\XliffVersion12();
switch ($versionNumber) {
case '1.2':
return new XliffVersion\XliffVersion12();

case '2.0':
return new XliffVersion\XliffVersion20();
}

throw new InvalidResourceException(sprintf(
throw new \InvalidArgumentException(sprintf(
'No support implemented for loading XLIFF version "%s".',
$versionNumber
));
Expand All @@ -150,6 +140,8 @@ private function getVersion(\DOMDocument $dom)
*
* @param \DOMDocument $dom
*
* @throws \InvalidArgumentException
*
* @return string
*/
private function getVersionNumber(\DOMDocument $dom)
Expand All @@ -160,6 +152,18 @@ private function getVersionNumber(\DOMDocument $dom)
if ($version) {
return $version->nodeValue;
}

$namespace = $xliff->attributes->getNamedItem('xmlns');
if ($namespace) {
if (substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34) !== 0) {
throw new \InvalidArgumentException(sprintf(
'Not a valid XLIFF namespace "%s"',
$namespace
));
}

return substr($namespace, 34);
}
}

// Falls back to v1.2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Loader\XliffVersion;

use Symfony\Component\Translation\MessageCatalogue;

/**
* XliffVersion20 loads XLIFF files identified with version 2.0
*
* @author Berny Cantos <be@rny.cc>
*/
class XliffVersion20 extends AbstractXliffVersion
{
/**
* @return string
*/
public function getSchema()
{
$source = file_get_contents(__DIR__.'/../schema/dic/xliff-core/xliff-core-2.0.xsd');

return $this->fixXmlLocation($source, 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd');
}

/**
* @param \DOMDocument $dom
* @param MessageCatalogue $catalogue
* @param string $domain
*/
public function extract(\DOMDocument $dom, MessageCatalogue $catalogue, $domain)
{
$xml = simplexml_import_dom($dom);
$encoding = strtoupper($dom->encoding);

$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:2.0');

foreach ($xml->xpath('//xliff:unit/xliff:segment') as $segment) {
$source = $segment->source;

// If the xlf file has another encoding specified, try to convert it because
// simple_xml will always return utf-8 encoded values
$target = $this->utf8ToCharset((string) $segment->target, $encoding);

$catalogue->set((string) $source, $target, $domain);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Component\Translation\Tests\Loader;

use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Translation\Loader\XliffFileLoader;

class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -139,4 +139,21 @@ public function testLoadNotes()
$this->assertNull($catalogue->getMetadata('extra', 'domain1'));
$this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux'))), $catalogue->getMetadata('key', 'domain1'));
}

public function testLoadVersion2()
{
$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources-2.0.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
$this->assertSame(array(), libxml_get_errors());

$domains = $catalogue->all();
$this->assertCount(3, $domains['domain1']);

// Notes aren't assigned to specific segments, but to whole units, so there's no way to do a mapping
$this->assertEmpty($catalogue->getMetadata());
}
}
25 changes: 25 additions & 0 deletions src/Symfony/Component/Translation/Tests/fixtures/resources-2.0.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US" trgLang="ja-JP">
<file id="f1" original="Graphic Example.psd">
<skeleton href="Graphic Example.psd.skl"/>
<unit id="1">
<segment>
<source>Quetzal</source>
<target>Quetzal</target>
</segment>
</unit>
<group id="1">
<unit id="2">
<segment>
<source>An application to manipulate and process XLIFF documents</source>
<target>XLIFF 文書を編集、または処理 するアプリケーションです。</target>
</segment>
</unit>
<unit id="3">
<segment>
<source>XLIFF Data Manager</source>
<target>XLIFF データ・マネージャ</target>
</segment>
</unit>
</group>
</file>
</xliff>
0