From 84279e9bfc19d5963e9eb6ce79bd6bcf87722401 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 18 Oct 2022 23:12:35 +0800 Subject: [PATCH] [fix] unit-test --- i18n/i18n.go | 8 ++++++-- widgets/app/app_test.go | 8 +++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/i18n/i18n.go b/i18n/i18n.go index cfddf2ee..c38ed4b2 100644 --- a/i18n/i18n.go +++ b/i18n/i18n.go @@ -1,6 +1,7 @@ package i18n import ( + "crypto/sha256" "fmt" "path/filepath" "strings" @@ -76,7 +77,9 @@ func Load(cfg config.Config) error { func Trans(langName string, widgets []string, data interface{}) (interface{}, error) { // Get From cache - key := fmt.Sprintf("%s::%s", langName, strings.Join(widgets, "::")) + hash := sha256.Sum256([]byte(fmt.Sprintf("%v", data))) + key := fmt.Sprintf("%s::%s::%s", langName, strings.Join(widgets, "::"), hash) + if res, has := cache.data[key]; has { return res, nil } @@ -99,7 +102,8 @@ func Trans(langName string, widgets []string, data interface{}) (interface{}, er func cacheSet(dict *lang.Dict, widgets []string, value interface{}) { cache.mu.Lock() defer cache.mu.Unlock() - key := fmt.Sprintf("%s::%s", dict.Name, strings.Join(widgets, "::")) + hash := sha256.Sum256([]byte(fmt.Sprintf("%v", value))) + key := fmt.Sprintf("%s::%s::%s", dict.Name, strings.Join(widgets, "::"), hash) cache.data[key] = value } diff --git a/widgets/app/app_test.go b/widgets/app/app_test.go index 1d9060b1..a14db25d 100644 --- a/widgets/app/app_test.go +++ b/widgets/app/app_test.go @@ -142,6 +142,8 @@ func TestExport(t *testing.T) { func TestProcessSetting(t *testing.T) { loadApp(t) + backup := config.Conf.Lang + config.Conf.Lang = "en-us" res, err := gou.NewProcess("yao.app.Setting").Exec() if err != nil { t.Fatal(err) @@ -169,10 +171,14 @@ func TestProcessSetting(t *testing.T) { lang := gou.NewProcess("yao.app.Setting").WithSID(setting.Sid).Lang() assert.Equal(t, "zh-hk", lang) + + config.Conf.Lang = backup } func TestProcessXgen(t *testing.T) { loadApp(t) + backup := config.Conf.Lang + config.Conf.Lang = "en-us" res, err := gou.NewProcess("yao.app.Xgen").Exec() if err != nil { t.Fatal(err) @@ -207,7 +213,7 @@ func TestProcessXgen(t *testing.T) { lang := gou.NewProcess("yao.app.Setting").WithSID(xgen2.Get("sid").(string)).Lang() assert.Equal(t, "zh-hk", lang) - + config.Conf.Lang = backup } func TestProcessMenu(t *testing.T) {