merged branch aerialls/xliff_fix (PR #3664)

Commits
-------

93d2a4f [Translation] Ignore xliff entries with no "target" node

Discussion
----------

[Translation] Ignore xliff entries with no "target" node

This PR will ignore some entries with no "target" node when a xliff file is loaded.

```xml
<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">
        <source>foo</source>
        <target>bar</target>
      </trans-unit>
      <trans-unit id="2">
        <source>extra</source>
      </trans-unit>
    </body>
  </file>
</xliff>
```
Before:

```php
array(
    'foo'   => 'bar',
    'extra' => ''
);
```

After:

```php
array(
    'foo' => 'bar'
);
```

---------------------------------------------------------------------------

by fabpot at 2012-03-21T17:35:51Z

Wouldn't it be better to throw an exception for such cases? A `trans-unit` without a `target` is useless anyway, no?

---------------------------------------------------------------------------

by aerialls at 2012-03-21T20:25:19Z

I'm using transifex (https://www.transifex.net/home/) and when a user doesn't translate an entry, transifex fills up a `trans-unit` node without a `target` children.
This commit is contained in:
Fabien Potencier 2012-03-21 22:27:33 +01:00
commit e83f5f4f03
5 changed files with 36 additions and 3 deletions

15
resources-clean.xlf Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<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">
<source>foo</source>
<target>bar</target>
</trans-unit>
<trans-unit id="2">
<source>key</source>
<target></target>
</trans-unit>
</body>
</file>
</xliff>

View File

@ -39,6 +39,9 @@ class XliffFileLoader implements LoaderInterface
$catalogue = new MessageCatalogue($locale);
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
if (2 !== count($translation)) {
continue;
}
$catalogue->set((string) $translation->source, (string) $translation->target, $domain);
}
$catalogue->addResource(new FileResource($resource));

View File

@ -19,13 +19,13 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
public function testDump()
{
$catalogue = new MessageCatalogue('en');
$catalogue->add(array('foo' => 'bar'));
$catalogue->add(array('foo' => 'bar', 'key' => ''));
$tempDir = sys_get_temp_dir();
$dumper = new XliffFileDumper();
$dumperString = $dumper->dump($catalogue, array('path' => $tempDir));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.xlf'), file_get_contents($tempDir.'/messages.en.xlf'));
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'), file_get_contents($tempDir.'/messages.en.xlf'));
unlink($tempDir.'/messages.en.xlf');
}

View File

@ -22,11 +22,19 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
$resource = __DIR__.'/../fixtures/resources.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testIncompleteResource()
{
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1');
$this->assertEquals(array('foo' => 'bar', 'key' => ''), $catalogue->all('domain1'));
$this->assertFalse($catalogue->has('extra', 'domain1'));
}
/**
* @expectedException \RuntimeException
*/

View File

@ -6,6 +6,13 @@
<source>foo</source>
<target>bar</target>
</trans-unit>
<trans-unit id="2">
<source>extra</source>
</trans-unit>
<trans-unit id="3">
<source>key</source>
<target></target>
</trans-unit>
</body>
</file>
</xliff>