main.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "encoding/json"
  8. "fmt"
  9. "log"
  10. "net/http"
  11. "os"
  12. "sort"
  13. )
  14. type stat struct {
  15. Translated int `json:"translated_entities"`
  16. Untranslated int `json:"untranslated_entities"`
  17. }
  18. type translation struct {
  19. Content string
  20. }
  21. func main() {
  22. log.SetFlags(log.Lshortfile)
  23. if u, p := userPass(); u == "" || p == "" {
  24. log.Fatal("Need environment variables TRANSIFEX_USER and TRANSIFEX_PASS")
  25. }
  26. resp := req("https://www.transifex.com/api/2/project/syncthing/resource/gui/stats")
  27. var stats map[string]stat
  28. err := json.NewDecoder(resp.Body).Decode(&stats)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. resp.Body.Close()
  33. var langs []string
  34. for code, stat := range stats {
  35. shortCode := code[:2]
  36. if pct := 100 * stat.Translated / (stat.Translated + stat.Untranslated); pct < 95 {
  37. log.Printf("Skipping language %q (too low completion ratio %d%%)", shortCode, pct)
  38. os.Remove("lang-" + shortCode + ".json")
  39. continue
  40. }
  41. langs = append(langs, shortCode)
  42. if shortCode == "en" {
  43. continue
  44. }
  45. log.Printf("Updating language %q", shortCode)
  46. resp := req("https://www.transifex.com/api/2/project/syncthing/resource/gui/translation/" + code)
  47. var t translation
  48. err := json.NewDecoder(resp.Body).Decode(&t)
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. resp.Body.Close()
  53. fd, err := os.Create("lang-" + shortCode + ".json")
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. fd.WriteString(t.Content)
  58. fd.Close()
  59. }
  60. sort.Strings(langs)
  61. fmt.Print("var validLangs = ")
  62. json.NewEncoder(os.Stdout).Encode(langs)
  63. }
  64. func userPass() (string, string) {
  65. user := os.Getenv("TRANSIFEX_USER")
  66. pass := os.Getenv("TRANSIFEX_PASS")
  67. return user, pass
  68. }
  69. func req(url string) *http.Response {
  70. user, pass := userPass()
  71. req, err := http.NewRequest("GET", url, nil)
  72. if err != nil {
  73. log.Fatal(err)
  74. }
  75. req.SetBasicAuth(user, pass)
  76. resp, err := http.DefaultClient.Do(req)
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. return resp
  81. }