From 1044ffd8b07100d6206da9b04501f166f4b86b0a Mon Sep 17 00:00:00 2001 From: gaowanliang <1175373504@qq.com> Date: Tue, 6 Apr 2021 20:29:26 +0800 Subject: [PATCH] Change language pack storage location --- i18n.go | 38 ++++++++++++++++++++++---------------- main.go | 26 +++++++++++++------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/i18n.go b/i18n.go index 43a158f..d32d212 100644 --- a/i18n.go +++ b/i18n.go @@ -20,28 +20,20 @@ var bundle *i18n.Bundle func init() { bundle = i18n.NewBundle(language.SimplifiedChinese) bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) - _, 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(".") + 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(fi.Name()) + bundle.LoadMessageFile(path.Join(dir, fi.Name())) } } + } func dropErr(err error) { @@ -88,8 +80,10 @@ type Loc struct { } func (loc *Loc) init(locLanguage string) { - dir, err := filepath.Abs(filepath.Dir(os.Args[0])) + 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 { @@ -123,6 +117,18 @@ func (loc *Loc) init(locLanguage string) { 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 { diff --git a/main.go b/main.go index 0090281..2236746 100644 --- a/main.go +++ b/main.go @@ -79,15 +79,7 @@ func restore(restoreSrvc *upload.RestoreService, filesToRestore map[string]fileu pool := make(chan struct{}, threads) checkPath := make(map[string]bool, 0) pathFiles := make(map[string]map[string]bool, 0) - temps, _botKey, iUserID := sendMsg() - var iSendMsg func(string) - temp := func(text string) { - temps(text) - if _botKey != "" && iUserID != "" { - iSendMsg(text) - } - } for filePath, fileInfo := range filesToRestore { wg.Add(1) pool <- struct{}{} @@ -102,7 +94,7 @@ func restore(restoreSrvc *upload.RestoreService, filesToRestore map[string]fileu paths = paths[:len(paths)-1] } if _, ok := checkPath[paths]; !ok { - userID, bearerToken := httpLocal.GetMyIDAndBearer(infoPath, thread, block, lang, timeOut, _botKey, _UserID) + userID, bearerToken := httpLocal.GetMyIDAndBearer(infoPath, thread, block, lang, timeOut, botKey, _UserID) files, _ := restoreSrvc.GetDriveItem(userID, bearerToken, paths) checkPath[paths] = true pathFiles[paths] = files @@ -115,17 +107,25 @@ func restore(restoreSrvc *upload.RestoreService, filesToRestore map[string]fileu defer func() { <-pool }() - if _, ok := pathFiles[paths][fileName]; !ok || mode == 0 { - tip := "`" + filePath + "`" + loc.print("startToUpload1") + temps, _botKey, iUserID := sendMsg() + var iSendMsg func(string) + tip := "`" + filePath + "`" + loc.print("startToUpload1") + if _botKey != "" && iUserID != "" { + iSendMsg = botSend(_botKey, iUserID, tip) + } + temp := func(text string) { + temps(text) if _botKey != "" && iUserID != "" { - iSendMsg = botSend(_botKey, iUserID, tip) + iSendMsg(text) } + } + if _, ok := pathFiles[paths][fileName]; !ok || mode == 0 { temp(tip) userID, bearerToken := httpLocal.GetMyIDAndBearer(infoPath, thread, block, lang, timeOut, _botKey, _UserID) username := strings.ReplaceAll(filepath.Base(infoPath), ".json", "") restoreSrvc.SimpleUploadToOriginalLoc(userID, bearerToken, "replace", targetFolder, filePath, fileInfo, temp, locText, username) } else { - tip := filePath + "已存在,自动跳过" + tip = filePath + "已存在,自动跳过" if _botKey != "" && iUserID != "" { iSendMsg = botSend(_botKey, iUserID, tip) }