Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a15228

Browse files
committed
[Translation] added <tool> element metadata to XliffFileDumper
1 parent c8e05e3 commit 9a15228

File tree

5 files changed

+91
-25
lines changed

5 files changed

+91
-25
lines changed

src/Symfony/Component/Translation/Dumper/XliffFileDumper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ class XliffFileDumper extends FileDumper
2525
*/
2626
private $defaultLocale;
2727

28+
/**
29+
* Generator metadata for <tool> element.
30+
*
31+
* @link http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#tool_elem
32+
*
33+
* @var array
34+
*/
35+
private $toolInfo = array(
36+
'tool-id' => 'symfony',
37+
'tool-name' => 'Symfony',
38+
);
39+
2840
/**
2941
* {@inheritdoc}
3042
*/
@@ -36,6 +48,10 @@ public function dump(MessageCatalogue $messages, $options = array())
3648
$this->defaultLocale = \Locale::getDefault();
3749
}
3850

51+
if (array_key_exists('tool_info', $options)) {
52+
$this->toolInfo = array_merge($this->toolInfo, $options['tool_info']);
53+
}
54+
3955
parent::dump($messages, $options);
4056
}
4157

@@ -57,6 +73,12 @@ protected function format(MessageCatalogue $messages, $domain)
5773
$xliffFile->setAttribute('datatype', 'plaintext');
5874
$xliffFile->setAttribute('original', 'file.ext');
5975

76+
$xliffHead = $xliffFile->appendChild($dom->createElement('header'));
77+
$xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
78+
foreach ($this->toolInfo as $id => $value) {
79+
$xliffTool->setAttribute($id, $value);
80+
}
81+
6082
$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
6183
foreach ($messages->all($domain) as $source => $target) {
6284
$translation = $dom->createElement('trans-unit');

src/Symfony/Component/Translation/Tests/Dumper/XliffFileDumperTest.php

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
class XliffFileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19+
protected $tempDir;
20+
21+
protected function setUp()
22+
{
23+
$this->tempDir = sys_get_temp_dir();
24+
}
25+
1926
public function testDump()
2027
{
2128
$catalogue = new MessageCatalogue('en_US');
@@ -27,50 +34,56 @@ public function testDump()
2734
$catalogue->setMetadata('foo', array('notes' => array(array('priority' => 1, 'from' => 'bar', 'content' => 'baz'))));
2835
$catalogue->setMetadata('key', array('notes' => array(array('content' => 'baz'), array('content' => 'qux'))));
2936

30-
$tempDir = sys_get_temp_dir();
3137
$dumper = new XliffFileDumper();
32-
$dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));
38+
$dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR'));
3339

3440
$this->assertEquals(
3541
file_get_contents(__DIR__.'/../fixtures/resources-clean.xlf'),
36-
file_get_contents($tempDir.'/messages.en_US.xlf')
42+
file_get_contents($this->tempDir.'/messages.en_US.xlf')
43+
);
44+
45+
unlink($this->tempDir.'/messages.en_US.xlf');
46+
}
47+
48+
public function testDumpWithCustomToolInfo()
49+
{
50+
$options = array(
51+
'path' => $this->tempDir,
52+
'default_locale' => 'en_US',
53+
'tool_info' => array('tool-id' => 'foo', 'tool-name' => 'foo', 'tool-version' => '0.0', 'tool-company' => 'Foo'),
54+
);
55+
56+
$catalogue = new MessageCatalogue('en_US');
57+
$catalogue->add(array('foo' => 'bar'));
58+
59+
$dumper = new XliffFileDumper();
60+
$dumper->dump($catalogue, $options);
61+
62+
$this->assertEquals(
63+
file_get_contents(__DIR__.'/../fixtures/resources-tool-info.xlf'),
64+
file_get_contents($this->tempDir.'/messages.en_US.xlf')
3765
);
3866

39-
unlink($tempDir.'/messages.en_US.xlf');
67+
unlink($this->tempDir.'/messages.en_US.xlf');
4068
}
4169

42-
public function testTargetAttributesMetadataIsSetInFile()
70+
public function testDumpWithTargetAttributesMetadata()
4371
{
4472
$catalogue = new MessageCatalogue('en_US');
4573
$catalogue->add(array(
4674
'foo' => 'bar',
4775
));
4876
$catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
4977

50-
$tempDir = sys_get_temp_dir();
78+
$this->tempDir = sys_get_temp_dir();
5179
$dumper = new XliffFileDumper();
52-
$dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));
53-
54-
$content = <<<EOT
55-
<?xml version="1.0" encoding="utf-8"?>
56-
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
57-
<file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
58-
<body>
59-
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
60-
<source>foo</source>
61-
<target state="needs-translation">bar</target>
62-
</trans-unit>
63-
</body>
64-
</file>
65-
</xliff>
66-
67-
EOT;
80+
$dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR'));
6881

6982
$this->assertEquals(
70-
$content,
71-
file_get_contents($tempDir.'/messages.en_US.xlf')
83+
file_get_contents(__DIR__.'/../fixtures/resources-target-attributes.xlf'),
84+
file_get_contents($this->tempDir.'/messages.en_US.xlf')
7285
);
7386

74-
unlink($tempDir.'/messages.en_US.xlf');
87+
unlink($this->tempDir.'/messages.en_US.xlf');
7588
}
7689
}

src/Symfony/Component/Translation/Tests/fixtures/resources-clean.xlf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
33
<file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
4+
<header>
5+
<tool tool-id="symfony" tool-name="Symfony"/>
6+
</header>
47
<body>
58
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
69
<source>foo</source>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
3+
<file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
4+
<header>
5+
<tool tool-id="symfony" tool-name="Symfony"/>
6+
</header>
7+
<body>
8+
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
9+
<source>foo</source>
10+
<target state="needs-translation">bar</target>
11+
</trans-unit>
12+
</body>
13+
</file>
14+
</xliff>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
3+
<file source-language="en-US" target-language="en-US" datatype="plaintext" original="file.ext">
4+
<header>
5+
<tool tool-id="foo" tool-name="foo" tool-version="0.0" tool-company="Foo"/>
6+
</header>
7+
<body>
8+
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
9+
<source>foo</source>
10+
<target>bar</target>
11+
</trans-unit>
12+
</body>
13+
</file>
14+
</xliff>

0 commit comments

Comments
 (0)