From bfae6f793772c6da39e3106636dbb1dce78c0108 Mon Sep 17 00:00:00 2001 From: cuigh Date: Thu, 12 Oct 2017 11:04:27 +0800 Subject: [PATCH] Add i18n support --- biz/setting.go | 13 -------- config/i18n/en.yml | 48 +++++++++++++++++++++++++++++ config/i18n/zh.yml | 48 +++++++++++++++++++++++++++++ dao/mongo/mongo.go | 7 ++++- main.go | 9 +++++- misc/misc.go | 2 +- misc/util.go | 29 ++++++++++++++++++ views/_layouts/default.jet | 46 ++++++++++++++-------------- views/index.jet | 55 ++++++++++++++++------------------ views/login.jet | 10 +++---- views/system/setting/index.jet | 4 +-- 11 files changed, 196 insertions(+), 75 deletions(-) create mode 100644 config/i18n/en.yml create mode 100644 config/i18n/zh.yml diff --git a/biz/setting.go b/biz/setting.go index 3990b78..a8d28ac 100644 --- a/biz/setting.go +++ b/biz/setting.go @@ -12,7 +12,6 @@ import ( var Setting = &settingBiz{} type settingBiz struct { - loc *time.Location } func (b *settingBiz) Get() (setting *model.Setting, err error) { @@ -33,15 +32,3 @@ func (b *settingBiz) Update(setting *model.Setting, user web.User) (err error) { }) return } - -func (b *settingBiz) Time(t time.Time) string { - if b.loc == nil { - // todo: auto refresh settings after update - if s, err := b.Get(); err == nil && s != nil { - b.loc = time.FixedZone(s.TimeZone.Name, int(s.TimeZone.Offset)) - } else { - b.loc = time.Local - } - } - return t.In(b.loc).Format("2006-01-02 15:04:05") -} diff --git a/config/i18n/en.yml b/config/i18n/en.yml new file mode 100644 index 0000000..e33ffaa --- /dev/null +++ b/config/i18n/en.yml @@ -0,0 +1,48 @@ +# common +description: A Docker management tool, focused on Swarm cluster. +copyright: All rights reserved. + +# button +button.login: Sign in +button.logout: Sign out +button.search: Search +button.submit: Submit +button.cancel: Cancel +button.delete: Delete + +# menu +menu.dashboard: Dashboard +menu.local: Local +menu.image: Images +menu.container: Containers +menu.volume: Volumes +menu.swarm: Swarm +menu.registry: Registries +menu.node: Nodes +menu.network: Networks +menu.service: Services +menu.stack: Stacks +menu.task: Tasks +menu.secret: Secrets +menu.config: Config +menu.system: System +menu.role: Roles +menu.user: Users +menu.setting: Settings +menu.event: Events +menu.version: Version +menu.profile: Profile +menu.password: Password + +# dashboard +dashboard.node: Nodes +dashboard.network: Networks +dashboard.service: Services +dashboard.stack: Stacks +dashboard.feature: Features + +# login page +login.title: Sign in to Swirl +login.name: Login Name +login.password: Password +login.forgot-password: Forgot password? \ No newline at end of file diff --git a/config/i18n/zh.yml b/config/i18n/zh.yml new file mode 100644 index 0000000..d45661a --- /dev/null +++ b/config/i18n/zh.yml @@ -0,0 +1,48 @@ +# common +description: 一个 Docker 管理工具,专注于 Swarm 集群。 +copyright: 保留所有权利。 + +# button +button.login: 登录 +button.logout: 登出 +button.search: 搜索 +button.submit: 提交 +button.cancel: 取消 +button.delete: 删除 + +# menu +menu.dashboard: 仪表盘 +menu.local: 单机 +menu.image: 镜像 +menu.container: 容器 +menu.volume: 数据卷 +menu.swarm: 集群 +menu.registry: 镜像仓库 +menu.node: 节点 +menu.network: 网络 +menu.service: 服务 +menu.stack: 编排 +menu.task: 任务 +menu.secret: 私密配置 +menu.config: 常规配置 +menu.system: 系统 +menu.role: 角色 +menu.user: 用户 +menu.setting: 设置 +menu.event: 事件 +menu.version: 版本 +menu.profile: 资料 +menu.password: 密码 + +# dashboard +dashboard.node: 节点 +dashboard.network: 网络 +dashboard.service: 服务 +dashboard.stack: 编排 +dashboard.feature: 功能 + +# login page +login.title: 登录到 Swirl +login.name: 登录名 +login.password: 密码 +login.forgot-password: 忘记密码? diff --git a/dao/mongo/mongo.go b/dao/mongo/mongo.go index b327efe..f165593 100644 --- a/dao/mongo/mongo.go +++ b/dao/mongo/mongo.go @@ -3,6 +3,8 @@ package mongo import ( "errors" + "time" + "github.com/cuigh/auxo/log" "gopkg.in/mgo.v2" ) @@ -30,6 +32,9 @@ var ( mgo.Index{Key: []string{"name"}}, mgo.Index{Key: []string{"-time"}}, }, + "template": { + mgo.Index{Key: []string{"name"}, Unique: true}, + }, } ) @@ -59,7 +64,7 @@ func New(addr string) (*Dao, error) { return nil, errors.New("database address must be configured for mongo storage") } - s, err := mgo.Dial(addr) + s, err := mgo.DialWithTimeout(addr, time.Second*5) if err != nil { return nil, err } diff --git a/main.go b/main.go index beabc48..3096a85 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "net/http" "path/filepath" "runtime" @@ -19,6 +20,11 @@ import ( ) func main() { + setting, err := biz.Setting.Get() + if err != nil { + panic(fmt.Sprintf("Load setting failed: %v", err)) + } + // customize error handler web.DefaultErrorHandler.OnCode(http.StatusNotFound, func(ctx web.Context, err error) { ctx.Redirect("/404") @@ -31,7 +37,8 @@ func main() { // set render/validator.. ws.Renderer = jet.New().SetDebug(config.App().Debug). - AddFunc("time", biz.Setting.Time). + AddFunc("time", misc.FormatTime(setting.TimeZone.Offset)). + AddFunc("i18n", misc.Message(setting.Language)). AddFuncs(misc.Funcs). AddVariable("version", misc.Version). AddVariable("go_version", runtime.Version()) diff --git a/misc/misc.go b/misc/misc.go index 2c866d4..2f71225 100644 --- a/misc/misc.go +++ b/misc/misc.go @@ -9,7 +9,7 @@ import ( const ( // Version is the version of Swirl - Version = "0.5.4" + Version = "0.5.5" ) const ( diff --git a/misc/util.go b/misc/util.go index 5c518fe..5feab07 100644 --- a/misc/util.go +++ b/misc/util.go @@ -6,6 +6,9 @@ import ( "fmt" "reflect" "strings" + "time" + + "github.com/cuigh/auxo/util/i18n" ) var Funcs = map[string]interface{}{ @@ -43,6 +46,32 @@ var Funcs = map[string]interface{}{ }, } +func Message(lang string) func(key string, args ...interface{}) string { + t, err := i18n.Find(lang, "en") + if err != nil { + panic(err) + } + + return func(key string, args ...interface{}) string { + return t.Format(key, args...) + } +} + +func FormatTime(offset int32) func(t time.Time) string { + const layout = "2006-01-02 15:04:05" + + var loc *time.Location + if offset == 0 { + loc = time.Local + } else { + loc = time.FixedZone("", int(offset)) + } + + return func(t time.Time) string { + return t.In(loc).Format(layout) + } +} + func Page(count, pageIndex, pageSize int) (start, end int) { start = pageSize * (pageIndex - 1) end = pageSize * pageIndex diff --git a/views/_layouts/default.jet b/views/_layouts/default.jet index 70f7924..4ad3585 100644 --- a/views/_layouts/default.jet +++ b/views/_layouts/default.jet @@ -23,38 +23,38 @@