Skip to content

[Translation] Support name attribute on the xliff2 translator loader #35373

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
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* added support for `name` attribute on `unit` element from xliff2 to be used as a translation key instead of always the `source` element

5.0.0
-----

Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ private function extractXliff2(\DOMDocument $dom, MessageCatalogue $catalogue, s

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,12 @@ public function testLoadWithMultipleFileNodes()
$catalogue->getMetadata('test', 'domain1')
);
}

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

$this->assertEquals(['foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo', 'qux' => 'qux source'], $catalogue->all('domain1'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en-US">
<file id="f1" original="Graphic Example.psd">
<unit id="1" name="foo">
<segment>
<source></source>
<target>bar</target>
</segment>
</unit>
<unit id="2" name="bar">
<segment>
<source>bar source</source>
<target>baz</target>
</segment>
</unit>
<unit id="3">
<segment>
<source>baz</source>
<target>foo</target>
</segment>
</unit>
<unit id="4" name="qux">
<segment>
<source>qux source</source>
</segment>
</unit>
</file>
</xliff>