mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-09-05 23:56:50 +08:00
refactor: simplify T function and remove unused localization functions
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/krau/SaveAny-Bot/client/bot"
|
"github.com/krau/SaveAny-Bot/client/bot"
|
||||||
"github.com/krau/SaveAny-Bot/common/cache"
|
"github.com/krau/SaveAny-Bot/common/cache"
|
||||||
|
"github.com/krau/SaveAny-Bot/common/i18n"
|
||||||
"github.com/krau/SaveAny-Bot/common/utils/ioutil"
|
"github.com/krau/SaveAny-Bot/common/utils/ioutil"
|
||||||
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
|
"github.com/krau/SaveAny-Bot/common/utils/tgutil"
|
||||||
"github.com/krau/SaveAny-Bot/config"
|
"github.com/krau/SaveAny-Bot/config"
|
||||||
@@ -61,6 +62,7 @@ func Upload(cmd *cobra.Command, args []string) error {
|
|||||||
if err := config.Init(ctx, configFile); err != nil {
|
if err := config.Init(ctx, configFile); err != nil {
|
||||||
return fmt.Errorf("failed to load config: %w", err)
|
return fmt.Errorf("failed to load config: %w", err)
|
||||||
}
|
}
|
||||||
|
i18n.Init(config.C().Lang)
|
||||||
cache.Init()
|
cache.Init()
|
||||||
database.Init(ctx)
|
database.Init(ctx)
|
||||||
|
|
||||||
|
|||||||
+1
-51
@@ -44,57 +44,7 @@ func Init(lang string) {
|
|||||||
|
|
||||||
func T(key i18nk.Key, templateData ...map[string]any) string {
|
func T(key i18nk.Key, templateData ...map[string]any) string {
|
||||||
if localizer == nil || bundle == nil {
|
if localizer == nil || bundle == nil {
|
||||||
panic("localizer or bundle is not initialized, call Init() first")
|
Init("zh-Hans")
|
||||||
}
|
|
||||||
templateDataMap := make(map[string]any)
|
|
||||||
for _, data := range templateData {
|
|
||||||
maps.Copy(templateDataMap, data)
|
|
||||||
}
|
|
||||||
msg, err := localizer.Localize(&i18n.LocalizeConfig{
|
|
||||||
MessageID: string(key),
|
|
||||||
TemplateData: templateDataMap,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return string(key)
|
|
||||||
}
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
|
|
||||||
func TWithLang(lang, key string, templateData ...map[string]any) string {
|
|
||||||
if bundle == nil {
|
|
||||||
panic("bundle is not initialized, call Init() first")
|
|
||||||
}
|
|
||||||
templateDataMap := make(map[string]any)
|
|
||||||
for _, data := range templateData {
|
|
||||||
maps.Copy(templateDataMap, data)
|
|
||||||
}
|
|
||||||
localizerWithLang := i18n.NewLocalizer(bundle, lang)
|
|
||||||
msg, err := localizerWithLang.Localize(&i18n.LocalizeConfig{
|
|
||||||
MessageID: key,
|
|
||||||
TemplateData: templateDataMap,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only use in tests or packages that load before i18n
|
|
||||||
func TWithoutInit(lang string, key i18nk.Key, templateData ...map[string]any) string {
|
|
||||||
bundle := i18n.NewBundle(language.SimplifiedChinese)
|
|
||||||
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
|
|
||||||
files, err := localesFS.ReadDir("locale")
|
|
||||||
if err != nil {
|
|
||||||
return string(key)
|
|
||||||
}
|
|
||||||
for _, file := range files {
|
|
||||||
if _, err := bundle.LoadMessageFileFS(localesFS, "locale/"+file.Name()); err != nil {
|
|
||||||
return string(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
localizer := i18n.NewLocalizer(bundle, lang)
|
|
||||||
if localizer == nil {
|
|
||||||
return string(key)
|
|
||||||
}
|
}
|
||||||
templateDataMap := make(map[string]any)
|
templateDataMap := make(map[string]any)
|
||||||
for _, data := range templateData {
|
for _, data := range templateData {
|
||||||
|
|||||||
Reference in New Issue
Block a user