Skip to content

Commit

Permalink
feature #15551 [Translation] added <tool> element metadata to XliffFi…
Browse files Browse the repository at this point in the history
…leDumper (aitboudad)

This PR was merged into the 2.8 branch.

Discussion
----------

[Translation] added <tool> element metadata to XliffFileDumper

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15190
| License       | MIT
| Doc PR        |  ~

Commits
-------

f667a34 [Translation] added <tool> element metadata to XliffFileDumper
  • Loading branch information
fabpot committed Sep 1, 2015
2 parents c6e5830 + f667a34 commit d4441e9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 41 deletions.
38 changes: 22 additions & 16 deletions src/Symfony/Component/Translation/Dumper/XliffFileDumper.php
Expand Up @@ -20,30 +20,22 @@
*/
class XliffFileDumper extends FileDumper
{
/**
* @var string
*/
private $defaultLocale;

/**
* {@inheritdoc}
*/
public function dump(MessageCatalogue $messages, $options = array())
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
{
if (array_key_exists('default_locale', $options)) {
$this->defaultLocale = $options['default_locale'];
$defaultLocale = $options['default_locale'];
} else {
$this->defaultLocale = \Locale::getDefault();
$defaultLocale = \Locale::getDefault();
}

parent::dump($messages, $options);
}
$toolInfo = array('tool-id' => 'symfony', 'tool-name' => 'Symfony');
if (array_key_exists('tool_info', $options)) {
$toolInfo = array_merge($toolInfo, $options['tool_info']);
}

/**
* {@inheritdoc}
*/
protected function format(MessageCatalogue $messages, $domain)
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;

Expand All @@ -52,11 +44,17 @@ protected function format(MessageCatalogue $messages, $domain)
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');

$xliffFile = $xliff->appendChild($dom->createElement('file'));
$xliffFile->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale));
$xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
$xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
$xliffFile->setAttribute('datatype', 'plaintext');
$xliffFile->setAttribute('original', 'file.ext');

$xliffHead = $xliffFile->appendChild($dom->createElement('header'));
$xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
foreach ($toolInfo as $id => $value) {
$xliffTool->setAttribute($id, $value);
}

$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
foreach ($messages->all($domain) as $source => $target) {
$translation = $dom->createElement('trans-unit');
Expand Down Expand Up @@ -105,6 +103,14 @@ protected function format(MessageCatalogue $messages, $domain)
return $dom->saveXML();
}

/**
* {@inheritdoc}
*/
protected function format(MessageCatalogue $messages, $domain)
{
return $this->formatCatalogue($messages, $domain);
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -16,6 +16,13 @@

class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
{
private $tempDir;

protected function setUp()
{
$this->tempDir = sys_get_temp_dir();
}

public function testDump()
{
$catalogue = new MessageCatalogue('en_US');
Expand All @@ -27,50 +34,56 @@ public function testDump()
$catalogue->setMetadata('foo', array('notes' => array(array('priority' => 1, 'from' => 'bar', 'content' => 'baz'))));
$catalogue->setMetadata('key', array('notes' => array(array('content' => 'baz'), array('content' => 'qux'))));

$tempDir = sys_get_temp_dir();
$dumper = new XliffFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));
$dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR'));

$this->assertEquals(
file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'),
file_get_contents($tempDir.'/messages.en_US.xlf')
file_get_contents($this->tempDir.'/messages.en_US.xlf')
);

unlink($this->tempDir.'/messages.en_US.xlf');
}

public function testDumpWithCustomToolInfo()
{
$options = array(
'path' => $this->tempDir,
'default_locale' => 'en_US',
'tool_info' => array('tool-id' => 'foo', 'tool-name' => 'foo', 'tool-version' => '0.0', 'tool-company' => 'Foo'),
);

$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array('foo' => 'bar'));

$dumper = new XliffFileDumper();
$dumper->dump($catalogue, $options);

$this->assertEquals(
file_get_contents(__DIR__.'/../fixtures/resources-tool-info.xlf'),
file_get_contents($this->tempDir.'/messages.en_US.xlf')
);

unlink($tempDir.'/messages.en_US.xlf');
unlink($this->tempDir.'/messages.en_US.xlf');
}

public function testTargetAttributesMetadataIsSetInFile()
public function testDumpWithTargetAttributesMetadata()
{
$catalogue = new MessageCatalogue('en_US');
$catalogue->add(array(
'foo' => 'bar',
));
$catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));

$tempDir = sys_get_temp_dir();
$this->tempDir = sys_get_temp_dir();
$dumper = new XliffFileDumper();
$dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));

$content = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
<source>foo</source>
<target state="needs-translation">bar</target>
</trans-unit>
</body>
</file>
</xliff>
EOT;
$dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR'));

$this->assertEquals(
$content,
file_get_contents($tempDir.'/messages.en_US.xlf')
file_get_contents(__DIR__.'/../fixtures/resources-target-attributes.xlf'),
file_get_contents($this->tempDir.'/messages.en_US.xlf')
);

unlink($tempDir.'/messages.en_US.xlf');
unlink($this->tempDir.'/messages.en_US.xlf');
}
}
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
<header>
<tool tool-id="symfony" tool-name="Symfony"/>
</header>
<body>
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
<source>foo</source>
Expand Down
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
<header>
<tool tool-id="symfony" tool-name="Symfony"/>
</header>
<body>
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
<source>foo</source>
<target state="needs-translation">bar</target>
</trans-unit>
</body>
</file>
</xliff>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en-US" target-language="en-US" datatype="plaintext" original="file.ext">
<header>
<tool tool-id="foo" tool-name="foo" tool-version="0.0" tool-company="Foo"/>
</header>
<body>
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
<source>foo</source>
<target>bar</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit d4441e9

Please sign in to comment.