-
Notifications
You must be signed in to change notification settings - Fork 0
/
loco.go
111 lines (95 loc) · 2.6 KB
/
loco.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
99
100
101
102
103
104
105
106
107
108
109
110
111
package loco
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const endpointTemplate = "https://localise.biz:443/api/export/%s.json?key=%s"
// Translation represents a translation from Loco
// as structure that is compatible with go-i18n
type Translation struct {
ID string `json:"id"`
Translation string `json:"translation"`
}
// FetchTranslations creates translations files from a Loco Project
// in the destination path it will try to
// fetch all available languages if none is provided
func FetchTranslations(locoKey, destinationPath string, langs ...string) error {
if len(langs) > 0 {
return fetchLangSpecifics(locoKey, destinationPath, langs)
}
return fetchAllLang(locoKey, destinationPath)
}
func fetchSpecificLang(locoKey, destinationPath, lang string) error {
uri := fmt.Sprintf(endpointTemplate, "locale/"+lang, locoKey)
locoData, err := sendLocoRequest(uri)
if err != nil {
return err
}
translationList := keyValueToTranslation(locoData)
err = writeToFile(translationList, destinationPath, lang)
if err != nil {
return err
}
return nil
}
func sendLocoRequest(url string) (map[string]interface{}, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
var locoData map[string]interface{}
err = json.NewDecoder(res.Body).Decode(&locoData)
if err != nil {
return nil, err
}
return locoData, nil
}
func fetchLangSpecifics(locoKey, destinationPath string, langs []string) error {
for _, lang := range langs {
err := fetchSpecificLang(locoKey, destinationPath, lang)
if err != nil {
return err
}
}
return nil
}
func fetchAllLang(locoKey, destinationPath string) error {
uri := fmt.Sprintf(endpointTemplate, "all", locoKey)
locoData, err := sendLocoRequest(uri)
if err != nil {
return err
}
for lang, keys := range locoData {
translationList := keyValueToTranslation(keys.(map[string]interface{}))
err := writeToFile(translationList, destinationPath, lang)
if err != nil {
return err
}
}
return nil
}
func keyValueToTranslation(assets map[string]interface{}) []Translation {
list := make([]Translation, 0)
for id, translation := range assets {
list = append(list, Translation{ID: id, Translation: translation.(string)})
}
return list
}
func writeToFile(translationList []Translation, path, lang string) error {
preparePath(&path)
output, err := json.Marshal(translationList)
if err != nil {
return err
}
return ioutil.WriteFile(fmt.Sprintf(path, lang), output, 0644)
}
func preparePath(path *string) {
if !strings.HasSuffix(*path, "/") {
*path = *path + "/"
}
*path = *path + "%s.json"
}