forked from gaowanliang/LightUploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.go
136 lines (122 loc) · 3.12 KB
/
i18n.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
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"time"
)
var bundle *i18n.Bundle
func init() {
bundle = i18n.NewBundle(language.SimplifiedChinese)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
dir, err := os.Executable()
dropErr(err)
dir = filepath.Dir(dir)
// fmt.Println(dir)
rd, err := ioutil.ReadDir(dir)
if err != nil {
log.Panic(err)
}
for _, fi := range rd {
if !fi.IsDir() && path.Ext(fi.Name()) == ".toml" {
bundle.LoadMessageFile(path.Join(dir, fi.Name()))
}
}
}
func dropErr(err error) {
if err != nil {
log.Panic(err)
}
}
func pageDownload(url string) string {
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
// 自定义Header
req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
resp, err := client.Do(req)
if err != nil {
fmt.Println("http get error", err)
return ""
}
//函数结束后关闭相关链接
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("read error", err)
return ""
}
return string(body)
}
func GetFileModTime(path string) int64 {
f, err := os.Open(path)
dropErr(err)
defer f.Close()
fi, err := f.Stat()
dropErr(err)
return fi.ModTime().Unix()
}
type Loc struct {
localize *i18n.Localizer
}
func (loc *Loc) init(locLanguage string) {
dir, err := os.Executable()
dropErr(err)
dir = filepath.Dir(dir)
//fmt.Println(dir)
dir = filepath.Join(dir, fmt.Sprintf("%s.toml", locLanguage))
_, err = os.Stat(dir)
if err != nil {
resp, err := http.Get(fmt.Sprintf("https://cdn.jsdelivr.net/gh/gaowanliang/OneDriveUploader/i18n/%s.toml", locLanguage))
dropErr(err)
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
dropErr(err)
ioutil.WriteFile(dir, data, 0666)
} else {
url := "https://cdn.jsdelivr.net/gh/gaowanliang/OneDriveUploader@latest/i18n/"
j := pageDownload(url)
var re = regexp.MustCompile(`(?m)i18n/(.*?)"[\s\S]*?<td class="time">(.*?)</td>`)
var newLanFileTime int64
for _, val := range re.FindAllStringSubmatch(j, -1) {
if fmt.Sprintf("%s.toml", locLanguage) == val[1] {
t, _ := time.Parse(time.RFC1123, val[2])
newLanFileTime = t.Unix()
}
}
oldLanFileTime := GetFileModTime(dir)
if newLanFileTime > oldLanFileTime {
err = os.RemoveAll(dir)
dropErr(err)
resp, err := http.Get(fmt.Sprintf("https://cdn.jsdelivr.net/gh/gaowanliang/OneDriveUploader/i18n/%s.toml", locLanguage))
dropErr(err)
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
dropErr(err)
ioutil.WriteFile(dir, data, 0644)
}
}
dir, err = os.Executable()
dropErr(err)
dir = filepath.Dir(dir)
rd, err := ioutil.ReadDir(dir)
if err != nil {
log.Panic(err)
}
for _, fi := range rd {
if !fi.IsDir() && path.Ext(fi.Name()) == ".toml" {
bundle.LoadMessageFile(path.Join(dir, fi.Name()))
}
}
loc.localize = i18n.NewLocalizer(bundle, locLanguage)
}
func (loc *Loc) print(tag string) string {
return loc.localize.MustLocalize(&i18n.LocalizeConfig{MessageID: tag})
}