aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/gotext.go
blob: d61ce3931a6418bd21884848f18e3ed612958896 (plain) (blame)
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
// +build generate
//go:generate go run gotext.go

/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2020 WireGuard LLC. All Rights Reserved.
 */

package main

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"golang.org/x/text/message/pipeline"
)

func main() {
	langDirs, err := ioutil.ReadDir("locales")
	if err != nil {
		panic(err)
	}
	var langs []string
	for _, dir := range langDirs {
		if !dir.IsDir() {
			continue
		}
		lang := dir.Name()
		if jsonData, err := ioutil.ReadFile(filepath.Join("locales", lang, "messages.gotext.json")); err == nil {
			var translations pipeline.Messages
			if err := json.Unmarshal(jsonData, &translations); err != nil {
				panic(err)
			}
			lang = translations.Language.String()
			if lang != dir.Name() {
				err = os.Rename(filepath.Join("locales", dir.Name()), filepath.Join("locales", lang))
				if err != nil {
					panic(err)
				}
			}
		} else if os.IsNotExist(err) {
			panic(err)
		}
		langs = append(langs, lang)
	}
	if len(langs) == 0 {
		panic("no locales found")
	}
	gotext, err := ioutil.TempFile("", "gotext*.exe")
	if err != nil {
		panic(err)
	}
	gotextFilename := gotext.Name()
	gotext.Close()
	defer os.Remove(gotextFilename)
	cmd := exec.Command("go", "build", "-o", gotextFilename, "golang.org/x/text/cmd/gotext")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		panic(err)
	}
	cmd = exec.Command(gotextFilename, "-srclang=en", "update", "-out=zgotext.go", "-lang="+strings.Join(langs, ","))
	cmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64", "CGO_ENABLED=1", "CC=x86_64-w64-mingw32-gcc")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		panic(err)
	}
}