This commit is contained in:
Darko Luketic 2017-02-17 22:09:37 +01:00
commit 78e8ba2579
4 changed files with 104 additions and 0 deletions

14
gotemplate Normal file
View File

@ -0,0 +1,14 @@
package lang
type language struct {
Code string
Description string
}
var Languages []language
func init() {
{{range .Items}}
Languages = append(Languages, language{Code: "{{.Lang}}", Description: "{{.Description}}"})
{{end}}
}

0
json/.notempty Normal file
View File

0
lang/.notempty Normal file
View File

90
main.go Normal file
View File

@ -0,0 +1,90 @@
package main
import (
"bufio"
"encoding/json"
"log"
"os"
"strings"
"text/template"
)
// wget http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
const (
inFilename = "language-subtag-registry"
outJSONFilename = "json/language.json"
outGoFilename = "lang/language.go"
)
type Item struct {
Lang string `json:"lang"`
Description string `json:"description"`
}
func main() {
f, e := os.Open(inFilename)
if e != nil {
log.Fatal(e.Error())
}
scanner := bufio.NewScanner(f)
items := []*Item{}
for scanner.Scan() {
if scanner.Text() == "%%" {
item := new(Item)
scanner.Scan()
if scanner.Text() == "Type: language" {
for !strings.HasPrefix(scanner.Text(), "Subtag:") {
scanner.Scan()
}
if strings.HasPrefix(scanner.Text(), "Subtag:") {
ss := strings.Split(scanner.Text(), ": ")
item.Lang = strings.Trim(ss[1], " ")
if len(item.Lang) > 2 {
continue
}
}
for !strings.HasPrefix(scanner.Text(), "Description:") {
scanner.Scan()
}
if strings.HasPrefix(scanner.Text(), "Description:") {
ss := strings.Split(scanner.Text(), ": ")
item.Description = strings.Trim(ss[1], " ")
scanner.Scan()
if strings.HasPrefix(scanner.Text(), " ") {
item.Description += strings.TrimPrefix(scanner.Text(), " ")
}
}
items = append(items, item)
}
}
}
if e := f.Close(); e != nil {
log.Fatal(e.Error())
}
jw, e := os.Create(outJSONFilename)
if e != nil {
log.Fatal(e.Error())
}
if e := json.NewEncoder(jw).Encode(items); e != nil {
log.Fatal(e.Error())
}
if e := jw.Close(); e != nil {
log.Fatal(e.Error())
}
gw, e := os.Create(outGoFilename)
if e != nil {
log.Fatal(e.Error())
}
gotmpl := template.Must(template.ParseFiles("gotemplate"))
ctx := make(map[string]interface{})
ctx["Items"] = items
if e := gotmpl.Execute(gw, ctx); e != nil {
log.Fatal(e.Error())
}
if e := gw.Close(); e != nil {
log.Fatal(e.Error())
}
}