diff --git a/engine/load.go b/engine/load.go index 06d705e1..908c9e5b 100644 --- a/engine/load.go +++ b/engine/load.go @@ -17,6 +17,7 @@ import ( "github.com/yaoapp/yao/connector" "github.com/yaoapp/yao/flow" "github.com/yaoapp/yao/fs" + "github.com/yaoapp/yao/i18n" "github.com/yaoapp/yao/importer" "github.com/yaoapp/yao/model" "github.com/yaoapp/yao/page" @@ -65,10 +66,18 @@ func Load(cfg config.Config) (err error) { printErr(cfg.Mode, "FileSystem", err) } - // Load Studio - err = studio.Load(cfg) + // Load i18n + err = i18n.Load(cfg) if err != nil { - printErr(cfg.Mode, "Studio", err) + printErr(cfg.Mode, "i18n", err) + } + + // Load Studio development mode only + if cfg.Mode == "development" { + err = studio.Load(cfg) + if err != nil { + printErr(cfg.Mode, "Studio", err) + } } // 第二步: 建立数据库 & 会话连接 diff --git a/widgets/app/app.go b/widgets/app/app.go index 4c0eb5f4..a0a6a8c8 100644 --- a/widgets/app/app.go +++ b/widgets/app/app.go @@ -6,9 +6,11 @@ import ( "os" "path/filepath" "strings" + "time" jsoniter "github.com/json-iterator/go" "github.com/yaoapp/gou" + "github.com/yaoapp/gou/session" "github.com/yaoapp/kun/exception" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/data" @@ -110,6 +112,19 @@ func exportAPI() error { } http.Paths = append(http.Paths, path) + // POST + path = gou.Path{ + Label: "App Setting", + Description: "App Setting", + Guard: "-", + Path: "/setting", + Method: "POST", + Process: process, + In: []string{":payload"}, + Out: gou.Out{Status: 200, Type: "application/json"}, + } + http.Paths = append(http.Paths, path) + process = "yao.app.Menu" args := []string{} if Setting.Menu.Process != "" { @@ -219,11 +234,33 @@ func processSetting(process *gou.Process) interface{} { return nil } + sid := process.Sid + if sid == "" { + sid = session.ID() + } + + // Set User ENV + if process.NumOfArgs() > 0 { + payload := process.ArgsMap(0, map[string]interface{}{ + "now": time.Now().Unix(), + "lang": "en-us", + "sid": "", + }) + + if payload["sid"] != "" { + sid = payload["sid"].(string) + } + + lang := fmt.Sprintf("%v", payload["lang"]) + session.Global().ID(sid).Set("__yao_lang", lang) + } + setting, err := i18n.Trans(process.Lang(), "app", "app", Setting) if err != nil { exception.New(err.Error(), 500).Throw() } + setting.(*DSL).Sid = sid return *setting.(*DSL) } @@ -233,6 +270,27 @@ func processXgen(process *gou.Process) interface{} { exception.New("the app does not init", 500).Throw() } + sid := process.Sid + if sid == "" { + sid = session.ID() + } + + // Set User ENV + if process.NumOfArgs() > 0 { + payload := process.ArgsMap(0, map[string]interface{}{ + "now": time.Now().Unix(), + "lang": "en-us", + "sid": "", + }) + + if payload["sid"] != "" { + sid = payload["sid"].(string) + } + + lang := fmt.Sprintf("%v", payload["lang"]) + session.Global().ID(sid).Set("__yao_lang", lang) + } + mode := os.Getenv("YAO_ENV") if mode == "" { mode = "production" @@ -324,6 +382,7 @@ func processXgen(process *gou.Process) interface{} { exception.New(err.Error(), 500).Throw() } + setting.(map[string]interface{})["sid"] = sid return setting.(map[string]interface{}) } diff --git a/widgets/app/app_test.go b/widgets/app/app_test.go index eeb787ef..021e670f 100644 --- a/widgets/app/app_test.go +++ b/widgets/app/app_test.go @@ -118,7 +118,7 @@ func TestExport(t *testing.T) { api, has := gou.APIs["widgets.app"] assert.True(t, has) - assert.Equal(t, 3, len(api.HTTP.Paths)) + assert.Equal(t, 4, len(api.HTTP.Paths)) _, has = gou.ThirdHandlers["yao.app.setting"] assert.True(t, has) @@ -146,6 +146,19 @@ func TestProcessSetting(t *testing.T) { assert.Equal(t, "flows.app.menu", setting.Menu.Process) assert.Equal(t, true, setting.Optional.HideNotification) assert.Equal(t, false, setting.Optional.HideSetting) + assert.Equal(t, true, setting.Sid != "") + + // Set + res, err = gou.NewProcess("yao.app.Setting", map[string]interface{}{"lang": "zh-hk", "sid": setting.Sid}).Exec() + if err != nil { + t.Fatal(err) + } + + setting2, ok := res.(DSL) + assert.Equal(t, setting.Sid, setting2.Sid) + + lang := gou.NewProcess("yao.app.Setting").WithSID(setting.Sid).Lang() + assert.Equal(t, "zh-hk", lang) } func TestProcessXgen(t *testing.T) { @@ -171,6 +184,20 @@ func TestProcessXgen(t *testing.T) { assert.Equal(t, "Demo Application", xgen.Get("name")) assert.Equal(t, true, xgen.Get("optional.hideNotification")) assert.Equal(t, "localStorage", xgen.Get("token")) + assert.Equal(t, true, xgen.Get("sid").(string) != "") + + // Set + res, err = gou.NewProcess("yao.app.Xgen", map[string]interface{}{"lang": "zh-hk", "sid": xgen.Get("sid")}).Exec() + if err != nil { + t.Fatal(err) + } + + xgen2 := any.Of(res).MapStr().Dot() + assert.Equal(t, xgen.Get("sid"), xgen2.Get("sid")) + + lang := gou.NewProcess("yao.app.Setting").WithSID(xgen2.Get("sid").(string)).Lang() + assert.Equal(t, "zh-hk", lang) + } func TestProcessMenu(t *testing.T) { diff --git a/widgets/app/types.go b/widgets/app/types.go index ec6adb41..4e4c7832 100644 --- a/widgets/app/types.go +++ b/widgets/app/types.go @@ -7,6 +7,8 @@ type DSL struct { Version string `json:"version,omitempty"` Description string `json:"description,omitempty"` Theme string `json:"theme,omitempty"` + Lang string `json:"lang,omitempty"` + Sid string `json:"sid,omitempty"` Logo string `json:"logo,omitempty"` Favicon string `json:"favicon,omitempty"` Menu MenuDSL `json:"menu,omitempty"`