translator.go 642 B

1234567891011121314151617181920212223242526272829303132
  1. package common
  2. import (
  3. "log/slog"
  4. )
  5. import (
  6. "github.com/nicksnyder/go-i18n/v2/i18n"
  7. "github.com/pelletier/go-toml/v2"
  8. "golang.org/x/text/language"
  9. )
  10. var bundle *i18n.Bundle
  11. func SetupTrans() {
  12. slog.Info("setup trans")
  13. bundle = i18n.NewBundle(language.English)
  14. bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
  15. _, err := bundle.LoadMessageFile("static/locale/common.en.toml")
  16. if err != nil {
  17. panic(err)
  18. }
  19. _, err = bundle.LoadMessageFile("static/locale/common.zh.toml")
  20. if err != nil {
  21. panic(err)
  22. }
  23. slog.Info("setup trans ok")
  24. }
  25. func GetTrans(lang string) *i18n.Localizer {
  26. return i18n.NewLocalizer(bundle, lang)
  27. }