2024-04-12 09:17:10 +00:00
|
|
|
package translator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
)
|
|
|
|
|
2024-04-16 10:00:05 +00:00
|
|
|
type A struct {
|
|
|
|
a int `json:"*killaraCustomer"`
|
|
|
|
}
|
|
|
|
|
2024-04-12 09:17:10 +00:00
|
|
|
var Bundle *i18n.Bundle
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
_, currentFile, _, _ := runtime.Caller(0)
|
|
|
|
currentDir := filepath.Dir(currentFile)
|
|
|
|
|
|
|
|
// 解析模板文件
|
|
|
|
globpath := filepath.Join(currentDir, "/*.toml")
|
|
|
|
|
|
|
|
Bundle = i18n.NewBundle(language.Chinese)
|
2024-04-15 08:08:17 +00:00
|
|
|
Bundle.RegisterUnmarshalFunc("toml", func(data []byte, v interface{}) error {
|
|
|
|
var m map[string]any
|
|
|
|
err := toml.Unmarshal(data, &m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range m {
|
|
|
|
vm := v.(map[string]any)
|
|
|
|
desc := vm["description"].(string)
|
|
|
|
vm["one"] = desc
|
|
|
|
vm["other"] = desc
|
|
|
|
}
|
|
|
|
|
|
|
|
fixdata, err := toml.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toml.Unmarshal(fixdata, v)
|
|
|
|
})
|
2024-04-12 09:17:10 +00:00
|
|
|
matches, err := filepath.Glob(globpath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, m := range matches {
|
|
|
|
Bundle.MustLoadMessageFile(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Localize(MessageID TrCode, Lang string, MessageTemplateParam any) (string, error) {
|
|
|
|
var localizer = i18n.NewLocalizer(Bundle, Lang)
|
|
|
|
return localizer.Localize(&i18n.LocalizeConfig{
|
|
|
|
MessageID: string(MessageID),
|
|
|
|
TemplateData: MessageTemplateParam,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func LocalizeEx(MessageID TrCode, MessageTemplateParam any, Langs ...string) (string, error) {
|
|
|
|
var localizer = i18n.NewLocalizer(Bundle, Langs...)
|
|
|
|
|
|
|
|
return localizer.Localize(&i18n.LocalizeConfig{
|
|
|
|
MessageID: string(MessageID),
|
|
|
|
TemplateData: MessageTemplateParam,
|
|
|
|
})
|
|
|
|
}
|