-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.go
98 lines (77 loc) · 2.09 KB
/
merge.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
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
package main
import (
"io/ioutil"
"path"
"github.com/agrison/go-tablib"
)
type mergedTranslations map[string]map[string]string
// MergeAllLocalizableCSV merges alls csv files into one
func MergeAllLocalizableCSV(config *CrowdinSheetsConfig) ([]byte, error) {
mergedCsv := tablib.NewDataset([]string{"English"})
results := make(mergedTranslations)
for _, language := range config.Languages {
mergedCsv.AppendColumn(language, nil)
results[language] = make(map[string]string)
}
for _, language := range config.Languages {
csvPath := path.Join(config.OutputFolder, language, "Localizable.csv")
csvBytes, err := ioutil.ReadFile(csvPath)
if err != nil {
continue
}
csvContent, err := tablib.LoadCSV(csvBytes)
if err != nil {
continue
}
var i int
for {
row, err := csvContent.Row(i)
if err != nil {
break
}
source, ok := row["Source"].(string)
translation, ok := row["English"].(string)
if !ok {
i++
continue
}
results[language][source] = translation
i++
}
}
var language string
for k := range results {
if language == "" {
language = k
}
}
horizontaledResults := make([][]interface{}, len(results[language]))
for source := range results[language] {
columns := []string{}
columns = append(columns, source)
for _, lang := range config.Languages {
columns = append(columns, results[lang][source])
}
columnsInterface := make([]interface{}, len(columns))
for i, v := range columns {
columnsInterface[i] = v
}
horizontaledResults = append(horizontaledResults, columnsInterface)
}
for _, row := range horizontaledResults {
if err := mergedCsv.Append(row); err != nil {
continue
}
}
mergedCsv = mergedCsv.Sort("English")
csvOutput, err := mergedCsv.CSV()
return csvOutput.Bytes(), err
}
// func MergeAllXML(config *CrowdinSheetsConfig) ([]byte, error) {
// mergedCsv := tablib.NewDataset([]string{"Key", "English"})
// results := make(mergedTranslations)
// for _, language := range config.Languages {
// mergedCsv.AppendColumn(language, nil)
// results[language] = make(map[string]string)
// }
// }