🌐 localization [skip ci]

This commit is contained in:
naiba
2022-04-27 23:51:45 +08:00
parent 0a6658fcb4
commit 7bd67b2233
15 changed files with 92 additions and 34 deletions

View File

@@ -12,6 +12,7 @@ import (
"code.cloudfoundry.org/bytefmt"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/naiba/nezha/pkg/mygin"
"github.com/naiba/nezha/service/singleton"
@@ -26,6 +27,18 @@ func ServeWeb(port uint) *http.Server {
}
r.Use(mygin.RecordPath)
r.SetFuncMap(template.FuncMap{
"tr": func(id string, dataAndCount ...interface{}) string {
conf := i18n.LocalizeConfig{
MessageID: id,
}
if len(dataAndCount) > 1 {
conf.TemplateData = dataAndCount[1]
}
if len(dataAndCount) > 2 {
conf.PluralCount = dataAndCount[2]
}
return singleton.Localizer.MustLocalize(&conf)
},
"tf": func(t time.Time) string {
return t.In(singleton.Loc).Format("2006年1月2号 15:04:05")
},

View File

@@ -2,12 +2,13 @@ package main
import (
"context"
"log"
"github.com/naiba/nezha/cmd/dashboard/controller"
"github.com/naiba/nezha/cmd/dashboard/rpc"
"github.com/naiba/nezha/model"
"github.com/naiba/nezha/service/singleton"
"github.com/ory/graceful"
"log"
)
func init() {
@@ -15,6 +16,7 @@ func init() {
singleton.Init()
singleton.InitConfigFromPath("data/config.yaml")
singleton.InitDBFromPath("data/sqlite.db")
singleton.InitLocalizer()
initSystem()
}