aboutsummaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
authorEmil Lundberg <emil@emlun.se>2023-11-13 21:04:24 +0100
committerGitHub <noreply@github.com>2023-11-13 21:04:24 +0100
commita1ad020b6387f851764cb9a6e97253df85cce12c (patch)
tree1c66930ca0e9e8030fd3278f627971659ca0a53c /script
parent8f1b0df74b6be8842a3f1c13aa800356b8685bf5 (diff)
downloadsyncthing-a1ad020b6387f851764cb9a6e97253df85cce12c.tar.gz
syncthing-a1ad020b6387f851764cb9a6e97253df85cce12c.zip
Support explicit translation ID and dotted namespaces in translation extraction (#9192)
Some translations, especially single words or other short labels for buttons and the like, may not be transferable between contexts even if they happen to be equal in English. In these cases, setting an explicit translation ID is important for context separation. Angular Translate also supports nested JSON in translation tables, addressed using `.` as namespace separator; this enhancement makes use of this when extracting translation with an explicit translation ID.
Diffstat (limited to 'script')
-rw-r--r--script/translate.go38
1 files changed, 27 insertions, 11 deletions
diff --git a/script/translate.go b/script/translate.go
index 69622b606..1d182ebe2 100644
--- a/script/translate.go
+++ b/script/translate.go
@@ -21,7 +21,7 @@ import (
"golang.org/x/net/html"
)
-var trans = make(map[string]string)
+var trans = make(map[string]interface{})
var attrRe = regexp.MustCompile(`\{\{\s*'([^']+)'\s+\|\s+translate\s*\}\}`)
var attrReCond = regexp.MustCompile(`\{\{.+\s+\?\s+'([^']+)'\s+:\s+'([^']+)'\s+\|\s+translate\s*\}\}`)
@@ -41,6 +41,7 @@ var aboutRe = regexp.MustCompile(`^([^/]+/[^/]+|(The Go Pro|Font Awesome ).+|Bui
func generalNode(n *html.Node, filename string) {
translate := false
+ translationId := ""
if n.Type == html.ElementNode {
if n.Data == "translate" { // for <translate>Text</translate>
translate = true
@@ -50,6 +51,7 @@ func generalNode(n *html.Node, filename string) {
for _, a := range n.Attr {
if a.Key == "translate" {
translate = true
+ translationId = a.Val
} else if a.Key == "id" && (a.Val == "contributor-list" ||
a.Val == "copyright-notices") {
// Don't translate a list of names and
@@ -57,11 +59,11 @@ func generalNode(n *html.Node, filename string) {
return
} else {
for _, matches := range attrRe.FindAllStringSubmatch(a.Val, -1) {
- translation(matches[1])
+ translation("", matches[1])
}
for _, matches := range attrReCond.FindAllStringSubmatch(a.Val, -1) {
- translation(matches[1])
- translation(matches[2])
+ translation("", matches[1])
+ translation("", matches[2])
}
if a.Key == "data-content" &&
!noStringRe.MatchString(a.Val) {
@@ -82,16 +84,16 @@ func generalNode(n *html.Node, filename string) {
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if translate {
- inTranslate(c, filename)
+ inTranslate(c, translationId, filename)
} else {
generalNode(c, filename)
}
}
}
-func inTranslate(n *html.Node, filename string) {
+func inTranslate(n *html.Node, translationId string, filename string) {
if n.Type == html.TextNode {
- translation(n.Data)
+ translation(translationId, n.Data)
} else {
log.Println("translate node with non-text child < (" + filename + ")")
log.Println(n)
@@ -102,12 +104,26 @@ func inTranslate(n *html.Node, filename string) {
}
}
-func translation(v string) {
+func translation(id string, v string) {
+ namespace := trans
+ idParts := strings.Split(id, ".")
+ id = idParts[len(idParts)-1]
+ for _, subNamespace := range idParts[0 : len(idParts)-1] {
+ if _, ok := namespace[subNamespace]; !ok {
+ namespace[subNamespace] = make(map[string]interface{})
+ }
+ namespace = namespace[subNamespace].(map[string]interface{})
+ }
+
v = strings.TrimSpace(v)
- if _, ok := trans[v]; !ok {
+ if id == "" {
+ id = v
+ }
+
+ if _, ok := namespace[id]; !ok {
av := strings.Replace(v, "{%", "{{", -1)
av = strings.Replace(av, "%}", "}}", -1)
- trans[v] = av
+ namespace[id] = av
}
}
@@ -136,7 +152,7 @@ func walkerFor(basePath string) filepath.WalkFunc {
for s := bufio.NewScanner(fd); s.Scan(); {
for _, re := range jsRe {
for _, matches := range re.FindAllStringSubmatch(s.Text(), -1) {
- translation(matches[1])
+ translation("", matches[1])
}
}
}