translate.go 4.4 KB

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