package translator import ( "path/filepath" "runtime" "github.com/nicksnyder/go-i18n/v2/i18n" "github.com/pelletier/go-toml/v2" "golang.org/x/text/language" ) type A struct { a int `json:"*killaraCustomer"` } 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) 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) }) 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, }) }