From 37b31149c6e2d1369622737fbb6cc1ae9964a507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Baptiste=20Clavi=C3=A9?= Date: Fri, 17 Jan 2020 17:43:55 +0100 Subject: [PATCH] Support name attribute on the xliff2 translator loader --- .../Component/Translation/CHANGELOG.md | 5 ++++ .../Translation/Loader/XliffFileLoader.php | 5 ++-- .../Tests/Loader/XliffFileLoaderTest.php | 8 ++++++ .../Tests/fixtures/resources-2.0-name.xlf | 28 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-name.xlf diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index b1de62a32a..e2930a418a 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index 6e2ed3b4f8..d01ae96068 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php @@ -135,11 +135,12 @@ class XliffFileLoader implements LoaderInterface 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); diff --git a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php index 79e51f1232..795967851f 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/XliffFileLoaderTest.php @@ -314,4 +314,12 @@ class XliffFileLoaderTest extends TestCase $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')); + } } diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-name.xlf b/src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-name.xlf new file mode 100644 index 0000000000..af4f075457 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/resources-2.0-name.xlf @@ -0,0 +1,28 @@ + + + + + + + bar + + + + + bar source + baz + + + + + baz + foo + + + + + qux source + + + +