summaryrefslogtreecommitdiff
path: root/transifex/main.go
blob: 37bae976bc7830e344f0dca66ca13dcce23a3fe7 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main

import (
	"encoding/json"
	"fmt"
	"os"
	"path"

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

const (
	outGotext      = "out.gotext.json"
	messagesGotext = "messages.gotext.json"
)

type transifex map[string]string

func main() {
	if len(os.Args) < 2 {
		panic("g2t or t2g should be passed as argument")
	}

	switch os.Args[1] {
	case "g2t":
		g2t(func(m pipeline.Message) string { return m.Message.Msg })
	case "lang2t":
		g2t(func(m pipeline.Message) string { return m.Translation.Msg })
	case "t2g":
		t2g()
	default:
		panic("g2t or t2g should be passed as argument")
	}
}

func g2t(getMessage func(pipeline.Message) string) {
	if len(os.Args) < 4 {
		panic(fmt.Sprintf("usage: %s g2t inFile outFile", os.Args[0]))
	}

	inF, err := os.Open(os.Args[2])
	if err != nil {
		panic(fmt.Sprintf("Can't open input file %s: %v", os.Args[2], err))
	}
	outF, err := os.Create(os.Args[3])
	if err != nil {
		panic(fmt.Sprintf("Can't open output file %s: %v", os.Args[3], err))
	}

	toTransifex(inF, outF, getMessage)
}

func t2g() {
	if len(os.Args) < 3 {
		panic(fmt.Sprintf("usage: %s t2g localeFolder", os.Args[0]))
	}

	origF, err := os.Open(path.Join(os.Args[2], outGotext))
	if err != nil {
		panic(fmt.Sprintf("Can't open file %s/%s: %v", os.Args[3], outGotext, err))
	}
	outF, err := os.Create(path.Join(os.Args[2], messagesGotext))
	if err != nil {
		panic(fmt.Sprintf("Can't open output file %s/%v: %v", os.Args[3], messagesGotext, err))
	}
	toGotext(origF, os.Stdin, outF)
}

func toTransifex(inF, outF *os.File, getMessage func(pipeline.Message) string) {
	messages := pipeline.Messages{}
	dec := json.NewDecoder(inF)
	err := dec.Decode(&messages)
	if err != nil {
		panic(fmt.Sprintf("An error ocurred decoding json: %v", err))
	}

	transfx := make(transifex)
	for _, m := range messages.Messages {
		transfx[m.ID[0]] = getMessage(m)
	}
	enc := json.NewEncoder(outF)
	enc.SetIndent("", "    ")
	err = enc.Encode(transfx)
	if err != nil {
		panic(fmt.Sprintf("An error ocurred encoding json: %v", err))
	}
}

func toGotext(origF, inF, outF *os.File) {
	transfx := make(transifex)
	dec := json.NewDecoder(inF)
	err := dec.Decode(&transfx)
	if err != nil {
		panic(fmt.Sprintf("An error ocurred decoding json: %v", err))
	}

	messages := pipeline.Messages{}
	dec = json.NewDecoder(origF)
	err = dec.Decode(&messages)
	if err != nil {
		panic(fmt.Sprintf("An error ocurred decoding orig json: %v", err))
	}

	for k, v := range transfx {
		found := false
		for i, m := range messages.Messages {
			if m.ID[0] == k {
				messages.Messages[i].Translation.Msg = v
				found = true
				break
			}
		}
		if !found {
			fmt.Printf("The original document doesn't have id: %s\n", k)
		}
	}

	enc := json.NewEncoder(outF)
	enc.SetIndent("", "    ")
	err = enc.Encode(messages)
	if err != nil {
		panic(fmt.Sprintf("An error ocurred encoding json: %v", err))
	}
}