Skip to content

Commit

Permalink
feature #49781 [Translation] Improve message extraction performance f…
Browse files Browse the repository at this point in the history
…or big code bases (welcoMattic)

This PR was merged into the 6.3 branch.

Discussion
----------

[Translation] Improve message extraction performance for big code bases

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #49585 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT
| Doc PR        |

As discussed in #49585, we try to extract messages from all src/ directory in `translation:debug` and `translation:extract` commands. But, it's not necessary to look into all files, as we can easily detect if a file contains something about translation (with some regexes).

This PR filter files before messages extraction, and it divide by at least 3 the time used to extract messages from an app with 100 000 php files.

Commits
-------

ae6c25b fix(translation): Improve performance of debug and extract command for big code base
  • Loading branch information
fabpot committed Mar 25, 2023
2 parents c2090d3 + ae6c25b commit 2e31041
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public function setPrefix(string $prefix): void

protected function canBeExtracted(string $file): bool
{
return 'php' === pathinfo($file, \PATHINFO_EXTENSION) && $this->isFile($file);
return 'php' === pathinfo($file, \PATHINFO_EXTENSION)
&& $this->isFile($file)
&& preg_match('/\bt\(|->trans\(|TranslatableMessage|Symfony\\\\Component\\\\Validator\\\\Constraints/i', file_get_contents($file));
}

protected function extractFromDirectory(array|string $resource): iterable|Finder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*/
final class ConstraintVisitor extends AbstractVisitor implements NodeVisitor
{
private const CONSTRAINT_VALIDATION_MESSAGE_PATTERN = '/[a-zA-Z]*message/i';

public function __construct(
private readonly array $constraintClassNames = []
) {
Expand Down Expand Up @@ -65,7 +63,7 @@ public function enterNode(Node $node): ?Node
}

if ($this->hasNodeNamedArguments($node)) {
$messages = $this->getStringArguments($node, self::CONSTRAINT_VALIDATION_MESSAGE_PATTERN, true);
$messages = $this->getStringArguments($node, '/message/i', true);
} else {
if (!$arg->value instanceof Node\Expr\Array_) {
// There is no way to guess which argument is a message to be translated.
Expand All @@ -81,7 +79,7 @@ public function enterNode(Node $node): ?Node
continue;
}

if (!preg_match(self::CONSTRAINT_VALIDATION_MESSAGE_PATTERN, $item->key->value ?? '')) {
if (false === stripos($item->key->value ?? '', 'message')) {
continue;
}

Expand Down

0 comments on commit 2e31041

Please sign in to comment.