translator.go 632 B

12345678910111213141516171819202122232425262728293031
  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. for _, filename := range MetadataConfig.Locales {
  16. slog.Info("loading locale", "filename", filename)
  17. _, err := bundle.LoadMessageFile(filename)
  18. if err != nil {
  19. panic(err)
  20. }
  21. }
  22. slog.Info("setup trans ok")
  23. }
  24. func GetTrans(lang string) *i18n.Localizer {
  25. return i18n.NewLocalizer(bundle, lang)
  26. }