forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reformat_translated_json.go
56 lines (48 loc) · 990 Bytes
/
reformat_translated_json.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Entry struct {
ID string `json:"id"`
Translation string `json:"translation"`
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "usage: reformat_translated_json <tranlation directory>\n")
os.Exit(1)
}
directory := os.Args[1]
files, err := ioutil.ReadDir(directory)
if err != nil {
panic(err)
}
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".all.json") {
continue
}
fullPath := filepath.Join(directory, file.Name())
fmt.Println("reformatting:", file.Name())
raw, err := ioutil.ReadFile(fullPath)
if err != nil {
panic(err)
}
var entries []Entry
err = json.Unmarshal(raw, &entries)
if err != nil {
panic(err)
}
rawOut, err := json.MarshalIndent(entries, "", " ")
if err != nil {
panic(err)
}
err = ioutil.WriteFile(fullPath, rawOut, file.Mode())
if err != nil {
panic(err)
}
}
}