Skip to content

Commit

Permalink
Merge pull request #619 from nextcloud/fix/vue-comments
Browse files Browse the repository at this point in the history
fix(translations): extract comments from vue files
  • Loading branch information
nickvergessen authored Jan 12, 2024
2 parents 34cf683 + dc7d63f commit ff717fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions translations/translationtool/src/translationtool.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,36 @@ private function getTranslatorHintWithVueSource(string $vueFile, string $content

$position = strpos($content, $translation);
$contentBefore = substr($content, 0, $position);
$contentAfter = substr($content, $position);
$lineNumber = substr_count($contentBefore, "\n") + 1;

$linesBefore = explode("\n", $contentBefore);
$linesAfter = explode("\n", $contentAfter);
$previousLine = $linesBefore[$lineNumber - 2];
$currentLine = $linesAfter[0];

// If we have a translation hint in the current line, we use it
// This prevents mismatching hints if we have two translations
// over consecutive lines
// Like https://github.com/nextcloud/forms/blob/5c905b36b1ce3ca1848175d39d813581732e159d/src/views/Results.vue#L261-L263
$searchComment = strpos($currentLine, 'TRANSLATORS') !== false
? $currentLine
: $previousLine;

// We try to find a comment with the translators hint
// <!-- TRANSLATORS: This is a comment -->
// <!-- TRANSLATORS : This is a comment -->
// <!-- TRANSLATORS This is a comment -->
// t('forms', 'Save to home') // TRANSLATORS: Export the file to the home path
$re = '/TRANSLATORS[: ]*(.*)(-->|\n)?/m';
preg_match_all($re, $searchComment, $matches, PREG_SET_ORDER, 0);

// If we have a comment, we use it
if (count($matches) > 0 && count($matches[0]) > 1) {
// Remove double spaces
return str_replace(' ', ' ', '// TRANSLATORS ' . $matches[0][1] . " ($relativeVuePath:$lineNumber)" . PHP_EOL);
}

return '// TRANSLATORS ' . $relativeVuePath . ':' . $lineNumber . PHP_EOL;
}

Expand Down
Binary file modified translations/translationtool/translationtool.phar
Binary file not shown.

0 comments on commit ff717fb

Please sign in to comment.