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

Skip to content

[Translation] added xliff loader/dumper with resname support #7304

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

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 13 additions & 7 deletions src/Symfony/Component/Translation/Dumper/XliffFileDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,30 @@ protected function format(MessageCatalogue $messages, $domain)
{
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;

$xliff = $dom->appendChild($dom->createElement('xliff'));
$xliff->setAttribute('version', '1.2');
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');

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

$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
$id = 1;
foreach ($messages->all($domain) as $source => $target) {
$trans = $dom->createElement('trans-unit');
$trans->setAttribute('id', $id);
$s = $trans->appendChild($dom->createElement('source'));
$translation = $dom->createElement('trans-unit');

$translation->setAttribute('id', md5($source));
$translation->setAttribute('resname', $source);

$s = $translation->appendChild($dom->createElement('source'));
$s->appendChild($dom->createTextNode($source));
$t = $trans->appendChild($dom->createElement('target'));

$t = $translation->appendChild($dom->createElement('target'));
$t->appendChild($dom->createTextNode($target));
$xliffBody->appendChild($trans);
$id++;

$xliffBody->appendChild($translation);
}

return $dom->saveXML();
Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Translation\Loader;

use Symfony\Component\DependencyInjection\SimpleXMLElement;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use statement looks wrong to me

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Config\Resource\FileResource;

Expand Down Expand Up @@ -39,10 +40,14 @@ public function load($resource, $locale, $domain = 'messages')

$catalogue = new MessageCatalogue($locale);
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
if (!isset($translation->source) || !isset($translation->target)) {
$attributes = $translation->attributes();

if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
continue;
}
$catalogue->set((string) $translation->source, (string) $translation->target, $domain);

$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
$catalogue->set((string) $source, (string) $translation->target, $domain);
}
$catalogue->addResource(new FileResource($resource));

Expand All @@ -54,6 +59,8 @@ public function load($resource, $locale, $domain = 'messages')
*
* @param string $file
*
* @throws \RuntimeException
*
* @return \SimpleXMLElement
*/
private function parseFile($file)
Expand Down Expand Up @@ -109,6 +116,8 @@ private function parseFile($file)
/**
* Returns the XML errors of the internal XML parser
*
* @param boolean $internalErrors
*
* @return array An array of errors
*/
private function getXmlErrors($internalErrors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@ protected function setUp()

public function testLoad()
{
$loader = new XliffFileLoader();
$loader = $this->createLoader();
$resource = __DIR__.'/../fixtures/resources.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}

public function testLoadWithResname()
{
$loader = $this->createLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1');

$this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1'));
}

public function testIncompleteResource()
{
$loader = new XliffFileLoader();
$loader = $this->createLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1');

$this->assertEquals(array('foo' => 'bar', 'key' => '', 'test' => 'with'), $catalogue->all('domain1'));
Expand All @@ -47,7 +55,7 @@ public function testIncompleteResource()
*/
public function testLoadInvalidResource()
{
$loader = new XliffFileLoader();
$loader = $this->createLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1');
}

Expand All @@ -56,7 +64,7 @@ public function testLoadInvalidResource()
*/
public function testLoadResourceDoesNotValidate()
{
$loader = new XliffFileLoader();
$loader = $this->createLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
}

Expand All @@ -65,7 +73,7 @@ public function testLoadResourceDoesNotValidate()
*/
public function testLoadThrowsAnExceptionIfFileNotLocal()
{
$loader = new XliffFileLoader();
$loader = $this->createLoader();
$resource = 'http://example.com/resources.xlf';
$loader->load($resource, 'en', 'domain1');
}
Expand All @@ -76,7 +84,12 @@ public function testLoadThrowsAnExceptionIfFileNotLocal()
*/
public function testDocTypeIsNotAllowed()
{
$loader = new XliffFileLoader();
$loader = $this->createLoader();
$loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1');
}

protected function createLoader()
{
return new XliffFileLoader();
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Translation/Tests/fixtures/resname.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1" resname="foo">
<source></source>
<target>bar</target>
</trans-unit>
<trans-unit id="2" resname="bar">
<source>bar source</source>
<target>baz</target>
</trans-unit>
<trans-unit id="3">
<source>baz</source>
<target>foo</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
<source>foo</source>
<target>bar</target>
</trans-unit>
<trans-unit id="2">
<trans-unit id="3c6e0b8a9c15224a8228b9a98ca1531d" resname="key">
<source>key</source>
<target></target>
</trans-unit>
Expand Down