-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2strings.go
47 lines (39 loc) · 962 Bytes
/
csv2strings.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
package main
import (
"fmt"
"strings"
"github.com/agrison/go-tablib"
)
func escapeTranslationString(src string) string {
escaped := strings.Replace(src, "\n", "\\n", -1)
escaped = strings.Replace(escaped, "\"", "\\\"", -1)
return escaped
}
// Csv2Strings convert input CSV content to iOS strings file
func Csv2Strings(csvSource []byte) (string, error) {
csv, err := tablib.LoadCSV(csvSource)
if err != nil {
return "", err
}
var results []string
var currentRow map[string]interface{}
var i int
for {
currentRow, err = csv.Row(i)
if err != nil {
break
}
source, ok := currentRow["Source"].(string)
translation, ok := currentRow["English"].(string)
if !ok {
i++
continue
}
source = escapeTranslationString(source)
translation = escapeTranslationString(translation)
line := fmt.Sprintf("\"%s\" = \"%s\";", source, translation)
results = append(results, line)
i++
}
return strings.Join(results, "\n"), nil
}