提交 9fe09ba4 编写于 作者: 7 710leo

code refactor

上级 9a71c005
......@@ -58,6 +58,8 @@ func main() {
// 初始化数据库和相关数据
models.InitMySQL("rdb", "ams")
i18n.Init(config.Config.I18n)
http.Start()
endingProc()
......
......@@ -4,13 +4,16 @@ import (
"fmt"
"github.com/didi/nightingale/src/common/loggeri"
"github.com/didi/nightingale/src/toolkits/i18n"
"github.com/toolkits/pkg/file"
)
type ConfigT struct {
Logger loggeri.Config `yaml:"logger"`
HTTP httpSection `yaml:"http"`
Tokens []string `yaml:"tokens"`
Logger loggeri.Config `yaml:"logger"`
HTTP httpSection `yaml:"http"`
Tokens []string `yaml:"tokens"`
I18n i18n.I18nSection `yaml:"i18n"`
}
type httpSection struct {
......@@ -35,6 +38,14 @@ func Parse() error {
}
Config = &c
if Config.I18n.DictPath == "" {
Config.I18n.DictPath = "etc/dict.json"
}
if Config.I18n.Lang == "" {
Config.I18n.Lang = "zh"
}
fmt.Println("config.file:", ymlFile)
return nil
......
......@@ -7,13 +7,15 @@ import (
"github.com/didi/nightingale/src/common/identity"
"github.com/didi/nightingale/src/common/loggeri"
"github.com/didi/nightingale/src/toolkits/i18n"
)
type ConfigT struct {
Logger loggeri.Config `yaml:"logger"`
HTTP httpSection `yaml:"http"`
Tokens []string `yaml:"tokens"`
Output outputSection `yaml:"output"`
Logger loggeri.Config `yaml:"logger"`
HTTP httpSection `yaml:"http"`
Tokens []string `yaml:"tokens"`
Output outputSection `yaml:"output"`
I18n i18n.I18nSection `yaml:"i18n"`
}
type httpSection struct {
......@@ -43,6 +45,15 @@ func Parse() error {
}
Config = &c
if Config.I18n.DictPath == "" {
Config.I18n.DictPath = "etc/dict.json"
}
if Config.I18n.Lang == "" {
Config.I18n.Lang = "zh"
}
fmt.Println("config.file:", ymlFile)
return identity.Parse()
......
......@@ -83,7 +83,7 @@ func main() {
// 将task_host_doing表缓存到内存里,减少DB压力
timer.CacheHostDoing()
i18n.Init()
i18n.Init(config.Config.I18n)
go rpc.Start()
http.Start()
......
......@@ -5,6 +5,8 @@ import (
"fmt"
"sync"
"github.com/didi/nightingale/src/toolkits/i18n"
"github.com/spf13/viper"
"github.com/toolkits/pkg/file"
)
......@@ -26,12 +28,7 @@ type ConfYaml struct {
Notify map[string][]string `yaml:"notify"`
Link linkSection `yaml:"link"`
IndexMod string `yaml:"indexMod"`
I18n i18nSection `yaml:"i18n"`
}
type i18nSection struct {
DictPath string `yaml:"dictPath"`
Lang string `yaml:"lang"`
I18n i18n.I18nSection `yaml:"i18n"`
}
type mergeSection struct {
......
......@@ -66,7 +66,7 @@ func main() {
scache.Init()
i18n.Init(config.Get().I18n.Lang, config.Get().I18n.DictPath)
i18n.Init(config.Get().I18n)
if err := scache.CheckJudge(); err != nil {
logger.Errorf("check judge fail: %v", err)
......
......@@ -6,6 +6,7 @@ import (
"github.com/toolkits/pkg/file"
"github.com/didi/nightingale/src/common/loggeri"
"github.com/didi/nightingale/src/toolkits/i18n"
)
type ConfigT struct {
......@@ -19,6 +20,7 @@ type ConfigT struct {
RabbitMQ rabbitmqSection `yaml:"rabbitmq"`
WeChat wechatSection `yaml:"wechat"`
Captcha bool `yaml:"captcha"`
I18n i18n.I18nSection `yaml:"i18n"`
}
type wechatSection struct {
......@@ -115,6 +117,14 @@ func Parse() error {
Config = &c
fmt.Println("config.file:", ymlFile)
if Config.I18n.DictPath == "" {
Config.I18n.DictPath = "etc/dict.json"
}
if Config.I18n.Lang == "" {
Config.I18n.Lang = "zh"
}
if err = parseOps(); err != nil {
return err
}
......
......@@ -67,7 +67,7 @@ func main() {
// 初始化 redis 用来发送邮件短信等
redisc.InitRedis()
cron.InitWorker()
i18n.Init()
i18n.Init(config.Config.I18n)
// 初始化 rabbitmq 处理部分异步逻辑
rabbitmq.Init()
......
......@@ -11,6 +11,42 @@ import (
"github.com/toolkits/pkg/file"
)
type I18nSection struct {
DictPath string `yaml:"dictPath"`
Lang string `yaml:"lang"`
}
// Init will init i18n support via input language.
func Init(config ...I18nSection) {
l := "zh"
fpath := "etc/dict.json"
if len(config) > 0 {
l = config[0].Lang
fpath = config[0].DictPath
}
lang := language.Chinese
switch l {
case "en":
lang = language.English
case "zh":
lang = language.Chinese
}
tag, _, _ := supported.Match(lang)
switch tag {
case language.AmericanEnglish, language.English:
initEnUS(lang)
case language.SimplifiedChinese, language.Chinese:
initZhCN(lang, fpath)
default:
initZhCN(lang, fpath)
}
p = message.NewPrinter(lang)
}
func initEnUS(tag language.Tag) {
}
......@@ -84,37 +120,6 @@ var supported = newMatcher([]language.Tag{
language.Chinese,
})
// Init will init i18n support via input language.
func Init(arr ...string) {
l := "zh"
fpath := "etc/dict.json"
if len(arr) == 2 {
l = arr[0]
fpath = arr[1]
}
lang := language.Chinese
switch l {
case "en":
lang = language.English
case "zh":
lang = language.Chinese
}
tag, _, _ := supported.Match(lang)
switch tag {
case language.AmericanEnglish, language.English:
initEnUS(lang)
case language.SimplifiedChinese, language.Chinese:
initZhCN(lang, fpath)
default:
initZhCN(lang, fpath)
}
p = message.NewPrinter(lang)
}
// Fprintf is like fmt.Fprintf, but using language-specific formatting.
func Fprintf(w io.Writer, key message.Reference, a ...interface{}) (n int, err error) {
return p.Fprintf(w, key, a...)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册