diff --git a/translations/translationtool/src/translationtool.php b/translations/translationtool/src/translationtool.php index 8a562180..15e24b7e 100644 --- a/translations/translationtool/src/translationtool.php +++ b/translations/translationtool/src/translationtool.php @@ -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(\/\/|\/\*||* /) + * $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; + } } }