feature #15551 [Translation] added <tool> element metadata to XliffFileDumper (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
This commit is contained in:
commit
d4441e97fe
@ -20,30 +20,22 @@ use Symfony\Component\Translation\MessageCatalogue;
|
|||||||
*/
|
*/
|
||||||
class XliffFileDumper extends FileDumper
|
class XliffFileDumper extends FileDumper
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $defaultLocale;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function dump(MessageCatalogue $messages, $options = array())
|
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
|
||||||
{
|
{
|
||||||
if (array_key_exists('default_locale', $options)) {
|
if (array_key_exists('default_locale', $options)) {
|
||||||
$this->defaultLocale = $options['default_locale'];
|
$defaultLocale = $options['default_locale'];
|
||||||
} else {
|
} 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 = new \DOMDocument('1.0', 'utf-8');
|
||||||
$dom->formatOutput = true;
|
$dom->formatOutput = true;
|
||||||
|
|
||||||
@ -52,11 +44,17 @@ class XliffFileDumper extends FileDumper
|
|||||||
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
|
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
|
||||||
|
|
||||||
$xliffFile = $xliff->appendChild($dom->createElement('file'));
|
$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('target-language', str_replace('_', '-', $messages->getLocale()));
|
||||||
$xliffFile->setAttribute('datatype', 'plaintext');
|
$xliffFile->setAttribute('datatype', 'plaintext');
|
||||||
$xliffFile->setAttribute('original', 'file.ext');
|
$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'));
|
$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
|
||||||
foreach ($messages->all($domain) as $source => $target) {
|
foreach ($messages->all($domain) as $source => $target) {
|
||||||
$translation = $dom->createElement('trans-unit');
|
$translation = $dom->createElement('trans-unit');
|
||||||
@ -105,6 +103,14 @@ class XliffFileDumper extends FileDumper
|
|||||||
return $dom->saveXML();
|
return $dom->saveXML();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function format(MessageCatalogue $messages, $domain)
|
||||||
|
{
|
||||||
|
return $this->formatCatalogue($messages, $domain);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -16,6 +16,13 @@ use Symfony\Component\Translation\Dumper\XliffFileDumper;
|
|||||||
|
|
||||||
class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
|
class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
private $tempDir;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->tempDir = sys_get_temp_dir();
|
||||||
|
}
|
||||||
|
|
||||||
public function testDump()
|
public function testDump()
|
||||||
{
|
{
|
||||||
$catalogue = new MessageCatalogue('en_US');
|
$catalogue = new MessageCatalogue('en_US');
|
||||||
@ -27,19 +34,40 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
|
|||||||
$catalogue->setMetadata('foo', array('notes' => array(array('priority' => 1, 'from' => 'bar', 'content' => 'baz'))));
|
$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'))));
|
$catalogue->setMetadata('key', array('notes' => array(array('content' => 'baz'), array('content' => 'qux'))));
|
||||||
|
|
||||||
$tempDir = sys_get_temp_dir();
|
|
||||||
$dumper = new XliffFileDumper();
|
$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(
|
$this->assertEquals(
|
||||||
file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'),
|
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($tempDir.'/messages.en_US.xlf');
|
unlink($this->tempDir.'/messages.en_US.xlf');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTargetAttributesMetadataIsSetInFile()
|
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($this->tempDir.'/messages.en_US.xlf');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDumpWithTargetAttributesMetadata()
|
||||||
{
|
{
|
||||||
$catalogue = new MessageCatalogue('en_US');
|
$catalogue = new MessageCatalogue('en_US');
|
||||||
$catalogue->add(array(
|
$catalogue->add(array(
|
||||||
@ -47,30 +75,15 @@ class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
$catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
|
$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 = new XliffFileDumper();
|
||||||
$dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));
|
$dumper->dump($catalogue, array('path' => $this->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;
|
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$content,
|
file_get_contents(__DIR__.'/../fixtures/resources-target-attributes.xlf'),
|
||||||
file_get_contents($tempDir.'/messages.en_US.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"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
|
<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">
|
<file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
|
||||||
|
<header>
|
||||||
|
<tool tool-id="symfony" tool-name="Symfony"/>
|
||||||
|
</header>
|
||||||
<body>
|
<body>
|
||||||
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
|
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
|
||||||
<source>foo</source>
|
<source>foo</source>
|
||||||
|
14
src/Symfony/Component/Translation/Tests/fixtures/resources-target-attributes.xlf
vendored
Normal file
14
src/Symfony/Component/Translation/Tests/fixtures/resources-target-attributes.xlf
vendored
Normal file
@ -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>
|
14
src/Symfony/Component/Translation/Tests/fixtures/resources-tool-info.xlf
vendored
Normal file
14
src/Symfony/Component/Translation/Tests/fixtures/resources-tool-info.xlf
vendored
Normal file
@ -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>
|
Reference in New Issue
Block a user