-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtranslate.go
125 lines (96 loc) · 2.76 KB
/
translate.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/99designs/api-sdk-go"
)
var cachePath = findCachePath()
func findCachePath() string {
var cachePath string
usr, err := user.Current()
if err == nil && usr.HomeDir != "" {
cachePath = filepath.Join(usr.HomeDir, ".smartling", "cache")
} else {
log.Panicln("Can't locate a cache directory")
}
_ = os.MkdirAll(cachePath, 0755)
return cachePath
}
func projectFileHash(projectFilepath string) string {
localpath := localRelativeFilePath(projectFilepath)
file, err := os.Open(localpath)
logAndQuitIfError(err)
defer file.Close()
hash := sha1.New()
_, err = io.Copy(hash, file)
logAndQuitIfError(err)
_, err = hash.Write([]byte(fmt.Sprintf("%#v%#v", filetypeForProjectFile(projectFilepath), ProjectConfig.ParserConfig)))
logAndQuitIfError(err)
b := []byte{}
h := hex.EncodeToString(hash.Sum(b))
return h[:7] // truncate to 7 chars
}
func translateProjectFile(projectFilepath, locale, prefix string) (hit bool, b []byte, err error) {
hash := projectFileHash(projectFilepath)
cacheFilePath := filepath.Join(cachePath, fmt.Sprintf("%s.%s", hash, locale))
// check cache
hit, b = getCachedTranslations(cacheFilePath)
if hit {
return hit, b, nil
}
// translate
b, err = translateViaSmartling(projectFilepath, prefix, locale)
if err != nil {
return
}
// write to cache
err = ioutil.WriteFile(cacheFilePath, b, 0644)
if err != nil {
return
}
return
}
func getCachedTranslations(cacheFilePath string) (hit bool, b []byte) {
if cacheFile, err := os.Open(cacheFilePath); err == nil {
if cfStat, err := cacheFile.Stat(); err == nil {
if time.Now().Sub(cfStat.ModTime()) < ProjectConfig.cacheMaxAge() {
if b, err = ioutil.ReadFile(cacheFilePath); err == nil {
return true, b // return the cached data
}
}
}
}
return
}
func findIdenticalRemoteFileOrPush(projectFilepath, prefix string) string {
remoteFile := projectFileRemoteName(projectFilepath, prefix)
allRemoteFiles := getRemoteFileList()
if allRemoteFiles.contains(remoteFile) {
// exact file already exists remotely
return remoteFile
}
for _, f := range allRemoteFiles {
if strings.Contains(f, fmt.Sprintf("/%s/", projectFileHash(projectFilepath))) {
// if file with the same hash exists remotely
return f
}
}
return pushProjectFile(projectFilepath, prefix)
}
func translateViaSmartling(projectFilepath, prefix, locale string) (b []byte, err error) {
remotePath := findIdenticalRemoteFileOrPush(projectFilepath, prefix)
b, err = client.DownloadTranslation(locale, smartling.FileDownloadRequest{
FileURIRequest: smartling.FileURIRequest{FileURI: remotePath},
})
fmt.Println("Downloaded", remotePath)
return
}