8000 feature #35373 [Translation] Support name attribute on the xliff2 tra… · symfony/symfony@9bfa258 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bfa258

Browse files
committed
feature #35373 [Translation] Support name attribute on the xliff2 translator loader (Taluu)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Translation] Support name attribute on the xliff2 translator loader | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #32048 | License | MIT | Doc PR | not done yet Support using the `name` attribute on the `unit` element in xliff 2.0 to use as the "translation key" rather than always relying on the `<source>` content, as was done on the xliff 1.2. Commits ------- 37b3114 Support name attribute on the xliff2 translator loader
2 parents 083a32d + 37b3114 commit 9bfa258

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

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

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

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