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('/