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

Skip to content

[Translation] added <tool> element metadata to XliffFileDumper #15551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/Symfony/Component/Translation/Dumper/XliffFileDumper.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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');
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>