mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-07-31 10:49:00 +08:00
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package redis
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"GoNavi-Wails/shared/i18n"
|
|
)
|
|
|
|
var (
|
|
redisBackendTextMu sync.RWMutex
|
|
redisBackendTextLanguage = i18n.LanguageZhCN
|
|
redisBackendTextLocalizer *i18n.Localizer
|
|
)
|
|
|
|
func SetBackendLanguage(language i18n.Language) {
|
|
normalized, ok := i18n.NormalizeLanguage(string(language))
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
redisBackendTextMu.Lock()
|
|
defer redisBackendTextMu.Unlock()
|
|
|
|
redisBackendTextLanguage = normalized
|
|
if redisBackendTextLocalizer == nil {
|
|
localizer, err := i18n.NewLocalizer(normalized)
|
|
if err != nil {
|
|
return
|
|
}
|
|
redisBackendTextLocalizer = localizer
|
|
return
|
|
}
|
|
redisBackendTextLocalizer.SetLanguage(normalized)
|
|
}
|
|
|
|
func localizedRedisBackendText(key string, params map[string]any) string {
|
|
redisBackendTextMu.RLock()
|
|
if redisBackendTextLocalizer != nil {
|
|
text := redisBackendTextLocalizer.T(key, params)
|
|
redisBackendTextMu.RUnlock()
|
|
return text
|
|
}
|
|
redisBackendTextMu.RUnlock()
|
|
|
|
redisBackendTextMu.Lock()
|
|
defer redisBackendTextMu.Unlock()
|
|
|
|
if redisBackendTextLocalizer == nil {
|
|
localizer, err := i18n.NewLocalizer(redisBackendTextLanguage)
|
|
if err != nil {
|
|
return key
|
|
}
|
|
redisBackendTextLocalizer = localizer
|
|
}
|
|
return redisBackendTextLocalizer.T(key, params)
|
|
}
|
|
|
|
func localizedRedisBackendError(key string, params map[string]any) error {
|
|
return fmt.Errorf("%s", localizedRedisBackendText(key, params))
|
|
}
|