2010-09-27 08:45:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:29 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Translation\Loader;
|
|
|
|
|
|
|
|
use Symfony\Component\Translation\MessageCatalogue;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-09-27 08:45:29 +01:00
|
|
|
/**
|
|
|
|
* XliffFileLoader loads translations from XLIFF files.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-09-27 08:45:29 +01:00
|
|
|
*/
|
|
|
|
class XliffFileLoader implements LoaderInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2010-11-14 19:24:29 +00:00
|
|
|
public function load($resource, $locale, $domain = 'messages')
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
|
|
|
$xml = $this->parseFile($resource);
|
|
|
|
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
|
|
|
|
|
|
|
|
$catalogue = new MessageCatalogue($locale);
|
|
|
|
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
|
2010-11-23 08:42:19 +00:00
|
|
|
$catalogue->set((string) $translation->source, (string) $translation->target, $domain);
|
2010-09-27 08:45:29 +01:00
|
|
|
}
|
|
|
|
$catalogue->addResource(new FileResource($resource));
|
|
|
|
|
|
|
|
return $catalogue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and parses the given file into a SimpleXMLElement
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @return SimpleXMLElement
|
|
|
|
*/
|
2011-03-08 19:47:32 +00:00
|
|
|
private function parseFile($file)
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$current = libxml_use_internal_errors(true);
|
|
|
|
if (!@$dom->load($file, LIBXML_COMPACT)) {
|
|
|
|
throw new \Exception(implode("\n", $this->getXmlErrors()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$parts = explode('/', str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd');
|
|
|
|
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
|
|
|
|
$location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
|
|
|
|
|
|
|
|
$source = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
|
|
|
|
$source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source);
|
|
|
|
|
|
|
|
if (!@$dom->schemaValidateSource($source)) {
|
|
|
|
throw new \Exception(implode("\n", $this->getXmlErrors()));
|
|
|
|
}
|
|
|
|
$dom->validateOnParse = true;
|
|
|
|
$dom->normalizeDocument();
|
|
|
|
libxml_use_internal_errors($current);
|
|
|
|
|
|
|
|
return simplexml_import_dom($dom);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the XML errors of the internal XML parser
|
|
|
|
*
|
|
|
|
* @return array An array of errors
|
|
|
|
*/
|
2011-03-08 19:47:32 +00:00
|
|
|
private function getXmlErrors()
|
2010-09-27 08:45:29 +01:00
|
|
|
{
|
|
|
|
$errors = array();
|
|
|
|
foreach (libxml_get_errors() as $error) {
|
|
|
|
$errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
|
|
|
|
LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
|
|
|
|
$error->code,
|
|
|
|
trim($error->message),
|
|
|
|
$error->file ? $error->file : 'n/a',
|
|
|
|
$error->line,
|
|
|
|
$error->column
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
libxml_clear_errors();
|
|
|
|
libxml_use_internal_errors(false);
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
}
|