8000 Support name attribute on the xliff2 translator loader · symfony/symfony@6174bac · GitHub
[go: up one dir, main page]

Skip to content

Commit 6174bac

Browse files
committed
Support name attribute on the xliff2 translator loader
1 parent af4035d commit 6174bac

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
* added support for `name` attribute on `unit` element from xliff2 to be used as a translation key instead of always the `source` element
7+
48
5.0.0
59
-----
610

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,12 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, s
135135

136136
foreach ($xml->xpath('//xliff:unit') as $unit) {
137137
foreach ($unit->segment as $segment) {
138-
$source = $segment->source;
138+
$attributes = $unit->attributes();
139+
$source = $attributes['name'] ?? $segment->source;
139140

140141
// If the xlf file has another encoding specified, try to convert it because
141142
// simple_xml will always return utf-8 encoded values
142-
$target = $this->utf8ToCharset((string) (isset($segment->target) ? $segment->target : $source), $encoding);
143+
$target = $this->utf8ToCharset((string) ($segment->target ?? $segment->source), $encoding);
143144

144145
$catalogue->set((string) $source, $target, $domain);
145146

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,12 @@ public function testLoadWithMultipleFileNodes()
314314
$catalogue->getMetadata('test', 'domain1')
315315
);
316316
}
317+
318+
public function testLoadVersion2WithName()
319+
{
320+
$loader = new XliffFileLoader();
321+
$catalogue = $loader->load(__DIR__.'/../fixtures/resources-2.0-name.xlf', 'en', 'domain1');
322+
323+
$this->assertEquals(['foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo', 'qux' => 'qux source'], $catalogue->all('domain1'));
324+
}
317325
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US">
3+
<file id="f1" original="Graphic Example.psd">
4+
<unit id="1" name="foo">
5+
<segment>
6+
<source></source>
7+
<target>bar</target>
8+
</segment>
9+
</unit>
10+
<unit id="2" name="bar">
11+
<segment>
12+
<source>bar source</source>
13+
<target>baz</target>
14+
</segment>
15+
</unit>
16+
<unit id="3">
17+
<segment>
18+
<source>baz</source>
19+
<target>foo</target>
20+
</segment>
21+
</unit>
22+
<unit id="4" name="qux">
23+
<segment>
24+
<source>qux source</source>
25+
</segment>
26+
</unit>
27+
</file>
28+
</xliff>

0 commit comments

Comments
 (0)
0