Skip to content

Commit

Permalink
Fixed language pack download mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanliang committed Apr 5, 2021
1 parent 1648b2e commit 8bd1966
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 16 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.history
config.json
temp.torrent
.goreleaser.yml
dist
.idea
Expand All @@ -9,4 +7,5 @@ info
*.exe
od
TestUploadFolder
OneDriveUploader
OneDriveUploader
*.toml
107 changes: 94 additions & 13 deletions i18n.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,125 @@
package main

import (
"io/ioutil"
"log"
"path"

"fmt"
"github.com/BurntSushi/toml"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"regexp"
"time"
)

var bundle *i18n.Bundle

func init() {
bundle = i18n.NewBundle(language.SimplifiedChinese)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
rd, err := ioutil.ReadDir("i18n")
_, err := os.Stat("i18n")
if err == nil {
rd, err := ioutil.ReadDir("i18n")
if err != nil {
log.Panic(err)
}
for _, fi := range rd {
if !fi.IsDir() && path.Ext(fi.Name()) == ".toml" {
bundle.LoadMessageFile("i18n/" + fi.Name())
}
}
}

rd, err := ioutil.ReadDir(".")
if err != nil {
log.Panic(err)
}
for _, fi := range rd {
if !fi.IsDir() && path.Ext(fi.Name()) == ".toml" {
bundle.LoadMessageFile("i18n/" + fi.Name())
bundle.LoadMessageFile(fi.Name())
}
}
/*bundle.MustParseMessageFileBytes([]byte(
}

), "zh-CN.toml")
bundle.MustParseMessageFileBytes([]byte(`
HelloWorld = "Hello World!"
`), "en.toml")*/
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(tag string) {
loc.localize = i18n.NewLocalizer(bundle, tag)
func (loc *Loc) init(locLanguage string) {
_, err := os.Stat(fmt.Sprintf("%s.toml", locLanguage))
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(fmt.Sprintf("%s.toml", locLanguage), 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(fmt.Sprintf("%s.toml", locLanguage))
if newLanFileTime > oldLanFileTime {
err := os.RemoveAll(fmt.Sprintf("%s.toml", locLanguage))
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(fmt.Sprintf("%s.toml", locLanguage), data, 0644)
}
}
loc.localize = i18n.NewLocalizer(bundle, locLanguage)
}
func (loc *Loc) print(tag string) string {
return loc.localize.MustLocalize(&i18n.LocalizeConfig{MessageID: tag})
Expand Down

0 comments on commit 8bd1966

Please sign in to comment.