translate.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 translation(id string, v string) {
  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. namespace[subNamespace] = make(map[string]interface{})
  103. }
  104. namespace = namespace[subNamespace].(map[string]interface{})
  105. }
  106. v = strings.TrimSpace(v)
  107. if id == "" {
  108. id = v
  109. }
  110. if _, ok := namespace[id]; !ok {
  111. av := strings.Replace(v, "{%", "{{", -1)
  112. av = strings.Replace(av, "%}", "}}", -1)
  113. namespace[id] = av
  114. }
  115. }
  116. func walkerFor(basePath string) filepath.WalkFunc {
  117. return func(name string, info os.FileInfo, err error) error {
  118. if err != nil {
  119. return err
  120. }
  121. if !info.Mode().IsRegular() {
  122. return nil
  123. }
  124. fd, err := os.Open(name)
  125. if err != nil {
  126. log.Fatal(err)
  127. }
  128. defer fd.Close()
  129. switch filepath.Ext(name) {
  130. case ".html":
  131. doc, err := html.Parse(fd)
  132. if err != nil {
  133. log.Fatal(err)
  134. }
  135. generalNode(doc, filepath.Base(name))
  136. case ".js":
  137. for s := bufio.NewScanner(fd); s.Scan(); {
  138. for _, re := range jsRe {
  139. for _, matches := range re.FindAllStringSubmatch(s.Text(), -1) {
  140. translation("", matches[1])
  141. }
  142. }
  143. }
  144. }
  145. return nil
  146. }
  147. }
  148. func collectThemes(basePath string) {
  149. files, err := os.ReadDir(basePath)
  150. if err != nil {
  151. log.Fatal(err)
  152. }
  153. for _, f := range files {
  154. if f.IsDir() {
  155. key := "theme-name-" + f.Name()
  156. if _, ok := trans[key]; !ok {
  157. name := strings.Title(f.Name())
  158. trans[key] = name
  159. }
  160. }
  161. }
  162. }
  163. func main() {
  164. fd, err := os.Open(os.Args[1])
  165. if err != nil {
  166. log.Fatal(err)
  167. }
  168. err = json.NewDecoder(fd).Decode(&trans)
  169. if err != nil {
  170. log.Fatal(err)
  171. }
  172. fd.Close()
  173. var guiDir = os.Args[2]
  174. filepath.Walk(guiDir, walkerFor(guiDir))
  175. collectThemes(guiDir)
  176. bs, err := json.MarshalIndent(trans, "", " ")
  177. if err != nil {
  178. log.Fatal(err)
  179. }
  180. os.Stdout.Write(bs)
  181. os.Stdout.WriteString("\n")
  182. }