Skip to content

Commit

Permalink
bug #18175 [Translation] Add support for fuzzy tags in PoFileLoader (…
Browse files Browse the repository at this point in the history
…nud)

This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #18175).

Discussion
----------

[Translation] Add support for fuzzy tags in PoFileLoader

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18161
| License       | MIT
| Doc PR        | -

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.

This commit can (and imo should) be back-ported as-is to the 2.x branch.

Commits
-------

970b956 bug #18161 [Translation] Add support for fuzzy tags in PoFileLoader
  • Loading branch information
fabpot committed Mar 15, 2016
2 parents 18615fc + 970b956 commit 4fece28
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Symfony/Component/Translation/Loader/PoFileLoader.php
Expand Up @@ -108,14 +108,20 @@ private function parse($resource)

$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
Expand All @@ -141,7 +147,9 @@ private function parse($resource)
}
}
// save last item
$this->addMessage($messages, $item);
if (!in_array('fuzzy', $flags)) {
$this->addMessage($messages, $item);
}
fclose($stream);

return $messages;
Expand Down
Expand Up @@ -93,4 +93,16 @@ public function testEscapedIdPlurals()
$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);
}
}
@@ -0,0 +1,10 @@
#, php-format
msgid "foo1"
msgstr "bar1"

#, fuzzy, php-format
msgid "foo2"
msgstr "fuzzy bar2"

msgid "foo3"
msgstr "bar3"

0 comments on commit 4fece28

Please sign in to comment.