2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-07-31 10:30:19 +00:00
|
|
|
|
2021-08-17 08:10:41 +00:00
|
|
|
//go:build ignore
|
2014-07-31 10:30:19 +00:00
|
|
|
// +build ignore
|
|
|
|
|
2014-07-28 13:14:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-07-30 09:52:16 +00:00
|
|
|
"fmt"
|
2021-11-22 07:59:47 +00:00
|
|
|
"io"
|
2014-07-28 13:14:02 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2014-08-06 12:56:32 +00:00
|
|
|
"regexp"
|
2014-07-28 13:14:02 +00:00
|
|
|
"sort"
|
2014-08-06 12:56:32 +00:00
|
|
|
"strings"
|
2014-07-28 13:14:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type stat struct {
|
|
|
|
Translated int `json:"translated_entities"`
|
|
|
|
Untranslated int `json:"untranslated_entities"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type translation struct {
|
|
|
|
Content string
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2014-07-29 09:06:52 +00:00
|
|
|
log.SetFlags(log.Lshortfile)
|
|
|
|
|
|
|
|
if u, p := userPass(); u == "" || p == "" {
|
|
|
|
log.Fatal("Need environment variables TRANSIFEX_USER and TRANSIFEX_PASS")
|
|
|
|
}
|
|
|
|
|
2014-08-06 12:56:32 +00:00
|
|
|
curValidLangs := map[string]bool{}
|
|
|
|
for _, lang := range loadValidLangs() {
|
|
|
|
curValidLangs[lang] = true
|
|
|
|
}
|
|
|
|
log.Println(curValidLangs)
|
|
|
|
|
2014-07-28 13:14:02 +00:00
|
|
|
resp := req("https://www.transifex.com/api/2/project/syncthing/resource/gui/stats")
|
|
|
|
|
|
|
|
var stats map[string]stat
|
|
|
|
err := json.NewDecoder(resp.Body).Decode(&stats)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2015-06-02 06:47:26 +00:00
|
|
|
names := make(map[string]string)
|
|
|
|
|
2014-07-28 13:14:02 +00:00
|
|
|
var langs []string
|
|
|
|
for code, stat := range stats {
|
2014-08-14 15:04:17 +00:00
|
|
|
code = strings.Replace(code, "_", "-", 1)
|
2014-10-08 11:57:11 +00:00
|
|
|
pct := 100 * stat.Translated / (stat.Translated + stat.Untranslated)
|
|
|
|
if pct < 75 || !curValidLangs[code] && pct < 95 {
|
|
|
|
log.Printf("Skipping language %q (too low completion ratio %d%%)", code, pct)
|
|
|
|
os.Remove("lang-" + code + ".json")
|
|
|
|
continue
|
2014-07-28 13:14:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 15:04:17 +00:00
|
|
|
langs = append(langs, code)
|
2015-06-02 06:47:26 +00:00
|
|
|
names[code] = languageName(code)
|
2014-08-14 15:04:17 +00:00
|
|
|
if code == "en" {
|
2014-07-28 13:17:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-08-14 15:04:17 +00:00
|
|
|
log.Printf("Updating language %q", code)
|
2014-07-28 13:14:02 +00:00
|
|
|
|
|
|
|
resp := req("https://www.transifex.com/api/2/project/syncthing/resource/gui/translation/" + code)
|
|
|
|
var t translation
|
|
|
|
err := json.NewDecoder(resp.Body).Decode(&t)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2014-08-14 15:04:17 +00:00
|
|
|
fd, err := os.Create("lang-" + code + ".json")
|
2014-07-28 13:14:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.WriteString(t.Content)
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
|
2014-08-06 12:56:32 +00:00
|
|
|
saveValidLangs(langs)
|
2015-06-02 06:47:26 +00:00
|
|
|
saveLanguageNames(names)
|
2014-08-06 12:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func saveValidLangs(langs []string) {
|
2014-07-28 13:14:02 +00:00
|
|
|
sort.Strings(langs)
|
2014-08-06 12:56:32 +00:00
|
|
|
fd, err := os.Create("valid-langs.js")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Fprint(fd, "var validLangs = ")
|
|
|
|
json.NewEncoder(fd).Encode(langs)
|
|
|
|
fd.Close()
|
2014-07-28 13:14:02 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 06:47:26 +00:00
|
|
|
func saveLanguageNames(names map[string]string) {
|
|
|
|
fd, err := os.Create("prettyprint.js")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Fprint(fd, "var langPrettyprint = ")
|
|
|
|
json.NewEncoder(fd).Encode(names)
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:06:52 +00:00
|
|
|
func userPass() (string, string) {
|
2014-07-28 13:14:02 +00:00
|
|
|
user := os.Getenv("TRANSIFEX_USER")
|
|
|
|
pass := os.Getenv("TRANSIFEX_PASS")
|
2014-07-29 09:06:52 +00:00
|
|
|
return user, pass
|
|
|
|
}
|
|
|
|
|
|
|
|
func req(url string) *http.Response {
|
|
|
|
user, pass := userPass()
|
2014-07-28 13:14:02 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
req.SetBasicAuth(user, pass)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
2014-08-06 12:56:32 +00:00
|
|
|
|
|
|
|
func loadValidLangs() []string {
|
|
|
|
fd, err := os.Open("valid-langs.js")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
2021-11-22 07:59:47 +00:00
|
|
|
bs, err := io.ReadAll(fd)
|
2014-08-06 12:56:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var langs []string
|
2015-06-02 06:33:36 +00:00
|
|
|
exp := regexp.MustCompile(`\[([a-zA-Z@",-]+)\]`)
|
2014-08-06 12:56:32 +00:00
|
|
|
if matches := exp.FindSubmatch(bs); len(matches) == 2 {
|
|
|
|
langs = strings.Split(string(matches[1]), ",")
|
|
|
|
for i := range langs {
|
|
|
|
// Remove quotes
|
|
|
|
langs[i] = langs[i][1 : len(langs[i])-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return langs
|
|
|
|
}
|
2015-06-02 06:47:26 +00:00
|
|
|
|
|
|
|
type languageResponse struct {
|
|
|
|
Code string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func languageName(code string) string {
|
|
|
|
var lang languageResponse
|
|
|
|
resp := req("https://www.transifex.com/api/2/language/" + code)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
json.NewDecoder(resp.Body).Decode(&lang)
|
|
|
|
if lang.Name == "" {
|
|
|
|
return code
|
|
|
|
}
|
|
|
|
return lang.Name
|
|
|
|
}
|