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

Skip to content

Commit 135233e

Browse files
committed
[Translation] added <tool> element metadata to XliffFileDumper
1 parent 4353134 commit 135233e

File tree

5 files changed

+91
-41
lines changed

5 files changed

+91
-41
lines changed

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,22 @@
2020
*/
2121
class XliffFileDumper extends FileDumper
2222
{
23-
/**
24-
* @var string
25-
*/
26-
private $defaultLocale;
27-
2823
/**
2924
* {@inheritdoc}
3025
*/
31-
public function dump(MessageCatalogue $messages, $options = array())
26+
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3227
{
3328
if (array_key_exists('default_locale', $options)) {
34-
$this->defaultLocale = $options['default_locale'];
29+
$defaultLocale = $options['default_locale'];
3530
} else {
36-
$this->defaultLocale = \Locale::getDefault();
31+
$defaultLocale = \Locale::getDefault();
3732
}
3833

39-
parent::dump($messages, $options);
40-
}
34+
$toolInfo = array('tool-id' => 'symfony', 'tool-name' => 'Symfony');
35+
if (array_key_exists('tool_info', $options)) {
36+
$toolInfo = array_merge($toolInfo, $options['tool_info']);
37+
}
4138

42-
/**
43-
* {@inheritdoc}
44-
*/
45-
protected function format(MessageCatalogue $messages, $domain)
46-
{
4739
$dom = new \DOMDocument('1.0', 'utf-8');
4840
$dom->formatOutput = true;
4941

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

5446
$xliffFile = $xliff->appendChild($dom->createElement('file'));
55-
$xliffFile->setAttribute('source-language', str_replace('_', '-', $this->defaultLocale));
47+
$xliffFile->setAttribute('source-language', str_replace('_', '-', $defaultLocale));
5648
$xliffFile->setAttribute('target-language', str_replace('_', '-', $messages->getLocale()));
5749
$xliffFile->setAttribute('datatype', 'plaintext');
5850
$xliffFile->setAttribute('original', 'file.ext');
5951

52+
$xliffHead = $xliffFile->appendChild($dom->createElement('header'));
53+
$xliffTool = $xliffHead->appendChild($dom->createElement('tool'));
54+
foreach ($toolInfo as $id => $value) {
55+
$xliffTool->setAttribute($id, $value);
56+
}
57+
6058
$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
6159
foreach ($messages->all($domain) as $source => $target) {
6260
$translation = $dom->createElement('trans-unit');
@@ -105,6 +103,14 @@ protected function format(MessageCatalogue $messages, $domain)
105103
return $dom->saveXML();
106104
}
107105

106+
/**
107+
* {@inheritdoc}
108+
*/
109+
protected function format(MessageCatalogue $messages, $domain)
110+
{
111+
return $this->formatCatalogue($messages, $domain);
112+
}
113+
108114
/**
109115
* {@inheritdoc}
110116
*/

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+
private $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)