From ab3d8d08d0abe950b71d0a637caada1b75dd8799 Mon Sep 17 00:00:00 2001 From: David Dreschner Date: Sat, 28 Nov 2026 18:50:57 +0100 Subject: [PATCH] fix(translationtool): extract every string of a Vue template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The patterns for t() and n() in Vue templates matched each text greedily up to the last quote of the line. A second call on a line was swallowed by the first, which broke the generated file whenever the text between them wasn't valid JavaScript. t('app', "…") and an n() whose forms use different quotes matched none of the patterns and were dropped without any warning. Each text is now a list of string literals joined with +, each ending at its own closing quote, in any kind of quotes. Like xgettext, comments may stand between the tokens of a call, and a text ends before a + followed by anything other than a string. The quantifiers are possessive, because otherwise the comments allowed between tokens exhaust PCRE's backtrack limit, and a text with thousands of escapes or joined parts its JIT stack. A failing pattern now stops the run instead of dropping strings, also deleting the generated files that would otherwise stay in the app. The hint for each call now comes from its own position. Searching for the call's text found the first occurrence, so two equal calls on different lines both got the comment and location of the first one. A TRANSLATORS comment on the line above or in the rest of the line only reaches the first string of the line, as xgettext does with one on the line above; the others get their location like any string without a hint. The location now names the line of the call also when the call starts a line, and a comment ends at its --> or */ instead of keeping what follows it. The calls stay grouped by the quotes of their first text, so the order of the existing strings in the POT only changes where a call has another call in its arguments or the old pattern wrote broken code. On Mail and the server's apps this adds nine strings, completes one that was cut off at its first quote, and otherwise only adds or cleans up hints. Left as they are: a template literal with ${…} and the whitespace inside a text are written as before, although they differ from the text at runtime, because changing them would change existing msgids. A comment inside a template expression is assigned by its line, not by its position as xgettext does: a TRANSLATORS comment between two calls on a line now reaches the first one, where the old pattern happened to give it to the second. Changing that also changes the lint's comment rules. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: David Dreschner --- .../translationtool/src/translationtool.php | 217 ++++++------ .../translationtool/tests/expected-lint.txt | 113 +++--- .../translationtool/tests/expected.pot | 331 ++++++++++++++++-- .../translationtool/tests/src/lint.vue | 10 +- .../translationtool/tests/src/vue.vue | 32 ++ translations/translationtool/tests/test.sh | 9 + .../translationtool/translationtool.phar | Bin 456039 -> 456737 bytes 7 files changed, 533 insertions(+), 179 deletions(-) mode change 100755 => 100644 translations/translationtool/translationtool.phar diff --git a/translations/translationtool/src/translationtool.php b/translations/translationtool/src/translationtool.php index 45bdcc4c..0fbd8b03 100644 --- a/translations/translationtool/src/translationtool.php +++ b/translations/translationtool/src/translationtool.php @@ -33,12 +33,20 @@ class TranslatableApp { private TranslationTool $tool; /** @var array> lines of each generated file: source file relative to the working directory, its line and the language of its strings */ private array $fakeFileSources = []; - /** @var array>> line of each Vue template string by file, with the lines getTranslatorHintWithVueSource() reads its comment from: the line above the call and the rest of the line from the call */ + /** @var array>> line of each Vue template string by file, with the lines createFakeFileForVueFiles() reads its comment from: the line above the call and the rest of the line from the call */ private array $vueTemplateLines = []; - /** @var array> lines by file whose TRANSLATORS comment getTranslatorHintWithVueSource() gives to a template string on the next line */ + /** @var array> lines by file whose TRANSLATORS comment createFakeFileForVueFiles() gives to a template string on the next line */ private array $vueCommentLines = []; - /** @var array hints that getTranslatorHintWithVueSource() writes for Vue strings without a TRANSLATORS comment, which only name the source */ + /** @var array hints that createFakeFileForVueFiles() writes for Vue strings without a TRANSLATORS comment, which only name the source */ private array $generatedVueHints = []; + /** A JavaScript string literal in single quotes, double quotes or backticks, possessive so a long text doesn't exhaust PCRE's stack */ + private const VUE_LITERAL = '(?:\'(?:[^\'\\\\]++|\\\\.)*+\'|"(?:[^"\\\\]++|\\\\.)*+"|`(?:[^`\\\\]++|\\\\.)*+`)'; + /** A JavaScript comment */ + private const VUE_COMMENT = '(?:\/\*(?:[^*]++|\*(?!\/))*+\*\/|\/\/[^\n]*+)'; + /** Whitespace and comments, which JavaScript allows between the tokens of a call */ + private const VUE_SPACE = '(?:\s++|' . self::VUE_COMMENT . ')*+'; + /** A text of a Vue template call: string literals joined with + */ + private const VUE_TEXT = '(' . self::VUE_LITERAL . '(?:' . self::VUE_SPACE . '\+' . self::VUE_SPACE . self::VUE_LITERAL . ')*+)'; /** Start of a comment xgettext reads: // not after a : or a quote as in URLs, and # between whitespace, unlike #000 or #[Attribute] */ private const LINE_COMMENT_MARKER = '(?potFile(); - // Gather required data - $this->createFakeFileForAppInfo(); - $this->createFakeFileForVueFiles(); - $this->createFakeFileForLocale(); - $translatableFiles = $this->findTranslatableFiles( - ['.php', '.js', '.jsx', '.mjs', '.html', '.ts', '.tsx', '.py'], - ['.min.js'] - ); - - // Let gettext create the pot file try { + // Gather required data + $this->createFakeFileForAppInfo(); + $this->createFakeFileForVueFiles(); + $this->createFakeFileForLocale(); + $translatableFiles = $this->findTranslatableFiles( + ['.php', '.js', '.jsx', '.mjs', '.html', '.ts', '.tsx', '.py'], + ['.min.js'] + ); + + // Let gettext create the pot file $this->extractTranslatableFiles($translatableFiles, $pathToPotFile, $checkFiles); } finally { // Don't forget to remove the temporary file @@ -118,13 +126,16 @@ private function extractTranslatableFiles(array $translatableFiles, string $path if ($checkFiles) { foreach ($translatableFiles as $entry) { $tmpfname = $this->temporaryFile('checkpot'); - // extract-all generates a recurrent warning - if ($this->extractStrings([$entry], $tmpfname, false, '--extract-all 2>/dev/null')) { - $this->checkMissingTranslations($entry, $tmpfname); - } else { - echo 'Warning: could not extract the strings of ' . $entry . PHP_EOL; + try { + // extract-all generates a recurrent warning + if ($this->extractStrings([$entry], $tmpfname, false, '--extract-all 2>/dev/null')) { + $this->checkMissingTranslations($entry, $tmpfname); + } else { + echo 'Warning: could not extract the strings of ' . $entry . PHP_EOL; + } + } finally { + unlink($tmpfname); } - unlink($tmpfname); } } else { // One call per run of files in the same language keeps the order of the pot file without rewriting it for every file @@ -511,49 +522,79 @@ private function createFakeFileForVueFiles(): void { $relativeVuePath = $this->relativePath($vueFile); $matches = []; - if (preg_match_all('/]*>(.+)<\/script>/sU', $vueSource, $matches, PREG_OFFSET_CAPTURE)) { + $scripts = preg_match_all('/]*>(.+)<\/script>/sU', $vueSource, $matches, PREG_OFFSET_CAPTURE); + if ($scripts === false) { + throw $this->vuePatternFailure($vueFile); + } + if ($scripts > 0) { foreach ($matches[1] as [$script, $offset]) { $append($script, $relativeVuePath, $this->lineNumber($vueSource, $offset)); } $append(";\n"); } - if (preg_match('/