blob: c1bbe907b6995d1dcb88d707bd56eff9a152f6fa [file] [log] [blame]
avm99963b5ab76c2020-12-27 02:18:49 +01001package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "log"
8 "net/http"
9 "os"
10)
11
12const isoLangsFileName = "isoLangs.json"
13
14type Language struct {
15 CodeName string `json:"-"`
16 Name string `json:"name"`
17 NativeName string `json:"nativeName"`
18}
19
20func getLanguage(isoLangs map[string]Language, lang string) (Language, error) {
21 for currLangCode, currLang := range isoLangs {
22 if currLangCode == lang {
23 currLang.CodeName = lang
24 return currLang, nil
25 }
26 }
27
28 return Language{}, fmt.Errorf("Didn't find language '%v' in isoLangs", lang)
29}
30
31func main() {
32 log.SetPrefix("[langs] ")
33 log.SetFlags(0)
34
35 isoLangsFile, err := os.Open(isoLangsFileName)
36 if err != nil {
37 log.Fatalf("Couldn't open file %v, error: %v", isoLangsFileName, err)
38 }
39 defer isoLangsFile.Close()
40
41 isoLangsRawData, err := ioutil.ReadAll(isoLangsFile)
42 if err != nil {
43 log.Fatalf("Couldn't read file %v, error: %v", isoLangsFileName, err)
44 }
45
46 var isoLangs map[string]Language
47 if err := json.Unmarshal(isoLangsRawData, &isoLangs); err != nil {
48 log.Fatalf("Couldn't unmarshal JSON file %v, error: %v", isoLangsFileName, err)
49 }
50
51 resp, err := http.Get("http://translate.google.com/translate_a/l?client=chrome")
52 if err != nil {
53 log.Fatalf("Couldn't get current Google Translate languages from server, error: %v", err)
54 }
55 defer resp.Body.Close()
56
57 gTranslateRawData, err := ioutil.ReadAll(resp.Body)
58 if err != nil {
59 log.Fatalf("Couldn't read body data from Google Translate languages request, error: %v", err)
60 }
61
62 var gTranslateJson map[string]interface{}
63 if err := json.Unmarshal(gTranslateRawData, &gTranslateJson); err != nil {
64 log.Fatalf("Couldn't unmarshal JSON data from the Google Translate languages request, error: %v", err)
65 }
66
67 gTranslateLangs := gTranslateJson["tl"].(map[string]interface{})
68 langs := make(map[string]Language, len(gTranslateLangs))
69
70 for langCode, name := range gTranslateLangs {
71 isoLang, err := getLanguage(isoLangs, langCode)
72 if err != nil {
73 log.Fatalf("Didn't find language '%v' in isoLangs, error: %v", langCode, err)
74 }
75 isoLang.Name = name.(string)
76 langs[langCode] = isoLang
77 }
78
79 result, err := json.MarshalIndent(langs, "", " ")
80 if err != nil {
81 log.Fatal("Couldn't marshal the final JSON, error: %v", err)
82 }
83
84 fmt.Printf("%v", string(result))
85}