translate.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:build ignore
  7. // +build ignore
  8. package main
  9. import (
  10. "bufio"
  11. "encoding/json"
  12. "log"
  13. "os"
  14. "path/filepath"
  15. "regexp"
  16. "strings"
  17. "golang.org/x/net/html"
  18. )
  19. var trans = make(map[string]interface{})
  20. var attrRe = regexp.MustCompile(`\{\{\s*'([^']+)'\s+\|\s+translate\s*\}\}`)
  21. var attrReCond = regexp.MustCompile(`\{\{.+\s+\?\s+'([^']+)'\s+:\s+'([^']+)'\s+\|\s+translate\s*\}\}`)
  22. // Find both $translate.instant("…") and $translate.instant("…",…) in JS.
  23. // Consider single quote variants too.
  24. var jsRe = []*regexp.Regexp{
  25. regexp.MustCompile(`\$translate\.instant\(\s*"(.+?)"(,.*|\s*)\)`),
  26. regexp.MustCompile(`\$translate\.instant\(\s*'(.+?)'(,.*|\s*)\)`),
  27. }
  28. // exceptions to the untranslated text warning
  29. var noStringRe = regexp.MustCompile(
  30. `^((\W*\{\{.*?\}\} ?.?\/?.?(bps)?\W*)+(\.stignore)?|[^a-zA-Z]+.?[^a-zA-Z]*|[kMGT]?B|Twitter|JS\W?|DEV|https?://\S+|TechUi)$`)
  31. // exceptions to the untranslated text warning specific to aboutModalView.html
  32. var aboutRe = regexp.MustCompile(`^([^/]+/[^/]+|(The Go Pro|Font Awesome ).+|Build \{\{.+\}\}|Copyright .+ the Syncthing Authors\.)$`)
  33. func generalNode(n *html.Node, filename string) {
  34. translate := false
  35. translationId := ""
  36. if n.Type == html.ElementNode {
  37. if n.Data == "translate" { // for <translate>Text</translate>
  38. translate = true
  39. } else if n.Data == "style" || n.Data == "noscript" {
  40. return
  41. } else {
  42. for _, a := range n.Attr {
  43. if a.Key == "translate" {
  44. translate = true
  45. translationId = a.Val
  46. } else if a.Key == "id" && (a.Val == "contributor-list" ||
  47. a.Val == "copyright-notices") {
  48. // Don't translate a list of names and
  49. // copyright notices of other projects
  50. return
  51. } else {
  52. for _, matches := range attrRe.FindAllStringSubmatch(a.Val, -1) {
  53. translation("", matches[1])
  54. }
  55. for _, matches := range attrReCond.FindAllStringSubmatch(a.Val, -1) {
  56. translation("", matches[1])
  57. translation("", matches[2])
  58. }
  59. if a.Key == "data-content" &&
  60. !noStringRe.MatchString(a.Val) {
  61. log.Println("Untranslated data-content string (" + filename + "):")
  62. log.Print("\t" + a.Val)
  63. }
  64. }
  65. }
  66. }
  67. } else if n.Type == html.TextNode {
  68. v := strings.TrimSpace(n.Data)
  69. if len(v) > 1 && !noStringRe.MatchString(v) &&
  70. !(filename == "aboutModalView.html" && aboutRe.MatchString(v)) &&
  71. !(filename == "logbar.html" && (v == "warn" || v == "errors")) {
  72. log.Println("Untranslated text node (" + filename + "):")
  73. log.Print("\t" + v)
  74. }
  75. }
  76. for c := n.FirstChild; c != nil; c = c.NextSibling {
  77. if translate {
  78. inTranslate(c, translationId, filename)
  79. } else {
  80. generalNode(c, filename)
  81. }
  82. }
  83. }
  84. func inTranslate(n *html.Node, translationId string, filename string) {
  85. if n.Type == html.TextNode {
  86. translation(translationId, n.Data)
  87. } else {
  88. log.Println("translate node with non-text child < (" + filename + ")")
  89. log.Println(n)
  90. }
  91. if n.FirstChild != nil {
  92. log.Println("translate node has children (" + filename + "):")
  93. log.Println(n.Data)
  94. }
  95. }
  96. func isTranslated(id string) bool {
  97. namespace := trans
  98. idParts := strings.Split(id, ".")
  99. id = idParts[len(idParts)-1]
  100. for _, subNamespace := range idParts[0 : len(idParts)-1] {
  101. if _, ok := namespace[subNamespace]; !ok {
  102. return false
  103. }
  104. namespace = namespace[subNamespace].(map[string]interface{})
  105. }
  106. _, ok := namespace[id]
  107. return ok
  108. }
  109. func translation(id string, v string) {
  110. namespace := trans
  111. idParts := strings.Split(id, ".")
  112. id = idParts[len(idParts)-1]
  113. for _, subNamespace := range idParts[0 : len(idParts)-1] {
  114. if _, ok := namespace[subNamespace]; !ok {
  115. namespace[subNamespace] = make(map[string]interface{})
  116. }
  117. namespace = namespace[subNamespace].(map[string]interface{})
  118. }
  119. v = strings.TrimSpace(v)
  120. if id == "" {
  121. id = v
  122. }
  123. if _, ok := namespace[id]; !ok {
  124. av := strings.Replace(v, "{%", "{{", -1)
  125. av = strings.Replace(av, "%}", "}}", -1)
  126. namespace[id] = av
  127. }
  128. }
  129. func walkerFor(basePath string) filepath.WalkFunc {
  130. return func(name string, info os.FileInfo, err error) error {
  131. if err != nil {
  132. return err
  133. }
  134. if !info.Mode().IsRegular() {
  135. return nil
  136. }
  137. fd, err := os.Open(name)
  138. if err != nil {
  139. log.Fatal(err)
  140. }
  141. defer fd.Close()
  142. switch filepath.Ext(name) {
  143. case ".html":
  144. doc, err := html.Parse(fd)
  145. if err != nil {
  146. log.Fatal(err)
  147. }
  148. generalNode(doc, filepath.Base(name))
  149. case ".js":
  150. for s := bufio.NewScanner(fd); s.Scan(); {
  151. for _, re := range jsRe {
  152. for _, matches := range re.FindAllStringSubmatch(s.Text(), -1) {
  153. translation("", matches[1])
  154. }
  155. }
  156. }
  157. }
  158. return nil
  159. }
  160. }
  161. func collectThemes(basePath string) {
  162. files, err := os.ReadDir(basePath)
  163. if err != nil {
  164. log.Fatal(err)
  165. }
  166. for _, f := range files {
  167. if f.IsDir() {
  168. key := "theme.name." + f.Name()
  169. if !isTranslated(key) {
  170. name := strings.Title(f.Name())
  171. translation(key, name)
  172. }
  173. }
  174. }
  175. }
  176. func main() {
  177. fd, err := os.Open(os.Args[1])
  178. if err != nil {
  179. log.Fatal(err)
  180. }
  181. err = json.NewDecoder(fd).Decode(&trans)
  182. if err != nil {
  183. log.Fatal(err)
  184. }
  185. fd.Close()
  186. var guiDir = os.Args[2]
  187. filepath.Walk(guiDir, walkerFor(guiDir))
  188. collectThemes(guiDir)
  189. bs, err := json.MarshalIndent(trans, "", " ")
  190. if err != nil {
  191. log.Fatal(err)
  192. }
  193. os.Stdout.Write(bs)
  194. os.Stdout.WriteString("\n")
  195. }