1
0

locale.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package locale
  2. import (
  3. "embed"
  4. "io/fs"
  5. "strings"
  6. "x-ui/logger"
  7. "github.com/gin-gonic/gin"
  8. "github.com/nicksnyder/go-i18n/v2/i18n"
  9. "github.com/pelletier/go-toml/v2"
  10. "golang.org/x/text/language"
  11. )
  12. var (
  13. i18nBundle *i18n.Bundle
  14. LocalizerWeb *i18n.Localizer
  15. LocalizerBot *i18n.Localizer
  16. )
  17. type I18nType string
  18. const (
  19. Bot I18nType = "bot"
  20. Web I18nType = "web"
  21. )
  22. type SettingService interface {
  23. GetTgLang() (string, error)
  24. }
  25. func InitLocalizer(i18nFS embed.FS, settingService SettingService) error {
  26. // set default bundle to english
  27. i18nBundle = i18n.NewBundle(language.MustParse("en-US"))
  28. i18nBundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
  29. // parse files
  30. if err := parseTranslationFiles(i18nFS, i18nBundle); err != nil {
  31. return err
  32. }
  33. // setup bot locale
  34. if err := initTGBotLocalizer(settingService); err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. func createTemplateData(params []string, seperator ...string) map[string]interface{} {
  40. var sep string = "=="
  41. if len(seperator) > 0 {
  42. sep = seperator[0]
  43. }
  44. templateData := make(map[string]interface{})
  45. for _, param := range params {
  46. parts := strings.SplitN(param, sep, 2)
  47. templateData[parts[0]] = parts[1]
  48. }
  49. return templateData
  50. }
  51. func I18n(i18nType I18nType, key string, params ...string) string {
  52. var localizer *i18n.Localizer
  53. switch i18nType {
  54. case "bot":
  55. localizer = LocalizerBot
  56. case "web":
  57. localizer = LocalizerWeb
  58. default:
  59. logger.Errorf("Invalid type for I18n: %s", i18nType)
  60. return ""
  61. }
  62. templateData := createTemplateData(params)
  63. msg, err := localizer.Localize(&i18n.LocalizeConfig{
  64. MessageID: key,
  65. TemplateData: templateData,
  66. })
  67. if err != nil {
  68. logger.Errorf("Failed to localize message: %v", err)
  69. return ""
  70. }
  71. return msg
  72. }
  73. func initTGBotLocalizer(settingService SettingService) error {
  74. botLang, err := settingService.GetTgLang()
  75. if err != nil {
  76. return err
  77. }
  78. LocalizerBot = i18n.NewLocalizer(i18nBundle, botLang)
  79. return nil
  80. }
  81. func LocalizerMiddleware() gin.HandlerFunc {
  82. return func(c *gin.Context) {
  83. var lang string
  84. if cookie, err := c.Request.Cookie("lang"); err == nil {
  85. lang = cookie.Value
  86. } else {
  87. lang = c.GetHeader("Accept-Language")
  88. }
  89. LocalizerWeb = i18n.NewLocalizer(i18nBundle, lang)
  90. c.Set("localizer", LocalizerWeb)
  91. c.Set("I18n", I18n)
  92. c.Next()
  93. }
  94. }
  95. func parseTranslationFiles(i18nFS embed.FS, i18nBundle *i18n.Bundle) error {
  96. err := fs.WalkDir(i18nFS, "translation",
  97. func(path string, d fs.DirEntry, err error) error {
  98. if err != nil {
  99. return err
  100. }
  101. if d.IsDir() {
  102. return nil
  103. }
  104. data, err := i18nFS.ReadFile(path)
  105. if err != nil {
  106. return err
  107. }
  108. _, err = i18nBundle.ParseMessageFileBytes(data, path)
  109. return err
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. return nil
  115. }