精简: 去掉 Telegram 机器人+通知: tgbot 包/stats-notify 与 hash 清理 job/登录通知/backuptotgbot/testTgBot/全部 tg* 设置/locale Bot 本地化/SecretClears 机制; 连带清理 Swagger/外部流量/邮件/2FA 的残留测试
This commit is contained in:
@@ -19,24 +19,17 @@ import (
|
||||
var (
|
||||
i18nBundle *i18n.Bundle
|
||||
LocalizerWeb *i18n.Localizer
|
||||
LocalizerBot *i18n.Localizer
|
||||
)
|
||||
|
||||
// I18nType represents the type of interface for internationalization.
|
||||
type I18nType string
|
||||
|
||||
const (
|
||||
Bot I18nType = "bot" // Bot interface type
|
||||
Web I18nType = "web" // Web interface type
|
||||
)
|
||||
|
||||
// SettingService interface defines methods for accessing locale settings.
|
||||
type SettingService interface {
|
||||
GetTgLang() (string, error)
|
||||
}
|
||||
|
||||
// InitLocalizer initializes the internationalization system with embedded translation files.
|
||||
func InitLocalizer(i18nFS embed.FS, settingService SettingService) error {
|
||||
func InitLocalizer(i18nFS embed.FS) error {
|
||||
// set default bundle to English
|
||||
i18nBundle = i18n.NewBundle(language.MustParse("en-US"))
|
||||
i18nBundle.RegisterUnmarshalFunc("json", json.Unmarshal)
|
||||
@@ -46,11 +39,6 @@ func InitLocalizer(i18nFS embed.FS, settingService SettingService) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// setup bot locale
|
||||
if err := initTGBotLocalizer(settingService); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -71,14 +59,12 @@ func createTemplateData(params []string, separator ...string) map[string]any {
|
||||
}
|
||||
|
||||
// I18n retrieves a localized message for the given key and type.
|
||||
// It supports both bot and web contexts, with optional template parameters.
|
||||
// It supports web contexts, with optional template parameters.
|
||||
// Returns the localized message or an empty string if localization fails.
|
||||
func I18n(i18nType I18nType, key string, params ...string) string {
|
||||
var localizer *i18n.Localizer
|
||||
|
||||
switch i18nType {
|
||||
case "bot":
|
||||
localizer = LocalizerBot
|
||||
case "web":
|
||||
localizer = LocalizerWeb
|
||||
default:
|
||||
@@ -105,17 +91,6 @@ func I18n(i18nType I18nType, key string, params ...string) string {
|
||||
return msg
|
||||
}
|
||||
|
||||
// initTGBotLocalizer initializes the bot localizer with the configured language.
|
||||
func initTGBotLocalizer(settingService SettingService) error {
|
||||
botLang, err := settingService.GetTgLang()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
LocalizerBot = i18n.NewLocalizer(i18nBundle, botLang)
|
||||
return nil
|
||||
}
|
||||
|
||||
// LocalizerMiddleware returns a Gin middleware that sets up localization for web requests.
|
||||
// It determines the user's language from cookies or Accept-Language header,
|
||||
// creates a localizer instance, and stores it in the Gin context for use in handlers.
|
||||
|
||||
Reference in New Issue
Block a user