From ff7577d6506d1ab7441af99f98ab4c2230589787 Mon Sep 17 00:00:00 2001 From: Stadly Date: Thu, 25 Apr 2019 14:56:02 +0200 Subject: [PATCH] Add sources when dumping qt files --- .../Translation/Dumper/QtFileDumper.php | 11 ++++++++++ .../Tests/Dumper/QtFileDumperTest.php | 21 ++++++++++++++++++- .../Tests/Loader/QtFileLoaderTest.php | 6 +++++- .../Translation/Tests/fixtures/resources.ts | 11 ++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Translation/Dumper/QtFileDumper.php b/src/Symfony/Component/Translation/Dumper/QtFileDumper.php index ec93f92e4a..79a64b2430 100644 --- a/src/Symfony/Component/Translation/Dumper/QtFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/QtFileDumper.php @@ -33,6 +33,17 @@ class QtFileDumper extends FileDumper foreach ($messages->all($domain) as $source => $target) { $message = $context->appendChild($dom->createElement('message')); + $metadata = $messages->getMetadata($source, $domain); + if (isset($metadata['sources'])) { + foreach ((array) $metadata['sources'] as $location) { + $loc = explode(':', $location, 2); + $location = $message->appendChild($dom->createElement('location')); + $location->setAttribute('filename', $loc[0]); + if (isset($loc[1])) { + $location->setAttribute('line', $loc[1]); + } + } + } $message->appendChild($dom->createElement('source', $source)); $message->appendChild($dom->createElement('translation', $target)); } diff --git a/src/Symfony/Component/Translation/Tests/Dumper/QtFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/QtFileDumperTest.php index edfad6005c..6c4b559278 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/QtFileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/QtFileDumperTest.php @@ -20,7 +20,26 @@ class QtFileDumperTest extends TestCase public function testFormatCatalogue() { $catalogue = new MessageCatalogue('en'); - $catalogue->add(['foo' => 'bar'], 'resources'); + $catalogue->add(['foo' => 'bar', 'foo_bar' => 'foobar', 'bar_foo' => 'barfoo'], 'resources'); + $catalogue->setMetadata('foo_bar', [ + 'comments' => [ + 'Comment 1', + 'Comment 2', + ], + 'flags' => [ + 'fuzzy', + 'another', + ], + 'sources' => [ + 'src/file_1', + 'src/file_2:50', + ], + ], 'resources'); + $catalogue->setMetadata('bar_foo', [ + 'comments' => 'Comment', + 'flags' => 'fuzzy', + 'sources' => 'src/file_1', + ], 'resources'); $dumper = new QtFileDumper(); diff --git a/src/Symfony/Component/Translation/Tests/Loader/QtFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/QtFileLoaderTest.php index 08f55e9022..f149b8c715 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/QtFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/QtFileLoaderTest.php @@ -23,7 +23,11 @@ class QtFileLoaderTest extends TestCase $resource = __DIR__.'/../fixtures/resources.ts'; $catalogue = $loader->load($resource, 'en', 'resources'); - $this->assertEquals(['foo' => 'bar'], $catalogue->all('resources')); + $this->assertEquals([ + 'foo' => 'bar', + 'foo_bar' => 'foobar', + 'bar_foo' => 'barfoo', + ], $catalogue->all('resources')); $this->assertEquals('en', $catalogue->getLocale()); $this->assertEquals([new FileResource($resource)], $catalogue->getResources()); } diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resources.ts b/src/Symfony/Component/Translation/Tests/fixtures/resources.ts index 40e18522c8..29e6a6fadf 100644 --- a/src/Symfony/Component/Translation/Tests/fixtures/resources.ts +++ b/src/Symfony/Component/Translation/Tests/fixtures/resources.ts @@ -6,5 +6,16 @@ foo bar + + + + foo_bar + foobar + + + + bar_foo + barfoo +