Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(translationtool): Allow vue-translator comments #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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