8000 [Translation] added xliff loader/dumper with resname support · symfony/symfony@5bc30bb · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5bc30bb

Browse files
jfsimonfabpot
authored andcommitted
[Translation] added xliff loader/dumper with resname support
1 parent 7241be9 commit 5bc30bb

File tree

5 files changed

+64
-17
lines changed

5 files changed

+64
-17
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,30 @@ protected function format(MessageCatalogue $messages, $domain)
2727
{
2828
$dom = new \DOMDocument('1.0', 'utf-8');
2929
$dom->formatOutput = true;
30+
3031
$xliff = $dom->appendChild($dom->createElement('xliff'));
3132
$xliff->setAttribute('version', '1.2');
3233
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
34+
3335
$xliffFile = $xliff->appendChild($dom->createElement('file'));
3436
$xliffFile->setAttribute('source-language', $messages->getLocale());
3537
$xliffFile->setAttribute('datatype', 'plaintext');
3638
$xliffFile->setAttribute('original', 'file.ext');
39+
3740
$xliffBody = $xliffFile->appendChild($dom->createElement('body'));
38-
$id = 1;
3941
foreach ($messages->all($domain) as $source => $target) {
40-
$trans = $dom->createElement('trans-unit');
41-
$trans->setAttribute('id', $id);
42-
$s = $trans->appendChild($dom->createElement('source'));
42+
$translation = $dom->createElement('trans-unit');
43+
44+
$translation->setAttribute('id', md5($source));
45+
$translation->setAttribute('resname', $source);
46+
47+
$s = $translation->appendChild($dom->createElement('source'));
4348
$s->appendChild($dom->createTextNode($source));
44-
$t = $trans->appendChild($dom->createElement('target'));
49+
50+
$t = $translation->appendChild($dom->createElement('target'));
4551
$t->appendChild($dom->createTextNode($target));
46-
$xliffBody->appendChild($trans);
47-
$id++;
52+
53+
$xliffBody->appendChild($translation);
4854
}
4955

5056
return $dom->saveXML();

src/Symfony/Component/Translation/Loader/XliffFileLoader.php

Lines changed: 11 additions & 2 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Translation\Loader;
1313

14+
use Symfony\Component\DependencyInjection\SimpleXMLElement;
1415
use Symfony\Component\Translation\MessageCatalogue;
1516
use Symfony\Component\Config\Resource\FileResource;
1617

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

4041
$catalogue = new MessageCatalogue($locale);
4142
foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
42-
if (!isset($translation->source) || !isset($translation->target)) {
43+
$attributes = $translation->attributes();
44+
45+
if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
4346
continue;
4447
}
45-
$catalogue->set((string) $translation->source, (string) $translation->target, $domain);
48+
49+
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
50+
$catalogue->set((string) $source, (string) $translation->target, $domain);
4651
}
4752
$catalogue->addResource(new FileResource($resource));
4853

@@ -54,6 +59,8 @@ public function load($resource, $locale, $domain = 'messages')
5459
*
5560
* @param string $file
5661
*
62+
* @throws \RuntimeException
63+
*
5764
* @return \SimpleXMLElement
5865
*/
5966
private function parseFile($file)
@@ -109,6 +116,8 @@ private function parseFile($file)
109116
/**
110117
* Returns the XML errors of the internal XML parser
111118
*
119+
* @param boolean $internalErrors
120+
*
112121
* @return array An array of errors
113122
*/
114123
private function getXmlErrors($internalErrors)

src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@ protected function setUp()
2525

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

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

36+
public function testLoadWithResname()
37+
{
38+
$loader = $this->createLoader();
39+
$catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1');
40+
41+
$this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1'));
42+
}
43+
3644
public function testIncompleteResource()
3745
{
38-
$loader = new XliffFileLoader();
46+
$loader = $this->createLoader();
3947
$catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1');
4048

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

@@ -56,7 +64,7 @@ public function testLoadInvalidResource()
5664
*/
5765
public function testLoadResourceDoesNotValidate()
5866
{
59-
$loader = new XliffFileLoader();
67+
$loader = $this->createLoader();
6068
$catalogue = $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
6169
}
6270

@@ -65,7 +73,7 @@ public function testLoadResourceDoesNotValidate()
6573
*/
6674
public function testLoadThrowsAnExceptionIfFileNotLocal()
6775
{
68-
$loader = new XliffFileLoader();
76+
$loader = $this->createLoader();
6977
$resource = 'http://example.com/resources.xlf';
7078
$loader->load($resource, 'en', 'domain1');
7179
}
@@ -76,7 +84,12 @@ public function testLoadThrowsAnExceptionIfFileNotLocal()
7684
*/
7785
public function testDocTypeIsNotAllowed()
7886
{
79-
$loader = new XliffFileLoader();
87+
$loader = $this->createLoader();
8088
$loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1');
8189
}
90+
91+
protected function createLoader()
92+
{
93+
return new XliffFileLoader();
94+
}
8295
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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" datatype="plaintext" original="file.ext">
4+
<body>
5+
<trans-unit id="1" resname="foo">
6+
<source></source>
7+
<target>bar</target>
8+
</trans-unit>
9+
<trans-unit id="2" resname="bar">
10+
<source>bar source</source>
11+
<target>baz</target>
12+
</trans-unit>
13+
<trans-unit id="3">
14+
<source>baz</source>
15+
<target>foo</target>
16+
</trans-unit>
17+
</body>
18+
</file>
19+
</xliff>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
33
<file source-language="en" datatype="plaintext" original="file.ext">
44
<body>
5-
<trans-unit id="1">
5+
<trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
66
<source>foo</source>
77
<target>bar</target>
88
</trans-unit>
9-
<trans-unit id="2">
9+
<trans-unit id="3c6e0b8a9c15224a8228b9a98ca1531d" resname="key">
1010
<source>key</source>
1111
<target></target>
1212
</trans-unit>

0 commit comments

Comments
 (0)
0