translate.go 3.5 KB

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