This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Translation/Loader/XliffFileLoader.php

158 lines
5.2 KiB
PHP
Raw Normal View History

2010-09-27 08:45:29 +01:00
<?php
/*
* This file is part of the Symfony package.
2010-09-27 08:45:29 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-09-27 08:45:29 +01: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
*/
namespace Symfony\Component\Translation\Loader;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Config\Resource\FileResource;
2010-09-27 08:45:29 +01:00
/**
* XliffFileLoader loads translations from XLIFF files.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
2010-09-27 08:45:29 +01:00
*/
class XliffFileLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*
* @api
2010-09-27 08:45:29 +01:00
*/
public function load($resource, $locale, $domain = 'messages')
2010-09-27 08:45:29 +01:00
{
if (!stream_is_local($resource)) {
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $resource));
}
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) {
$attributes = $translation->attributes();
if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
continue;
}
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
$target = (string) $translation->target;
// If the xlf file has another encoding specified, try to convert it because
// simple_xml will always return utf-8 encoded values
if ('UTF-8' !== $this->encoding && !empty($this->encoding)) {
if (function_exists('mb_convert_encoding')) {
$target = mb_convert_encoding($target, $this->encoding, 'UTF-8');
} elseif (function_exists('iconv')) {
$target = iconv('UTF-8', $this->encoding, $target);
} else {
throw new \RuntimeException('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).');
}
}
$catalogue->set((string) $source, $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
*
2012-05-18 18:41:48 +01:00
* @param string $file
2011-12-13 07:50:54 +00:00
*
* @throws \RuntimeException
*
* @return \SimpleXMLElement
2010-09-27 08:45:29 +01:00
*/
private function parseFile($file)
2010-09-27 08:45:29 +01:00
{
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
libxml_clear_errors();
2010-09-27 08:45:29 +01:00
$dom = new \DOMDocument();
$dom->validateOnParse = true;
if (!@$dom->loadXML(file_get_contents($file), LIBXML_NONET | (defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
libxml_disable_entity_loader($disableEntities);
throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors)));
2010-09-27 08:45:29 +01:00
}
$this->encoding = strtoupper($dom->encoding);
libxml_disable_entity_loader($disableEntities);
foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
libxml_use_internal_errors($internalErrors);
throw new \RuntimeException('Document types are not allowed.');
}
2010-09-27 08:45:29 +01:00
}
2011-06-23 13:07:53 +01:00
$location = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
$parts = explode('/', $location);
if (0 === stripos($location, 'phar://')) {
$tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
if ($tmpfile) {
copy($location, $tmpfile);
$parts = explode('/', str_replace('\\', '/', $tmpfile));
}
}
2010-09-27 08:45:29 +01:00
$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 \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors)));
2010-09-27 08:45:29 +01:00
}
2010-09-27 08:45:29 +01:00
$dom->normalizeDocument();
libxml_use_internal_errors($internalErrors);
2010-09-27 08:45:29 +01:00
return simplexml_import_dom($dom);
}
/**
* Returns the XML errors of the internal XML parser
*
* @param boolean $internalErrors
*
* @return array An array of errors
2010-09-27 08:45:29 +01:00
*/
private function getXmlErrors($internalErrors)
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($internalErrors);
2010-09-27 08:45:29 +01:00
return $errors;
}
}