File: trgen.go

package info (click to toggle)
git-lfs 3.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,880 kB
  • sloc: sh: 23,157; makefile: 519; ruby: 404
file content (83 lines) | stat: -rw-r--r-- 2,019 bytes parent folder | download | duplicates (2)
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
76
77
78
79
80
81
82
83
package main

import (
	"encoding/base64"
	"flag"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"regexp"
)

func infof(w io.Writer, format string, a ...interface{}) {
	if !*verbose {
		return
	}
	fmt.Fprintf(w, format, a...)
}

func warnf(w io.Writer, format string, a ...interface{}) {
	fmt.Fprintf(w, format, a...)
}

func readPoDir() (string, []os.DirEntry) {
	rootDirs := []string{
		".",
		"..",
		"../..",
	}

	var err error
	for _, rootDir := range rootDirs {
		fs, err := os.ReadDir(filepath.Join(rootDir, "po", "build"))
		if err == nil {
			return rootDir, fs
		}
	}

	// In this case, we don't care about the fact that the build dir doesn't
	// exist since that just means there are no translations built.  That's
	// fine for us, so just exit successfully.
	infof(os.Stderr, "Failed to open po dir: %v\n", err)
	os.Exit(0)
	return "", nil
}

var (
	verbose = flag.Bool("verbose", false, "Show verbose output.")
)

func main() {
	flag.Parse()

	infof(os.Stderr, "Converting po files into translations...\n")
	rootDir, fs := readPoDir()
	poDir := filepath.Join(rootDir, "po", "build")
	out, err := os.Create(filepath.Join(rootDir, "tr", "tr_gen.go"))
	if err != nil {
		warnf(os.Stderr, "Failed to create go file: %v\n", err)
		os.Exit(2)
	}
	out.WriteString("package tr\n\nfunc init() {\n")
	out.WriteString("\t// THIS FILE IS GENERATED, DO NOT EDIT\n")
	out.WriteString("\t// Use 'go generate ./tr/trgen' to update\n")
	fileregex := regexp.MustCompile(`([A-Za-z\-_]+).mo`)
	count := 0
	for _, f := range fs {
		if match := fileregex.FindStringSubmatch(f.Name()); match != nil {
			infof(os.Stderr, "%v\n", f.Name())
			cmd := match[1]
			content, err := os.ReadFile(filepath.Join(poDir, f.Name()))
			if err != nil {
				warnf(os.Stderr, "Failed to open %v: %v\n", f.Name(), err)
				os.Exit(2)
			}
			fmt.Fprintf(out, "\tlocales[\"%s\"] = \"%s\"\n", cmd, base64.StdEncoding.EncodeToString(content))
			count++
		}
	}
	out.WriteString("}\n")
	infof(os.Stderr, "Successfully processed %d translations.\n", count)

}