package translator import ( "path/filepath" "runtime" "github.com/nicksnyder/go-i18n/v2/i18n" "github.com/pelletier/go-toml/v2" "golang.org/x/text/language" ) 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", toml.Unmarshal) matches, err := filepath.Glob(globpath) if err != nil { panic(err) } for _, m := range matches { Bundle.MustLoadMessageFile(m) } // localizer := i18n.NewLocalizer(Bundle, "zh_cn") // log.Println(localizer.Localize(&i18n.LocalizeConfig{ // MessageID: "format_account_is_insufficient", // TemplateData: map[string]any{ // "CatalogCurrency": "USDT", // }, // })) } 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, }) }