Skip to content

Commit

Permalink
Allow vue-Translators Comments
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Rittershofer <[email protected]>
  • Loading branch information
jotoeri committed Mar 22, 2021
1 parent 546b481 commit 932b8d7
Showing 1 changed file with 64 additions and 19 deletions.
83 changes: 64 additions & 19 deletions translations/translationtool/src/translationtool.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,25 +326,70 @@ private function createFakeFileForVueFiles() {
continue;
}

// t
preg_match_all("/\Wt\s*\(\s*'([\w]+)',\s*'(.+)'/", $vueSource, $singleQuoteMatches);
preg_match_all("/\Wt\s*\(\s*\"([\w]+)\",\s*\"(.+)\"/", $vueSource, $doubleQuoteMatches);
preg_match_all("/\Wt\s*\(\s*\'([\w]+)\'\s*,\s*\`(.+)\`\s*\)/msU", $vueSource, $templateQuoteMatches);
$matches = array_merge($singleQuoteMatches[2], $doubleQuoteMatches[2], $templateQuoteMatches[2]);
foreach ($matches as $match) {
$fakeFileContent .= "t('" . $this->name . "', '" . preg_replace('/\s+/', ' ', $match) . "');" . PHP_EOL;
}

// n
preg_match_all("/\Wn\s*\(\s*'([\w]+)',\s*'(.+)'\s*,\s*'(.+)'\s*(.+)/", $vueSource, $singleQuoteMatches);
preg_match_all("/\Wn\s*\(\s*\"([\w]+)\",\s*\"(.+)\"\s*,\s*\"(.+)\"\s*(.+)/", $vueSource, $doubleQuoteMatches);
preg_match_all("/\Wn\s*\(\s*\'([\w]+)\'\s*,\s*\`(.+)\`\s*,\s*\`(.+)\`\s*\)/msU", $vueSource, $templateQuoteMatches);
$matches1 = array_merge($singleQuoteMatches[2], $doubleQuoteMatches[2], $templateQuoteMatches[2]);
$matches2 = array_merge($singleQuoteMatches[3], $doubleQuoteMatches[3], $templateQuoteMatches[3]);
foreach (array_keys($matches1) as $k) {
$match1 = $matches1[$k];
$match2 = $matches2[$k];
$fakeFileContent .= "n('" . $this->name . "', '" . preg_replace('/\s+/', ' ', $match1) . "', '" . preg_replace('/\s+/', ' ', $match2) . "');" . PHP_EOL;
// Optional TRANSLATORS-comment
$translatorsRegex = "(\W(\/\/|\/\*|<!--)\s(TRANSLATORS)\s(.+))*";
// Text between comment and translation-function
$inbetweenRegex = "(\s*.*\s*)";

/**
* t - Singular Translations
*/
// Regex for translation-function t. Items Contain Regex-Ending '/' as template quotes needs special ending
$tTranslationRegex = [
"\Wt\s*\(\s*'([\w]+)',\s*'(.+)'" . "/", // Single Quotes
"\Wt\s*\(\s*\"([\w]+)\",\s*\"(.+)\"" . "/", // Double Quotes
"\Wt\s*\(\s*\'([\w]+)\'\s*,\s*\`(.+)\`\s*\)" . "/msU" // Template Quotes
];
foreach ($tTranslationRegex as $tRegex) {
// Regex-Ending within tRegex, see array above.
preg_match_all("/". $translatorsRegex . $inbetweenRegex . $tRegex, $vueSource, $matches);

/* The variable $matches now contains 8 Arrays corresponding to the Regex-Groups, out of which:
* $matches[0] Holds an array of the whole Matches
* $matches[4] Holds an array of the comments, but still including a possible comment-ending (-->|* /)
* $matches[7] Holds an array of the texts to translate
* All inner arrays contain the respective content with corresponding indices.
* So $matches[4][2] is the corresponding comment to the text in $matches[7][2]
*/
foreach ($matches[0] as $index => $fullMatch) {
if ($matches[4][$index] !== '') {
// Replace end-tags by nothing
$comment = preg_replace('/\s*(-->|\*\/)\s*/', '', $matches[4][$index]);
$fakeFileContent .= "// TRANSLATORS " . $comment . PHP_EOL;
}
$fakeFileContent .= "t('" . $this->name . "', '" . preg_replace('/\s+/', ' ', $matches[7][$index]) . "');" . PHP_EOL;
}
};

/**
* n - Plural translations
*/
// Regex for translation-function n. Items Contain Regex-Ending '/' as template quotes needs special ending
$nTranslationRegex = [
"\Wn\s*\(\s*'([\w]+)',\s*'(.+)'\s*,\s*'(.+)'\s*(.+)" . "/", // Single Quotes
"\Wn\s*\(\s*\"([\w]+)\",\s*\"(.+)\"\s*,\s*\"(.+)\"\s*(.+)" . "/", // Double Quotes
"\Wn\s*\(\s*\'([\w]+)\'\s*,\s*\`(.+)\`\s*,\s*\`(.+)\`\s*\)" . "/msU" // Template Quotes
];
foreach ($nTranslationRegex as $nRegex) {
// Regex-Ending within tRegex, see array above.
preg_match_all("/". $translatorsRegex . $inbetweenRegex . $nRegex, $vueSource, $matches);

/* The variable $matches now contains 8 Arrays corresponding to the Regex-Groups, out of which:
* $matches[0] Holds an array of the whole Matches
* $matches[4] Holds an array of the comments, but still including a possible comment-ending (-->|* /)
* $matches[7] Holds an array of the singular form of the text to translate
* $matches[8] Holds an array of the plural form of the text to translate
* All inner arrays contain the respective content with corresponding indices.
* So $matches[4][2] is the corresponding comment to the text in $matches[7][2]
*/
foreach ($matches[0] as $index => $fullMatch) {
if ($matches[4][$index] !== '') {
// Replace end-tags by nothing
$comment = preg_replace('/\s*(-->|\*\/)\s*/', '', $matches[4][$index]);
$fakeFileContent .= "// TRANSLATORS " . $comment . PHP_EOL;
}
$fakeFileContent .= "n('" . $this->name . "', '" . preg_replace('/\s+/', ' ', $matches[7][$index]) . "', '" . preg_replace('/\s+/', ' ', $matches[8][$index]) . "');" . PHP_EOL;
}
}
}

Expand Down

0 comments on commit 932b8d7

Please sign in to comment.