translate.go 5.5 KB

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