translate.go 4.1 KB

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