translate.go 3.7 KB

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