2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-07-31 10:30:19 +00:00
|
|
|
|
2021-08-17 08:10:41 +00:00
|
|
|
//go:build ignore
|
2014-07-31 10:30:19 +00:00
|
|
|
// +build ignore
|
|
|
|
|
2014-07-20 11:49:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-23 12:24:08 +00:00
|
|
|
"bufio"
|
2014-07-20 11:49:26 +00:00
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"os"
|
2015-08-02 06:27:05 +00:00
|
|
|
"path/filepath"
|
2014-08-06 12:41:46 +00:00
|
|
|
"regexp"
|
2014-07-20 11:49:26 +00:00
|
|
|
"strings"
|
|
|
|
|
2014-11-29 23:17:00 +00:00
|
|
|
"golang.org/x/net/html"
|
2014-07-20 11:49:26 +00:00
|
|
|
)
|
|
|
|
|
2023-11-13 20:04:24 +00:00
|
|
|
var trans = make(map[string]interface{})
|
2021-07-22 09:47:03 +00:00
|
|
|
var attrRe = regexp.MustCompile(`\{\{\s*'([^']+)'\s+\|\s+translate\s*\}\}`)
|
|
|
|
var attrReCond = regexp.MustCompile(`\{\{.+\s+\?\s+'([^']+)'\s+:\s+'([^']+)'\s+\|\s+translate\s*\}\}`)
|
2022-09-16 20:52:33 +00:00
|
|
|
|
|
|
|
// 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*)\)`),
|
|
|
|
}
|
2014-07-20 11:49:26 +00:00
|
|
|
|
2017-03-18 00:27:22 +00:00
|
|
|
// exceptions to the untranslated text warning
|
|
|
|
var noStringRe = regexp.MustCompile(
|
2021-07-22 09:47:03 +00:00
|
|
|
`^((\W*\{\{.*?\}\} ?.?\/?.?(bps)?\W*)+(\.stignore)?|[^a-zA-Z]+.?[^a-zA-Z]*|[kMGT]?B|Twitter|JS\W?|DEV|https?://\S+|TechUi)$`)
|
2017-03-18 00:27:22 +00:00
|
|
|
|
|
|
|
// exceptions to the untranslated text warning specific to aboutModalView.html
|
2020-08-30 06:01:46 +00:00
|
|
|
var aboutRe = regexp.MustCompile(`^([^/]+/[^/]+|(The Go Pro|Font Awesome ).+|Build \{\{.+\}\}|Copyright .+ the Syncthing Authors\.)$`)
|
2017-03-18 00:27:22 +00:00
|
|
|
|
2015-08-02 06:27:05 +00:00
|
|
|
func generalNode(n *html.Node, filename string) {
|
2014-07-20 11:49:26 +00:00
|
|
|
translate := false
|
2023-11-13 20:04:24 +00:00
|
|
|
translationId := ""
|
2014-07-20 11:49:26 +00:00
|
|
|
if n.Type == html.ElementNode {
|
2015-08-02 06:27:05 +00:00
|
|
|
if n.Data == "translate" { // for <translate>Text</translate>
|
|
|
|
translate = true
|
2020-11-24 21:17:52 +00:00
|
|
|
} else if n.Data == "style" || n.Data == "noscript" {
|
2017-03-18 00:27:22 +00:00
|
|
|
return
|
2015-08-02 06:27:05 +00:00
|
|
|
} else {
|
|
|
|
for _, a := range n.Attr {
|
|
|
|
if a.Key == "translate" {
|
|
|
|
translate = true
|
2023-11-13 20:04:24 +00:00
|
|
|
translationId = a.Val
|
2017-03-18 00:27:22 +00:00
|
|
|
} else if a.Key == "id" && (a.Val == "contributor-list" ||
|
|
|
|
a.Val == "copyright-notices") {
|
|
|
|
// Don't translate a list of names and
|
|
|
|
// copyright notices of other projects
|
|
|
|
return
|
2015-08-02 06:27:05 +00:00
|
|
|
} else {
|
2020-08-30 06:01:46 +00:00
|
|
|
for _, matches := range attrRe.FindAllStringSubmatch(a.Val, -1) {
|
2023-11-13 20:04:24 +00:00
|
|
|
translation("", matches[1])
|
2015-08-02 06:27:05 +00:00
|
|
|
}
|
2020-08-30 06:01:46 +00:00
|
|
|
for _, matches := range attrReCond.FindAllStringSubmatch(a.Val, -1) {
|
2023-11-13 20:04:24 +00:00
|
|
|
translation("", matches[1])
|
|
|
|
translation("", matches[2])
|
2020-08-30 06:01:46 +00:00
|
|
|
}
|
2017-03-25 11:56:51 +00:00
|
|
|
if a.Key == "data-content" &&
|
|
|
|
!noStringRe.MatchString(a.Val) {
|
|
|
|
log.Println("Untranslated data-content string (" + filename + "):")
|
|
|
|
log.Print("\t" + a.Val)
|
|
|
|
}
|
2014-08-06 12:41:46 +00:00
|
|
|
}
|
2014-07-20 11:49:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if n.Type == html.TextNode {
|
|
|
|
v := strings.TrimSpace(n.Data)
|
2017-03-18 00:27:22 +00:00
|
|
|
if len(v) > 1 && !noStringRe.MatchString(v) &&
|
|
|
|
!(filename == "aboutModalView.html" && aboutRe.MatchString(v)) &&
|
|
|
|
!(filename == "logbar.html" && (v == "warn" || v == "errors")) {
|
2015-08-02 06:27:05 +00:00
|
|
|
log.Println("Untranslated text node (" + filename + "):")
|
2014-07-20 11:49:26 +00:00
|
|
|
log.Print("\t" + v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
|
|
if translate {
|
2023-11-13 20:04:24 +00:00
|
|
|
inTranslate(c, translationId, filename)
|
2014-07-20 11:49:26 +00:00
|
|
|
} else {
|
2015-08-02 06:27:05 +00:00
|
|
|
generalNode(c, filename)
|
2014-07-20 11:49:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 20:04:24 +00:00
|
|
|
func inTranslate(n *html.Node, translationId string, filename string) {
|
2014-07-20 11:49:26 +00:00
|
|
|
if n.Type == html.TextNode {
|
2023-11-13 20:04:24 +00:00
|
|
|
translation(translationId, n.Data)
|
2014-07-20 11:49:26 +00:00
|
|
|
} else {
|
2015-08-02 06:27:05 +00:00
|
|
|
log.Println("translate node with non-text child < (" + filename + ")")
|
2014-07-20 11:49:26 +00:00
|
|
|
log.Println(n)
|
|
|
|
}
|
|
|
|
if n.FirstChild != nil {
|
2015-08-02 06:27:05 +00:00
|
|
|
log.Println("translate node has children (" + filename + "):")
|
2014-07-20 11:49:26 +00:00
|
|
|
log.Println(n.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 06:22:52 +00:00
|
|
|
func isTranslated(id string) bool {
|
|
|
|
namespace := trans
|
|
|
|
idParts := strings.Split(id, ".")
|
|
|
|
id = idParts[len(idParts)-1]
|
|
|
|
for _, subNamespace := range idParts[0 : len(idParts)-1] {
|
|
|
|
if _, ok := namespace[subNamespace]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
namespace = namespace[subNamespace].(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := namespace[id]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2023-11-13 20:04:24 +00:00
|
|
|
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{})
|
|
|
|
}
|
|
|
|
|
2014-08-06 12:41:46 +00:00
|
|
|
v = strings.TrimSpace(v)
|
2023-11-13 20:04:24 +00:00
|
|
|
if id == "" {
|
|
|
|
id = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := namespace[id]; !ok {
|
2014-08-06 12:41:46 +00:00
|
|
|
av := strings.Replace(v, "{%", "{{", -1)
|
|
|
|
av = strings.Replace(av, "%}", "}}", -1)
|
2023-11-13 20:04:24 +00:00
|
|
|
namespace[id] = av
|
2014-08-06 12:41:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 06:27:05 +00:00
|
|
|
func walkerFor(basePath string) filepath.WalkFunc {
|
|
|
|
return func(name string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:24:08 +00:00
|
|
|
if !info.Mode().IsRegular() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fd, err := os.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
switch filepath.Ext(name) {
|
|
|
|
case ".html":
|
2015-08-02 06:27:05 +00:00
|
|
|
doc, err := html.Parse(fd)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
generalNode(doc, filepath.Base(name))
|
2021-07-23 12:24:08 +00:00
|
|
|
case ".js":
|
|
|
|
for s := bufio.NewScanner(fd); s.Scan(); {
|
2022-09-16 20:52:33 +00:00
|
|
|
for _, re := range jsRe {
|
|
|
|
for _, matches := range re.FindAllStringSubmatch(s.Text(), -1) {
|
2023-11-13 20:04:24 +00:00
|
|
|
translation("", matches[1])
|
2022-09-16 20:52:33 +00:00
|
|
|
}
|
2021-07-23 12:24:08 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-02 06:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 17:44:38 +00:00
|
|
|
func collectThemes(basePath string) {
|
|
|
|
files, err := os.ReadDir(basePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.IsDir() {
|
2023-11-14 06:22:52 +00:00
|
|
|
key := "theme.name." + f.Name()
|
|
|
|
if !isTranslated(key) {
|
2021-10-20 17:44:38 +00:00
|
|
|
name := strings.Title(f.Name())
|
2023-11-14 06:22:52 +00:00
|
|
|
translation(key, name)
|
2021-10-20 17:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 11:49:26 +00:00
|
|
|
func main() {
|
|
|
|
fd, err := os.Open(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = json.NewDecoder(fd).Decode(&trans)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
2015-08-02 06:27:05 +00:00
|
|
|
var guiDir = os.Args[2]
|
|
|
|
|
|
|
|
filepath.Walk(guiDir, walkerFor(guiDir))
|
2021-10-20 17:44:38 +00:00
|
|
|
collectThemes(guiDir)
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2022-12-18 07:29:59 +00:00
|
|
|
bs, err := json.MarshalIndent(trans, "", " ")
|
2014-07-20 11:49:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Stdout.Write(bs)
|
2014-07-31 07:08:08 +00:00
|
|
|
os.Stdout.WriteString("\n")
|
2014-07-20 11:49:26 +00:00
|
|
|
}
|