mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
🌐 localization [skip ci]
This commit is contained in:
@@ -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")
|
||||
},
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -3,25 +3,48 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/naiba/nezha/service/singleton"
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type Cat struct {
|
||||
age int
|
||||
name string
|
||||
friends []string
|
||||
func htmlTemplateTranslateFn(id string, data interface{}, count interface{}) string {
|
||||
return singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: id,
|
||||
TemplateData: data,
|
||||
PluralCount: count,
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}
|
||||
b := Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}
|
||||
c := Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}
|
||||
wilson := []*Cat{&a, &b, &c}
|
||||
nikita := []Cat{}
|
||||
copier.Copy(&nikita, &wilson)
|
||||
singleton.InitLocalizer()
|
||||
fmt.Println(singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "nezhaMonitor",
|
||||
}))
|
||||
|
||||
nikita[0].friends = append(nikita[0].friends, "Syd")
|
||||
fmt.Println(singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
MessageID: "nezhaMonitor",
|
||||
}))
|
||||
|
||||
fmt.Println(wilson[0])
|
||||
fmt.Println(nikita[0])
|
||||
fmt.Println("tr nezhaMonitor", htmlTemplateTranslateFn("nezhaMonitor", nil, nil))
|
||||
fmt.Println("tr nezhaMonitor", htmlTemplateTranslateFn("nezhaMonitor", nil, 2))
|
||||
fmt.Println("tr nezhaMonitor", htmlTemplateTranslateFn("nezhaMonitor", map[string]string{
|
||||
"Ext": "Plus",
|
||||
}, 2))
|
||||
|
||||
bundle := i18n.NewBundle(language.English)
|
||||
localizer := i18n.NewLocalizer(bundle, "en")
|
||||
catsMessage := &i18n.Message{
|
||||
ID: "Cats",
|
||||
One: "I have {{.PluralCount}} cat.",
|
||||
Other: "I have {{.PluralCount}} cats.",
|
||||
}
|
||||
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
DefaultMessage: catsMessage,
|
||||
PluralCount: 1,
|
||||
}))
|
||||
fmt.Println(localizer.MustLocalize(&i18n.LocalizeConfig{
|
||||
DefaultMessage: catsMessage,
|
||||
PluralCount: 2,
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user