Skip to content
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

v4: Prevent adding the same translator comment to multiple functions #271

Merged
merged 2 commits into from
Jul 13, 2021
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"gettext/languages": "^2.3"
},
"require-dev": {
"illuminate/view": "*",
"illuminate/view": "^5.0.x-dev",
"twig/twig": "^1.31|^2.0",
"twig/extensions": "*",
"symfony/yaml": "~2",
Expand Down
16 changes: 15 additions & 1 deletion src/Utils/FunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public function saveGettextFunctions($translations, array $options)
$functions = $options['functions'];
$file = $options['file'];

/**
* List of source code comments already associated with a function.
*
* Prevents associating the same comment to multiple functions.
*
* @var ParsedComment[] $commentsCache
*/
$commentsCache = [];

foreach ($this->getFunctions($options['constants']) as $function) {
list($name, $line, $args) = $function;

Expand Down Expand Up @@ -78,8 +87,13 @@ public function saveGettextFunctions($translations, array $options)
$translation->addReference($file, $line);

if (isset($function[3])) {
/* @var ParsedComment $extractedComment */
foreach ($function[3] as $extractedComment) {
$translation->addExtractedComment($extractedComment);
if (in_array($extractedComment, $commentsCache, true)) {
continue;
}
$translation->addExtractedComment($extractedComment->getComment());
$commentsCache[] = $extractedComment;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/ParsedFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ParsedFunction
/**
* Extracted comments.
*
* @var string[]|null
* @var ParsedComment[]|null
*/
protected $comments;

Expand Down Expand Up @@ -112,7 +112,7 @@ public function addArgumentChunk($chunk)
/**
* Add a comment associated to this function.
*
* @param string $comment
* @param ParsedComment $comment
*/
public function addComment($comment)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/PhpFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getFunctions(array $constants = [])
$comment = $bufferComments[0];

if ($comment->isRelatedWith($newFunction)) {
$newFunction->addComment($comment->getComment());
$newFunction->addComment($comment);
}
}

Expand All @@ -148,7 +148,7 @@ public function getFunctions(array $constants = [])

// The comment is inside the function call.
if (isset($bufferFunctions[0])) {
$bufferFunctions[0]->addComment($comment->getComment());
$bufferFunctions[0]->addComment($comment);
}
}
break;
Expand Down
2 changes: 1 addition & 1 deletion tests/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function testPhpCode2()
'CONTEXT' => 'my-context',
],
]);
$countTranslations = 14;
$countTranslations = 19;
$countTranslated = 0;
$countHeaders = 8;

Expand Down
5 changes: 5 additions & 0 deletions tests/assets/phpcode2/Csv.csv
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ my-context,"All comments",
,foo,
,bar,
,"foo bar",
,World,
,"No comment",
,"Within printf: %s",
,placeholder,
,"After printf",
5 changes: 5 additions & 0 deletions tests/assets/phpcode2/CsvDictionary.csv
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ plain,
foo,
bar,
"foo bar",
World,
"No comment",
"Within printf: %s",
placeholder,
"After printf",
15 changes: 15 additions & 0 deletions tests/assets/phpcode2/Jed.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@
],
"foo bar": [
""
],
"World": [
""
],
"No comment": [
""
],
"Within printf: %s": [
""
],
"placeholder": [
""
],
"After printf": [
""
]
}
}
15 changes: 15 additions & 0 deletions tests/assets/phpcode2/Json.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@
],
"foo bar": [
""
],
"World": [
""
],
"No comment": [
""
],
"Within printf: %s": [
""
],
"placeholder": [
""
],
"After printf": [
""
]
},
"my-context": {
Expand Down
7 changes: 6 additions & 1 deletion tests/assets/phpcode2/JsonDictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
"i18n tagged %s": "",
"foo": "",
"bar": "",
"foo bar": ""
"foo bar": "",
"World": "",
"No comment": "",
"Within printf: %s": "",
"placeholder": "",
"After printf": ""
}
20 changes: 20 additions & 0 deletions tests/assets/phpcode2/PhpArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@
array (
0 => '',
),
'World' =>
array (
0 => '',
),
'No comment' =>
array (
0 => '',
),
'Within printf: %s' =>
array (
0 => '',
),
'placeholder' =>
array (
0 => '',
),
'After printf' =>
array (
0 => '',
),
),
'my-context' =>
array (
Expand Down
22 changes: 22 additions & 0 deletions tests/assets/phpcode2/Po.po
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,25 @@ msgstr ""
#: ./tests/assets/phpcode2/input.php:51
msgid "foo bar"
msgstr ""

#. translators: Hello
#: ./tests/assets/phpcode2/input.php:54
msgid "World"
msgstr ""

#: ./tests/assets/phpcode2/input.php:55
msgid "No comment"
msgstr ""

#. translators: Only used once
#: ./tests/assets/phpcode2/input.php:57
msgid "Within printf: %s"
msgstr ""

#: ./tests/assets/phpcode2/input.php:57
msgid "placeholder"
msgstr ""

#: ./tests/assets/phpcode2/input.php:57
msgid "After printf"
msgstr ""
52 changes: 52 additions & 0 deletions tests/assets/phpcode2/Xliff.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,57 @@
<target></target>
</segment>
</unit>
<unit id="f5a7924e621e84c9280a9a27e1bcb7f6">
<notes>
<note category="context"></note>
<note category="extracted-comment">translators: Hello</note>
<note category="reference">./tests/assets/phpcode2/input.php:54</note>
</notes>
<segment>
<source>World</source>
<target></target>
</segment>
</unit>
<unit id="84ed1c865d4156e47b203fdca60efd4f">
<notes>
<note category="context"></note>
<note category="reference">./tests/assets/phpcode2/input.php:55</note>
</notes>
<segment>
<source>No comment</source>
<target></target>
</segment>
</unit>
<unit id="01ef55328bf7dc83a387cbdc0c34eb98">
<notes>
<note category="context"></note>
<note category="extracted-comment">translators: Only used once</note>
<note category="reference">./tests/assets/phpcode2/input.php:57</note>
</notes>
<segment>
<source>Within printf: %s</source>
<target></target>
</segment>
</unit>
<unit id="6a99c575ab87f8c7d1ed1e52e7e349ce">
<notes>
<note category="context"></note>
<note category="reference">./tests/assets/phpcode2/input.php:57</note>
</notes>
<segment>
<source>placeholder</source>
<target></target>
</segment>
</unit>
<unit id="bd557c72f14c06eaa44bd2f23836d90a">
<notes>
<note category="context"></note>
<note category="reference">./tests/assets/phpcode2/input.php:57</note>
</notes>
<segment>
<source>After printf</source>
<target></target>
</segment>
</unit>
</file>
</xliff>
5 changes: 5 additions & 0 deletions tests/assets/phpcode2/Yaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ messages:
foo: ''
bar: ''
'foo bar': ''
World: ''
'No comment': ''
'Within printf: %s': ''
placeholder: ''
'After printf': ''
my-context:
'All comments': ''
5 changes: 5 additions & 0 deletions tests/assets/phpcode2/YamlDictionary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ plain: ''
foo: ''
bar: ''
'foo bar': ''
World: ''
'No comment': ''
'Within printf: %s': ''
placeholder: ''
'After printf': ''
5 changes: 5 additions & 0 deletions tests/assets/phpcode2/input.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ function foo() {
*/
__( 'foo bar' );
}

printf( /* translators: Hello */ __( 'World' ) );
__( 'No comment' );

printf( /* translators: Only used once */ __( 'Within printf: %s' ), __( 'placeholder' ) ); __( 'After printf' );