syncthing/script/translate.go

162 lines
4.1 KiB
Go
Raw Permalink Normal View History

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,
// You can obtain one at https://mozilla.org/MPL/2.0/.
// +build ignore
package main
import (
"bufio"
"encoding/json"
"log"
"os"
2015-08-02 06:27:05 +00:00
"path/filepath"
"regexp"
"strings"
"golang.org/x/net/html"
)
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\("([^"]+)"\)`)
// exceptions to the untranslated text warning
var noStringRe = regexp.MustCompile(
`^((\W*\{\{.*?\}\} ?.?\/?.?(bps)?\W*)+(\.stignore)?|[^a-zA-Z]+.?[^a-zA-Z]*|[kMGT]?B|Twitter|JS\W?|DEV|https?://\S+|TechUi)$`)
// exceptions to the untranslated text warning specific to aboutModalView.html
var aboutRe = regexp.MustCompile(`^([^/]+/[^/]+|(The Go Pro|Font Awesome ).+|Build \{\{.+\}\}|Copyright .+ the Syncthing Authors\.)$`)
2015-08-02 06:27:05 +00:00
func generalNode(n *html.Node, filename string) {
translate := false
if n.Type == html.ElementNode {
2015-08-02 06:27:05 +00:00
if n.Data == "translate" { // for <translate>Text</translate>
translate = true
} else if n.Data == "style" || n.Data == "noscript" {
return
2015-08-02 06:27:05 +00:00
} else {
for _, a := range n.Attr {
if a.Key == "translate" {
translate = true
} 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 {
for _, matches := range attrRe.FindAllStringSubmatch(a.Val, -1) {
2015-08-02 06:27:05 +00:00
translation(matches[1])
}
for _, matches := range attrReCond.FindAllStringSubmatch(a.Val, -1) {
translation(matches[1])
translation(matches[2])
}
if a.Key == "data-content" &&
!noStringRe.MatchString(a.Val) {
log.Println("Untranslated data-content string (" + filename + "):")
log.Print("\t" + a.Val)
}
}
}
}
} else if n.Type == html.TextNode {
v := strings.TrimSpace(n.Data)
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 + "):")
log.Print("\t" + v)
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if translate {
2015-08-02 06:27:05 +00:00
inTranslate(c, filename)
} else {
2015-08-02 06:27:05 +00:00
generalNode(c, filename)
}
}
}
2015-08-02 06:27:05 +00:00
func inTranslate(n *html.Node, filename string) {
if n.Type == html.TextNode {
translation(n.Data)
} else {
2015-08-02 06:27:05 +00:00
log.Println("translate node with non-text child < (" + filename + ")")
log.Println(n)
}
if n.FirstChild != nil {
2015-08-02 06:27:05 +00:00
log.Println("translate node has children (" + filename + "):")
log.Println(n.Data)
}
}
func translation(v string) {
v = strings.TrimSpace(v)
if _, ok := trans[v]; !ok {
av := strings.Replace(v, "{%", "{{", -1)
av = strings.Replace(av, "%}", "}}", -1)
trans[v] = av
}
}
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
}
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))
case ".js":
for s := bufio.NewScanner(fd); s.Scan(); {
for _, matches := range jsRe.FindAllStringSubmatch(s.Text(), -1) {
translation(matches[1])
}
}
2015-08-02 06:27:05 +00:00
}
return nil
}
}
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))
bs, err := json.MarshalIndent(trans, "", " ")
if err != nil {
log.Fatal(err)
}
os.Stdout.Write(bs)
2014-07-31 07:08:08 +00:00
os.Stdout.WriteString("\n")
}