8000 feature #49781 [Translation] Improve message extraction performance f… · symfony/symfony@2e31041 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e31041

Browse files
committed
feature #49781 [Translation] Improve message extraction performance for 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
2 parents c2090d3 + ae6c25b commit 2e31041

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/Symfony/Component/Translation/Extractor/PhpAstExtractor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public function setPrefix(string $prefix): void
6464

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

7072
protected function extractFromDirectory(array|string $resource): iterable|Finder

src/Symfony/Component/Translation/Extractor/Visitor/ConstraintVisitor.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
*/
2222
final class ConstraintVisitor extends AbstractVisitor implements NodeVisitor
2323
{
24-
private const CONSTRAINT_VALIDATION_MESSAGE_PATTERN = '/[a-zA-Z]*message/i';
25-
2624
public function __construct(
2725
private readonly array $constraintClassNames = []
2826
) {
@@ -65,7 +63,7 @@ public function enterNode(Node $node): ?Node
6563
}
6664

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

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

0 commit comments

Comments
 (0)
0