script: Support single quotes in $translate.instant() parsing (#8542)

Duplicate the regular expression for single and double quotes.
Support additional arguments (string substitution) in both variants.
Simplify the translation string group matching by using a lazy
quantifier instead of excluding the quote itself.
This commit is contained in:
André Colomb
2022-09-16 22:52:33 +02:00
committed by GitHub
parent 39d3424e34
commit 698346edc3
2 changed files with 21 additions and 3 deletions
+11 -3
View File
@@ -24,7 +24,13 @@ import (
var trans = make(map[string]string)
var attrRe = regexp.MustCompile(`\{\{\s*'([^']+)'\s+\|\s+translate\s*\}\}`)
var attrReCond = regexp.MustCompile(`\{\{.+\s+\?\s+'([^']+)'\s+:\s+'([^']+)'\s+\|\s+translate\s*\}\}`)
var jsRe = regexp.MustCompile(`\$translate.instant\("([^"]+)"\)`)
// Find both $translate.instant("…") and $translate.instant("…",…) in JS.
// Consider single quote variants too.
var jsRe = []*regexp.Regexp{
regexp.MustCompile(`\$translate\.instant\(\s*"(.+?)"(,.*|\s*)\)`),
regexp.MustCompile(`\$translate\.instant\(\s*'(.+?)'(,.*|\s*)\)`),
}
// exceptions to the untranslated text warning
var noStringRe = regexp.MustCompile(
@@ -128,8 +134,10 @@ func walkerFor(basePath string) filepath.WalkFunc {
generalNode(doc, filepath.Base(name))
case ".js":
for s := bufio.NewScanner(fd); s.Scan(); {
for _, matches := range jsRe.FindAllStringSubmatch(s.Text(), -1) {
translation(matches[1])
for _, re := range jsRe {
for _, matches := range re.FindAllStringSubmatch(s.Text(), -1) {
translation(matches[1])
}
}
}
}