merged branch benjaminpaap/bugfixes/xliff_encoding (PR #7698)
This PR was squashed before being merged into the 2.1 branch (closes #7698).
Discussion
----------
[Translator] added additional conversion for encodings other than utf-8
Added an additional conversion if there is another encoding in the
xlf file present. Values from simple_xml are always utf-8 encoded.
Also added some tests to verify this new behaviour.
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| License | MIT
Commits
-------
54bcf5c
[Translator] added additional conversion for encodings other than utf-8
This commit is contained in:
commit
8792575795
|
@ -23,6 +23,37 @@ use Symfony\Component\Config\Resource\FileResource;
|
|||
*/
|
||||
class XliffFileLoader implements LoaderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* Encoding specified in xlf file
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $encoding = null;
|
||||
|
||||
/**
|
||||
* Get $encoding
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getEncoding()
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set $encoding
|
||||
*
|
||||
* @param string $encoding
|
||||
* @return \Symfony\Component\Translation\Loader\XliffFileLoader
|
||||
*/
|
||||
public function setEncoding($encoding)
|
||||
{
|
||||
$this->encoding = strtoupper($encoding);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
|
@ -37,6 +68,8 @@ class XliffFileLoader implements LoaderInterface
|
|||
$xml = $this->parseFile($resource);
|
||||
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2');
|
||||
|
||||
$encoding = $this->getEncoding();
|
||||
|
||||
$catalogue = new MessageCatalogue($locale);
|
||||
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
|
||||
$attributes = $translation->attributes();
|
||||
|
@ -46,7 +79,21 @@ class XliffFileLoader implements LoaderInterface
|
|||
}
|
||||
|
||||
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
|
||||
$catalogue->set((string) $source, (string) $translation->target, $domain);
|
||||
$target = (string) $translation->target;
|
||||
|
||||
// If the xlf file has another encoding specified try to convert it here because
|
||||
// simple_xml will always return utf-8 encoded values
|
||||
if ($encoding !== null) {
|
||||
if (function_exists('mb_convert_encoding')) {
|
||||
$target = mb_convert_encoding($target, $encoding, 'UTF-8');
|
||||
} elseif (function_exists('iconv')) {
|
||||
$target = iconv('UTF-8', $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) $translation->source, $target, $domain);
|
||||
}
|
||||
$catalogue->addResource(new FileResource($resource));
|
||||
|
||||
|
@ -76,6 +123,11 @@ class XliffFileLoader implements LoaderInterface
|
|||
throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors)));
|
||||
}
|
||||
|
||||
$encoding = strtoupper($dom->encoding);
|
||||
if (!empty($encoding) && $encoding != 'UTF-8') {
|
||||
$this->setEncoding($encoding);
|
||||
}
|
||||
|
||||
libxml_disable_entity_loader($disableEntities);
|
||||
|
||||
foreach ($dom->childNodes as $child) {
|
||||
|
|
|
@ -50,6 +50,15 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertFalse($catalogue->has('extra', 'domain1'));
|
||||
}
|
||||
|
||||
public function testEncoding()
|
||||
{
|
||||
$loader = $this->createLoader();
|
||||
$catalogue = $loader->load(__DIR__.'/../fixtures/encoding.xlf', 'en', 'domain1');
|
||||
|
||||
$this->assertEquals(utf8_decode('föö'), $catalogue->get('bar', 'domain1'));
|
||||
$this->assertEquals(utf8_decode('bär'), $catalogue->get('foo', 'domain1'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
|
||||
<file source-language="en" datatype="plaintext" original="file.ext">
|
||||
<body>
|
||||
<trans-unit id="1" resname="foo">
|
||||
<source>foo</source>
|
||||
<target>bär</target>
|
||||
</trans-unit>
|
||||
<trans-unit id="2" resname="bar">
|
||||
<source>bar</source>
|
||||
<target>föö</target>
|
||||
</trans-unit>
|
||||
</body>
|
||||
</file>
|
||||
</xliff>
|
Reference in New Issue