12 langSource = "datagen/language/language_codes.csv"
13 outputFileName = "language_code_info.go"
22 func decodeLang() *[]lang {
25 csvfile, err := os.Open(langSource)
33 reader := csv.NewReader(csvfile)
34 // there are no empty fields
35 reader.FieldsPerRecord = 3
37 rawCSVdata, err := reader.ReadAll()
43 // move raw CSV data to struct
47 for _, v := range rawCSVdata {
51 rows = append(rows, row)
56 func generateSwitch(d []lang) (string, error) {
57 out := `// generated by "go generate"; DO NOT EDIT
66 // LangCodeInfo returns the English and native language in its script for a
67 // given string, and an error if any. If there are more than one official
68 // names for the language (either English or native), they are separated by a
70 // Language codes should always be lowercase, and this is enforced.
71 func LangCodeInfo(s string) (english, native string, err error) {
73 // codes have to be two characters long
76 errors.New("ISO 639-1 language codes must be 2 characters long")
81 out += fmt.Sprintf("\nswitch s[0] {\n\ncase %q:\nswitch s[1]{\n", c1)
83 // check that the code is exactly 2 characters long
84 if !isCodeValid(r.code) {
85 return "", fmt.Errorf("The code %q is not 2 characters long", r.code)
87 // new first letter of code
91 out += fmt.Sprintf("}\n\ncase %q:\nswitch s[1]{\n", c1)
93 out += fmt.Sprintf("case %q:\nreturn %q, %q, nil\n", r.code[1], r.nameEn, r.nameNat)
99 fmt.Errorf("%q is not a valid ISO-639-1 language code", s)
105 func isCodeValid(s string) bool {
109 if s[0] < 'a' || s[0] > 'z' || s[1] < 'a' || s[1] > 'z' {
115 func writeFile(s, fileName string) error {
118 err := ioutil.WriteFile(fileName, b, 0644)
120 return errors.New("Couldn't write the file")
127 res, _ := generateSwitch(*rows)
128 err := writeFile(res, outputFileName)
132 fmt.Println("language_code_info.go generated")