aboutsummaryrefslogtreecommitdiff
path: root/script/weblatedl.go
blob: 730c841cdd2ec5a7c9facf0c7dc1c1fc5d510b30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (C) 2022 The Syncthing Authors.
//
// 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,
// You can obtain one at https://mozilla.org/MPL/2.0/.

//go:build ignore
// +build ignore

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"regexp"
	"sort"
	"strings"
)

type stat struct {
	Code       string `json:"code"`
	Name       string `json:"name"`
	Total      int    `json:"total"`
	Translated int    `json:"translated"`
	Fuzzy      int    `json:"fuzzy"`
}

type translation map[string]any

func main() {
	log.SetFlags(log.Lshortfile)

	token := os.Getenv("WEBLATE_TOKEN")
	if token == "" {
		log.Fatal("Need environment variable WEBLATE_TOKEN")
	}

	curValidLangs := make(map[string]struct{})
	for _, lang := range loadValidLangs() {
		curValidLangs[lang] = struct{}{}
	}
	log.Println(curValidLangs)

	resp := req("https://hosted.weblate.org/exports/stats/syncthing/gui/?format=json", token)

	var stats []stat
	err := json.NewDecoder(resp.Body).Decode(&stats)
	if err != nil {
		log.Fatal(err)
	}
	resp.Body.Close()

	names := make(map[string]string)

	var langs []string
	for _, stat := range stats {
		code := reformatLanguageCode(stat.Code)
		pct := 100 * stat.Translated / stat.Total
		if _, valid := curValidLangs[code]; pct < 75 || !valid && pct < 95 {
			log.Printf("Language %q too low completion ratio %d%%", code, pct)
		} else {
			langs = append(langs, code)
			names[code] = stat.Name
		}
		if code == "en" {
			continue
		}

		log.Printf("Updating language %q", code)

		resp := req("https://hosted.weblate.org/api/translations/syncthing/gui/"+stat.Code+"/file/", token)
		bs, err := io.ReadAll(resp.Body)
		if err != nil {
			log.Fatal(err)
		}
		resp.Body.Close()

		var t translation
		if err := json.Unmarshal(bs, &t); err != nil {
			log.Fatal(err)
		}

		fd, err := os.Create("lang-" + code + ".json")
		if err != nil {
			log.Fatal(err)
		}
		fd.Write(bs)
		fd.Close()
	}

	saveValidLangs(langs)
	saveLanguageNames(names)
}

func reformatLanguageCode(origCode string) string {
	switch origCode {
	case "ko":
		return "ko-KR"
	case "nb_NO":
		return "nb"
	case "ro":
		return "ro-RO"
	case "zh_Hans":
		return "zh-CN"
	case "zh_Hant":
		return "zh-TW"
	case "zh_Hant_HK":
		return "zh-HK"
	default:
		return strings.Replace(origCode, "_", "-", 1)
	}
}

func saveValidLangs(langs []string) {
	sort.Strings(langs)
	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()
}

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()
}

func req(url, token string) *http.Response {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Authorization", "Token "+token)
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	return resp
}

func loadValidLangs() []string {
	fd, err := os.Open("valid-langs.js")
	if err != nil {
		log.Fatal(err)
	}
	defer fd.Close()
	bs, err := io.ReadAll(fd)
	if err != nil {
		log.Fatal(err)
	}

	var langs []string
	exp := regexp.MustCompile(`\[([a-zA-Z@",-_]+)\]`)
	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
}