Skip to content

Commit

Permalink
merged branch benjaminpaap/bugfixes/xliff_encoding (PR #7698)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Apr 20, 2013
2 parents a8915ff + 54bcf5c commit 8792575
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@
*/
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}
*
Expand All @@ -37,6 +68,8 @@ public function load($resource, $locale, $domain = 'messages')
$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();
Expand All @@ -46,7 +79,21 @@ public function load($resource, $locale, $domain = 'messages')
}

$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));

Expand Down Expand Up @@ -76,6 +123,11 @@ private function parseFile($file)
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public function testIncompleteResource()
$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
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Translation/Tests/fixtures/encoding.xlf
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 8792575

Please sign in to comment.