nezha/service/singleton/l10n.go

35 lines
836 B
Go
Raw Normal View History

2022-04-27 11:51:45 -04:00
package singleton
import (
2022-04-29 12:48:39 -04:00
"log"
2022-04-27 11:51:45 -04:00
"github.com/BurntSushi/toml"
2022-04-29 12:48:39 -04:00
"github.com/naiba/nezha/pkg/utils"
2022-04-27 11:51:45 -04:00
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
var Localizer *i18n.Localizer
func InitLocalizer() {
bundle := i18n.NewBundle(language.Chinese)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
2022-04-29 12:48:39 -04:00
userCustomLanguageFile := "resource/l10n/" + Conf.Language + ".toml"
if exists, err := utils.PathExists(userCustomLanguageFile); !exists {
log.Println("NEZHA>> language file not found:", userCustomLanguageFile, err)
Conf.Language = "zh-CN"
} else {
_, err := bundle.LoadMessageFile(userCustomLanguageFile)
if err != nil {
panic(err)
}
2022-04-27 22:17:38 -04:00
}
2022-04-29 12:48:39 -04:00
if _, err := bundle.LoadMessageFile("resource/l10n/zh-CN.toml"); err != nil {
2022-04-27 22:17:38 -04:00
panic(err)
}
Localizer = i18n.NewLocalizer(bundle, Conf.Language)
2022-04-27 11:51:45 -04:00
}