mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-12-20 06:18:56 +08:00
refactor: Optimize i18n initialization with sync.Once for thread safety
This commit is contained in:
parent
e7d483620c
commit
83874f4c27
1 changed files with 19 additions and 12 deletions
|
|
@ -3,6 +3,7 @@ package i18n
|
|||
import (
|
||||
"embed"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/core/app/repo"
|
||||
|
|
@ -136,6 +137,7 @@ func UseI18n() gin.HandlerFunc {
|
|||
}
|
||||
|
||||
func Init() {
|
||||
initOnce.Do(func() {
|
||||
bundle = i18n.NewBundle(language.Chinese)
|
||||
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
|
||||
|
||||
|
|
@ -151,9 +153,13 @@ func Init() {
|
|||
dbLang = defaultLang
|
||||
}
|
||||
global.I18n = i18n.NewLocalizer(bundle, dbLang)
|
||||
})
|
||||
}
|
||||
|
||||
func UseI18nForCmd(lang string) {
|
||||
if bundle == nil {
|
||||
Init()
|
||||
}
|
||||
if lang == "" {
|
||||
lang = defaultLang
|
||||
}
|
||||
|
|
@ -205,6 +211,7 @@ func getLanguageFromDBInternal() string {
|
|||
}
|
||||
|
||||
var cachedDBLang atomic.Value
|
||||
var initOnce sync.Once
|
||||
|
||||
func GetLanguage() string {
|
||||
if v := cachedDBLang.Load(); v != nil {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue