This repository has been archived by the owner on Dec 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
shared.go
62 lines (52 loc) · 1.45 KB
/
shared.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
package main
import (
"github.com/phrase/phraseapp-go/phraseapp"
)
var Debug bool
type ProjectLocales interface {
ProjectIds() []string
}
type LocaleCacheKey struct {
ProjectID string
Branch string
}
type LocaleCache map[LocaleCacheKey][]*phraseapp.Locale
func LocalesForProjects(client *phraseapp.Client, projectLocales ProjectLocales, branch string) (LocaleCache, error) {
projectIdToLocales := LocaleCache{}
for _, pid := range projectLocales.ProjectIds() {
key := LocaleCacheKey{
ProjectID: pid,
Branch: branch,
}
if _, ok := projectIdToLocales[key]; !ok {
remoteLocales, err := RemoteLocales(client, key)
if err != nil {
if _, ok := (err).(phraseapp.ErrNotFound); ok && branch != "" {
// skip this key if we targeted a branch in
// a project which does not exist
continue
}
return nil, err
}
projectIdToLocales[key] = remoteLocales
}
}
return projectIdToLocales, nil
}
func RemoteLocales(client *phraseapp.Client, key LocaleCacheKey) ([]*phraseapp.Locale, error) {
page := 1
locales, err := client.LocalesList(key.ProjectID, page, 25, &phraseapp.LocalesListParams{Branch: &key.Branch})
if err != nil {
return nil, err
}
result := locales
for len(locales) == 25 {
page = page + 1
locales, err = client.LocalesList(key.ProjectID, page, 25, &phraseapp.LocalesListParams{Branch: &key.Branch})
if err != nil {
return nil, err
}
result = append(result, locales...)
}
return result, nil
}