From 970b9568b17ecc957ba82553e7395c2a53739197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20Fr=C3=A9cinaux?= Date: Tue, 15 Mar 2016 12:47:29 +0100 Subject: [PATCH] bug #18161 [Translation] Add support for fuzzy tags in PoFileLoader The traditional gettext tools usually try to find similar strings when updating translations. This results in some strings having a translation but being marked as "fuzzy", even if the translation is not correct. The expected result when a string is marked as fuzzy is that it should not be used by the translation system. Using symfony, though, the translations were used, which could result in "funny" messages being displayed in the interface despite being often completely out of context. This commit discards messages in .po files that have a '#, fuzzy' flag. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | 18161 | License | MIT | Doc PR | - --- .../Component/Translation/Loader/PoFileLoader.php | 12 ++++++++++-- .../Translation/Tests/Loader/PoFileLoaderTest.php | 12 ++++++++++++ .../Translation/Tests/fixtures/fuzzy-translations.po | 10 ++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Translation/Tests/fixtures/fuzzy-translations.po diff --git a/src/Symfony/Component/Translation/Loader/PoFileLoader.php b/src/Symfony/Component/Translation/Loader/PoFileLoader.php index b5d12e9821..29e898cc47 100644 --- a/src/Symfony/Component/Translation/Loader/PoFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/PoFileLoader.php @@ -108,14 +108,20 @@ class PoFileLoader extends ArrayLoader $messages = array(); $item = $defaults; + $flags = array(); while ($line = fgets($stream)) { $line = trim($line); if ($line === '') { // Whitespace indicated current item is done - $this->addMessage($messages, $item); + if (!in_array('fuzzy', $flags)) { + $this->addMessage($messages, $item); + } $item = $defaults; + $flags = array(); + } elseif (substr($line, 0, 2) === '#,') { + $flags = array_map('trim', explode(',', substr($line, 2))); } elseif (substr($line, 0, 7) === 'msgid "') { // We start a new msg so save previous // TODO: this fails when comments or contexts are added @@ -141,7 +147,9 @@ class PoFileLoader extends ArrayLoader } } // save last item - $this->addMessage($messages, $item); + if (!in_array('fuzzy', $flags)) { + $this->addMessage($messages, $item); + } fclose($stream); return $messages; diff --git a/src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php b/src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php index 87090eb736..5d340c766f 100644 --- a/src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php +++ b/src/Symfony/Component/Translation/Tests/Loader/PoFileLoaderTest.php @@ -93,4 +93,16 @@ class PoFileLoaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals('escaped "bar"', $messages['escaped "foo"']); $this->assertEquals('escaped "bar"|escaped "bars"', $messages['escaped "foos"']); } + + public function testSkipFuzzyTranslations() + { + $loader = new PoFileLoader(); + $resource = __DIR__.'/../fixtures/fuzzy-translations.po'; + $catalogue = $loader->load($resource, 'en', 'domain1'); + + $messages = $catalogue->all('domain1'); + $this->assertArrayHasKey('foo1', $messages); + $this->assertArrayNotHasKey('foo2', $messages); + $this->assertArrayHasKey('foo3', $messages); + } } diff --git a/src/Symfony/Component/Translation/Tests/fixtures/fuzzy-translations.po b/src/Symfony/Component/Translation/Tests/fixtures/fuzzy-translations.po new file mode 100644 index 0000000000..04d4047aa4 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/fuzzy-translations.po @@ -0,0 +1,10 @@ +#, php-format +msgid "foo1" +msgstr "bar1" + +#, fuzzy, php-format +msgid "foo2" +msgstr "fuzzy bar2" + +msgid "foo3" +msgstr "bar3"