Ensure we always have an up to date list of language names

This commit is contained in:
Jakob Borg
2015-06-02 08:47:26 +02:00
parent 5bd1e4a167
commit e952da7f91
5 changed files with 40 additions and 8 deletions

View File

@@ -51,6 +51,8 @@ func main() {
}
resp.Body.Close()
names := make(map[string]string)
var langs []string
for code, stat := range stats {
code = strings.Replace(code, "_", "-", 1)
@@ -62,6 +64,7 @@ func main() {
}
langs = append(langs, code)
names[code] = languageName(code)
if code == "en" {
continue
}
@@ -85,6 +88,7 @@ func main() {
}
saveValidLangs(langs)
saveLanguageNames(names)
}
func saveValidLangs(langs []string) {
@@ -98,6 +102,16 @@ func saveValidLangs(langs []string) {
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 userPass() (string, string) {
user := os.Getenv("TRANSIFEX_USER")
pass := os.Getenv("TRANSIFEX_PASS")
@@ -142,3 +156,19 @@ func loadValidLangs() []string {
return langs
}
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
}