2022-08-16 23:30:23 +08:00
|
|
|
package i18n
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"strings"
|
|
|
|
|
2024-11-27 16:02:35 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
|
|
"golang.org/x/text/language"
|
2023-03-11 19:55:37 +08:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-08-16 23:30:23 +08:00
|
|
|
)
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func GetMsgWithMap(key string, maps map[string]interface{}) string {
|
2024-02-19 13:55:27 +08:00
|
|
|
var content string
|
2022-08-16 23:30:23 +08:00
|
|
|
if maps == nil {
|
2024-02-19 13:55:27 +08:00
|
|
|
content, _ = global.I18n.Localize(&i18n.LocalizeConfig{
|
2023-03-20 10:38:39 +08:00
|
|
|
MessageID: key,
|
2022-08-16 23:30:23 +08:00
|
|
|
})
|
|
|
|
} else {
|
2024-02-19 13:55:27 +08:00
|
|
|
content, _ = global.I18n.Localize(&i18n.LocalizeConfig{
|
2023-03-20 10:38:39 +08:00
|
|
|
MessageID: key,
|
2022-08-16 23:30:23 +08:00
|
|
|
TemplateData: maps,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
content = strings.ReplaceAll(content, ": <no value>", "")
|
|
|
|
if content == "" {
|
2023-03-20 10:38:39 +08:00
|
|
|
return key
|
2022-08-16 23:30:23 +08:00
|
|
|
} else {
|
|
|
|
return content
|
2024-04-24 17:34:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMsgWithName(key string, name string, err error) string {
|
|
|
|
var (
|
|
|
|
content string
|
|
|
|
dataMap = make(map[string]interface{})
|
|
|
|
)
|
|
|
|
dataMap["name"] = name
|
|
|
|
if err != nil {
|
|
|
|
dataMap["err"] = err.Error()
|
|
|
|
}
|
|
|
|
content, _ = global.I18n.Localize(&i18n.LocalizeConfig{
|
|
|
|
MessageID: key,
|
|
|
|
TemplateData: dataMap,
|
|
|
|
})
|
|
|
|
content = strings.ReplaceAll(content, "<no value>", "")
|
|
|
|
if content == "" {
|
|
|
|
return key
|
|
|
|
} else {
|
|
|
|
return content
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 10:38:39 +08:00
|
|
|
func GetErrMsg(key string, maps map[string]interface{}) string {
|
2024-02-19 13:55:27 +08:00
|
|
|
var content string
|
2022-12-01 15:48:40 +08:00
|
|
|
if maps == nil {
|
2024-02-19 13:55:27 +08:00
|
|
|
content, _ = global.I18n.Localize(&i18n.LocalizeConfig{
|
2023-03-20 10:38:39 +08:00
|
|
|
MessageID: key,
|
2022-12-01 15:48:40 +08:00
|
|
|
})
|
|
|
|
} else {
|
2024-02-19 13:55:27 +08:00
|
|
|
content, _ = global.I18n.Localize(&i18n.LocalizeConfig{
|
2023-03-20 10:38:39 +08:00
|
|
|
MessageID: key,
|
2022-12-01 15:48:40 +08:00
|
|
|
TemplateData: maps,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2023-05-22 17:10:51 +08:00
|
|
|
func GetMsgByKey(key string) string {
|
2024-02-19 13:55:27 +08:00
|
|
|
content, _ := global.I18n.Localize(&i18n.LocalizeConfig{
|
2023-05-22 17:10:51 +08:00
|
|
|
MessageID: key,
|
|
|
|
})
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2022-08-16 23:30:23 +08:00
|
|
|
//go:embed lang/*
|
|
|
|
var fs embed.FS
|
2024-02-19 13:55:27 +08:00
|
|
|
var bundle *i18n.Bundle
|
2022-08-16 23:30:23 +08:00
|
|
|
|
2024-02-19 13:55:27 +08:00
|
|
|
func UseI18n() gin.HandlerFunc {
|
|
|
|
return func(context *gin.Context) {
|
|
|
|
lang := context.GetHeader("Accept-Language")
|
|
|
|
if lang == "" {
|
|
|
|
lang = "zh"
|
|
|
|
}
|
|
|
|
global.I18n = i18n.NewLocalizer(bundle, lang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init() {
|
2024-11-27 16:02:35 +08:00
|
|
|
if bundle != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-02-19 13:55:27 +08:00
|
|
|
bundle = i18n.NewBundle(language.Chinese)
|
|
|
|
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
|
|
|
|
_, _ = bundle.LoadMessageFileFS(fs, "lang/zh.yaml")
|
|
|
|
_, _ = bundle.LoadMessageFileFS(fs, "lang/en.yaml")
|
|
|
|
_, _ = bundle.LoadMessageFileFS(fs, "lang/zh-Hant.yaml")
|
2024-11-27 16:02:35 +08:00
|
|
|
_, _ = bundle.LoadMessageFileFS(fs, "lang/fa.yaml")
|
|
|
|
}
|
|
|
|
|
|
|
|
func UseI18nForCmd(lang string) {
|
|
|
|
if lang == "" {
|
|
|
|
lang = "zh"
|
|
|
|
}
|
|
|
|
if bundle == nil {
|
|
|
|
Init()
|
|
|
|
}
|
|
|
|
global.I18nForCmd = i18n.NewLocalizer(bundle, lang)
|
|
|
|
}
|
|
|
|
func GetMsgByKeyForCmd(key string) string {
|
|
|
|
if global.I18nForCmd == nil {
|
|
|
|
UseI18nForCmd("")
|
|
|
|
}
|
|
|
|
content, _ := global.I18nForCmd.Localize(&i18n.LocalizeConfig{
|
|
|
|
MessageID: key,
|
|
|
|
})
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
func GetMsgWithMapForCmd(key string, maps map[string]interface{}) string {
|
|
|
|
if global.I18nForCmd == nil {
|
|
|
|
UseI18nForCmd("")
|
|
|
|
}
|
|
|
|
var content string
|
|
|
|
if maps == nil {
|
|
|
|
content, _ = global.I18nForCmd.Localize(&i18n.LocalizeConfig{
|
|
|
|
MessageID: key,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
content, _ = global.I18nForCmd.Localize(&i18n.LocalizeConfig{
|
|
|
|
MessageID: key,
|
|
|
|
TemplateData: maps,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
content = strings.ReplaceAll(content, ": <no value>", "")
|
|
|
|
if content == "" {
|
|
|
|
return key
|
|
|
|
} else {
|
|
|
|
return content
|
|
|
|
}
|
2022-08-16 23:30:23 +08:00
|
|
|
}
|