Skip to content

Commit 75d91d1

Browse files
committed
update
1 parent 559d99e commit 75d91d1

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

middleware/language/config.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package language
1919
import (
2020
"net/http"
2121

22+
"github.com/webx-top/echo"
2223
"github.com/webx-top/echo/param"
2324
)
2425

@@ -49,6 +50,31 @@ type Config struct {
4950
// Reload indicates whether to reload the language file each time it is modified.
5051
Reload bool
5152
fsFunc func(string) http.FileSystem
53+
kvList echo.KVList
54+
}
55+
56+
func (c *Config) Init() {
57+
c.kvList = make(echo.KVList, len(c.AllList))
58+
for index, lang := range c.AllList {
59+
extra := c.ExtraBy(lang)
60+
label := extra.String(`label`)
61+
if len(label) == 0 {
62+
label = lang
63+
}
64+
flag := extra.String(`flag`)
65+
var kv *echo.KV
66+
if len(flag) > 0 {
67+
kv = echo.NewKV(lang, flag+` `+label)
68+
} else {
69+
kv = echo.NewKV(lang, label)
70+
}
71+
kv.SetH(extra)
72+
c.kvList[index] = kv
73+
}
74+
}
75+
76+
func (c Config) KVList() echo.KVList {
77+
return c.kvList
5278
}
5379

5480
func (c *Config) SetFSFunc(fsFunc func(string) http.FileSystem) *Config {
@@ -57,13 +83,13 @@ func (c *Config) SetFSFunc(fsFunc func(string) http.FileSystem) *Config {
5783
}
5884

5985
// FSFunc returns the configured http.FileSystem function.
60-
func (c *Config) FSFunc() func(string) http.FileSystem {
86+
func (c Config) FSFunc() func(string) http.FileSystem {
6187
return c.fsFunc
6288
}
6389

6490
// ExtraBy returns the extra parameters for the specified language.
6591
// If no extra parameters exist for the language or Extra is nil, returns an empty Store.
66-
func (c *Config) ExtraBy(lang string) param.Store {
92+
func (c Config) ExtraBy(lang string) param.Store {
6793
if c.Extra == nil {
6894
return param.Store{}
6995
}
@@ -73,7 +99,7 @@ func (c *Config) ExtraBy(lang string) param.Store {
7399
return param.Store{}
74100
}
75101

76-
func (c *Config) Clone() Config {
102+
func (c Config) Clone() Config {
77103
cfg := Config{
78104
Project: c.Project,
79105
Default: c.Default,
@@ -84,12 +110,17 @@ func (c *Config) Clone() Config {
84110
MessagesPath: make([]string, len(c.MessagesPath)),
85111
Reload: c.Reload,
86112
fsFunc: c.fsFunc,
113+
kvList: make(echo.KVList, len(c.kvList)),
87114
}
88115
copy(cfg.AllList, c.AllList)
89116
copy(cfg.RulesPath, c.RulesPath)
90117
copy(cfg.MessagesPath, c.MessagesPath)
91118
for k, v := range c.Extra {
92119
cfg.Extra[k] = v.Clone()
93120
}
121+
for k, v := range c.kvList {
122+
cloned := v.Clone()
123+
cfg.kvList[k] = &cloned
124+
}
94125
return cfg
95126
}

middleware/language/language.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (a *Language) Init(c *Config) {
7676
a.Set(`en`, true)
7777
}
7878
}
79+
c.Init()
7980
a.I18n = NewI18n(c)
8081
if c.Reload {
8182
a.I18n.Monitor()

0 commit comments

Comments
 (0)