Skip to content

Commit d1c4f25

Browse files
Merge pull request #731 from nextcloud/fix/translation-tool
fix(translationtool): Only handle vue template with custom parser
2 parents 3cd3cc0 + f5562bf commit d1c4f25

File tree

4 files changed

+80
-31
lines changed

4 files changed

+80
-31
lines changed

translations/translationtool/src/translationtool.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -361,30 +361,41 @@ private function createFakeFileForVueFiles(): void {
361361
continue;
362362
}
363363

364-
// t
365-
preg_match_all("/\Wt\s*\(\s*'?([\w.]+)'?,\s*'(.+)'\s*[),]/", $vueSource, $singleQuoteMatches);
366-
preg_match_all("/\Wt\s*\(\s*\"?([\w.]+)\"?,\s*\"(.+)\"\s*[),]/", $vueSource, $doubleQuoteMatches);
367-
preg_match_all("/\Wt\s*\(\s*'?([\w.]+)'?\s*,\s*`(.+)`\s*[),]/msU", $vueSource, $templateQuoteMatches);
368-
$matches0 = array_merge($singleQuoteMatches[0], $doubleQuoteMatches[0], $templateQuoteMatches[0]);
369-
$matches2 = array_merge($singleQuoteMatches[2], $doubleQuoteMatches[2], $templateQuoteMatches[2]);
370-
foreach (array_keys($matches2) as $k) {
371-
$match = $matches2[$k];
372-
$fakeFileContent .= $this->getTranslatorHintWithVueSource($vueFile, $vueSource, $matches0[$k]);
373-
$fakeFileContent .= "t('" . $this->name . "', '" . preg_replace('/\s+/', ' ', $match) . "');" . PHP_EOL;
364+
$matches = [];
365+
if (preg_match('/<script[^>]*>(.+)<\/script>/s', $vueSource, $matches)) {
366+
$fakeFileContent .= $matches[1] . ";\n";
374367
}
375368

376-
// n
377-
preg_match_all("/\Wn\s*\(\s*'?([\w.]+)'?,\s*'(.+)'\s*,\s*'(.+)'\s*(.+)\s*[),]/", $vueSource, $singleQuoteMatches);
378-
preg_match_all("/\Wn\s*\(\s*\"?([\w.]+)\"?,\s*\"(.+)\"\s*,\s*\"(.+)\"\s*(.+)\s*[),]/", $vueSource, $doubleQuoteMatches);
379-
preg_match_all("/\Wn\s*\(\s*'?([\w.]+)'?\s*,\s*`(.+)`\s*,\s*`(.+)`\s*[),]/msU", $vueSource, $templateQuoteMatches);
380-
$matches0 = array_merge($singleQuoteMatches[0], $doubleQuoteMatches[0], $templateQuoteMatches[0]);
381-
$matches2 = array_merge($singleQuoteMatches[2], $doubleQuoteMatches[2], $templateQuoteMatches[2]);
382-
$matches3 = array_merge($singleQuoteMatches[3], $doubleQuoteMatches[3], $templateQuoteMatches[3]);
383-
foreach (array_keys($matches2) as $k) {
384-
$match2 = $matches2[$k];
385-
$match3 = $matches3[$k];
386-
$fakeFileContent .= $this->getTranslatorHintWithVueSource($vueFile, $vueSource, $matches0[$k]);
387-
$fakeFileContent .= "n('" . $this->name . "', '" . preg_replace('/\s+/', ' ', $match2) . "', '" . preg_replace('/\s+/', ' ', $match3) . "');" . PHP_EOL;
369+
if (preg_match('/<template>(.+)<\/template>/s', $vueSource, $matches, PREG_OFFSET_CAPTURE)) {
370+
// Also parse the template but make sure we keep the correct line numbers for source references
371+
$vueSource = str_repeat("\n", substr_count($vueSource, "\n", 0, $matches[1][1]));
372+
$vueSource .= $matches[1][0];
373+
374+
// t
375+
preg_match_all("/\Wt\s*\(\s*'?([\w.]+)'?,\s*'(.+)'\s*[),]/", $vueSource, $singleQuoteMatches);
376+
preg_match_all("/\Wt\s*\(\s*\"?([\w.]+)\"?,\s*\"(.+)\"\s*[),]/", $vueSource, $doubleQuoteMatches);
377+
preg_match_all("/\Wt\s*\(\s*'?([\w.]+)'?\s*,\s*`(.+)`\s*[),]/msU", $vueSource, $templateQuoteMatches);
378+
$matches0 = array_merge($singleQuoteMatches[0], $doubleQuoteMatches[0], $templateQuoteMatches[0]);
379+
$matches2 = array_merge($singleQuoteMatches[2], $doubleQuoteMatches[2], $templateQuoteMatches[2]);
380+
foreach (array_keys($matches2) as $k) {
381+
$match = $matches2[$k];
382+
$fakeFileContent .= $this->getTranslatorHintWithVueSource($vueFile, $vueSource, $matches0[$k]);
383+
$fakeFileContent .= "t('" . $this->name . "', '" . preg_replace('/\s+/', ' ', $match) . "');" . PHP_EOL;
384+
}
385+
386+
// n
387+
preg_match_all("/\Wn\s*\(\s*'?([\w.]+)'?,\s*'(.+)'\s*,\s*'(.+)'\s*(.+)\s*[),]/", $vueSource, $singleQuoteMatches);
388+
preg_match_all("/\Wn\s*\(\s*\"?([\w.]+)\"?,\s*\"(.+)\"\s*,\s*\"(.+)\"\s*(.+)\s*[),]/", $vueSource, $doubleQuoteMatches);
389+
preg_match_all("/\Wn\s*\(\s*'?([\w.]+)'?\s*,\s*`(.+)`\s*,\s*`(.+)`\s*[),]/msU", $vueSource, $templateQuoteMatches);
390+
$matches0 = array_merge($singleQuoteMatches[0], $doubleQuoteMatches[0], $templateQuoteMatches[0]);
391+
$matches2 = array_merge($singleQuoteMatches[2], $doubleQuoteMatches[2], $templateQuoteMatches[2]);
392+
$matches3 = array_merge($singleQuoteMatches[3], $doubleQuoteMatches[3], $templateQuoteMatches[3]);
393+
foreach (array_keys($matches2) as $k) {
394+
$match2 = $matches2[$k];
395+
$match3 = $matches3[$k];
396+
$fakeFileContent .= $this->getTranslatorHintWithVueSource($vueFile, $vueSource, $matches0[$k]);
397+
$fakeFileContent .= "n('" . $this->name . "', '" . preg_replace('/\s+/', ' ', $match2) . "', '" . preg_replace('/\s+/', ' ', $match3) . "');" . PHP_EOL;
398+
}
388399
}
389400
}
390401

translations/translationtool/tests/expected.pot

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,51 +45,69 @@ msgid_plural "PHP %n Plurals with positional %1$s"
4545
msgstr[0] ""
4646
msgstr[1] ""
4747

48+
#: specialVueFakeDummyForL10nScript.js:8
49+
msgid "one string"
50+
msgid_plural "%n strings"
51+
msgstr[0] ""
52+
msgstr[1] ""
53+
4854
#. TRANSLATORS VUE string with plain text (src/vue.vue:8)
49-
#: specialVueFakeDummyForL10nScript.js:2
55+
#: specialVueFakeDummyForL10nScript.js:17
5056
msgid "VUE String"
5157
msgstr ""
5258

5359
#. TRANSLATORS VUE string with parameters (src/vue.vue:10)
54-
#: specialVueFakeDummyForL10nScript.js:4
60+
#: specialVueFakeDummyForL10nScript.js:19
5561
msgid "VUE String with inline {parameter}"
5662
msgstr ""
5763

5864
#. TRANSLATORS VUE string with wrapped parameters (src/vue.vue:12)
59-
#: specialVueFakeDummyForL10nScript.js:6
65+
#: specialVueFakeDummyForL10nScript.js:21
6066
msgid "VUE String with wrapped {parameter}"
6167
msgstr ""
6268

6369
#. TRANSLATORS VUE Fix for https://github.com/nextcloud/docker-ci/pull/663 - Part 1 (src/vue.vue:26)
64-
#: specialVueFakeDummyForL10nScript.js:8
70+
#: specialVueFakeDummyForL10nScript.js:23
6571
msgid "VUE String testing"
6672
msgstr ""
6773

68-
#: specialVueFakeDummyForL10nScript.js:8
74+
#: specialVueFakeDummyForL10nScript.js:23
6975
msgid "VUE String not"
7076
msgstr ""
7177

78+
#. TRANSLATORS src/vue.vue:31
79+
#: specialVueFakeDummyForL10nScript.js:25
80+
msgid "test"
81+
msgstr ""
82+
7283
#. TRANSLATORS VUE Fix for https://github.com/nextcloud/docker-ci/pull/663 - Part 2 (src/vue.vue:29)
73-
#: specialVueFakeDummyForL10nScript.js:10
84+
#: specialVueFakeDummyForL10nScript.js:27
7485
msgid "VUE String with followup double quotes"
7586
msgstr ""
7687

7788
#. TRANSLATORS VUE plural with plain text (src/vue.vue:17)
78-
#: specialVueFakeDummyForL10nScript.js:12
89+
#: specialVueFakeDummyForL10nScript.js:29
7990
msgid "VUE %n Plural"
8091
msgid_plural "VUE %n Plurals"
8192
msgstr[0] ""
8293
msgstr[1] ""
8394

8495
#. TRANSLATORS VUE plural with wrapped parameters (src/vue.vue:21)
85-
#: specialVueFakeDummyForL10nScript.js:16
96+
#: specialVueFakeDummyForL10nScript.js:33
8697
msgid "VUE %n Plural with wrapped %s"
8798
msgid_plural "VUE %n Plurals with wrapped %s"
8899
msgstr[0] ""
89100
msgstr[1] ""
90101

102+
#. TRANSLATORS src/vue.vue:32
103+
#: specialVueFakeDummyForL10nScript.js:35
104+
msgid "one test"
105+
msgid_plural "%n tests"
106+
msgstr[0] ""
107+
msgstr[1] ""
108+
91109
#. TRANSLATORS VUE plural with parameters (src/vue.vue:19)
92-
#: specialVueFakeDummyForL10nScript.js:14
110+
#: specialVueFakeDummyForL10nScript.js:31
93111
msgid "VUE %n Plural with %s"
94112
msgid_plural "VUE %n Plurals with %s"
95113
msgstr[0] ""

translations/translationtool/tests/src/vue.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,24 @@
2727

2828
<!-- TRANSLATORS VUE Fix for https://github.com/nextcloud/docker-ci/pull/663 - Part 2 -->
2929
{{ t("test", "VUE String with followup double quotes") }}: {{ data["random property"] }}
30+
31+
<button :aria-label="t('test', 'test')">content</button>
32+
<button :aria-label="n('test', 'one test', '%n tests', 6)">content</button>
33+
{{ text }}
3034
</template>
35+
36+
<script>
37+
export default {
38+
computed: {
39+
text() {
40+
// ensure we also handle formats like this
41+
return n(
42+
'forms',
43+
'one string',
44+
'%n strings',
45+
6,
46+
)
47+
}
48+
}
49+
}
50+
</script>
309 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)