8000 [Translation] Add support for fuzzy tags in PoFileLoader by nud · Pull Request #18175 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Translation] Add support for fuzzy tags in PoFileLoader #18175

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

Closed
wants to merge 1 commit into from
Closed
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
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        | -
  • Loading branch information
nud committed Mar 15, 2016
commit ede617d77449fe8ee1af4a9477550bb82e97a0a9
12 changes: 10 additions & 2 deletions src/Symfony/Component/Translation/Loader/PoFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ protected function loadResource($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 @@ -104,7 +110,9 @@ protected function loadResource($resource)
}
}
// save last item
$this->addMessage($messages, $item);
if (!in_array('fuzzy', $flags)) {
$this->addMessage($messages, $item);
}
fclose($stream);

return $messages;
Expand Down
Original file line number Diff line number Diff line change
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#, php-format
msgid "foo1"
msgstr "bar1"

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

msgid "foo3"
msgstr "bar3"
0