Skip to content

Commit 0a93dbe

Browse files
authored
feat: i18n国际化语言支持完全可配置,并新增俄语支持 (#987)
1 parent 84b947d commit 0a93dbe

File tree

7 files changed

+633
-208
lines changed

7 files changed

+633
-208
lines changed

commands/command.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,43 @@ func RegisterFunction() {
319319
logs.Error("注册函数 i18n 出错 ->", err)
320320
os.Exit(-1)
321321
}
322-
langs := strings.Split("en-us|zh-cn", "|")
323-
for _, lang := range langs {
322+
323+
i18nList, err := web.AppConfig.String("i18n_list")
324+
if err != nil {
325+
logs.Error("error : failed to read i18n_list config ->", err)
326+
i18nList = ""
327+
}
328+
if i18nList == "" { // 之所以分开判断是因为读取出的配置也可能是空串
329+
logs.Error("error : config `i18n_list` is empty, please add config item like format: `i18n_list=zh-CN:简体中文|en-US:English`")
330+
i18nList = "zh-cn:简体中文|en-us:English|ru-ru:Русский" // 没有配置时给个默认配置,避免啥语言都没有
331+
}
332+
333+
langs := strings.Split(i18nList, "|")
334+
i18nMap := make(map[string]string)
335+
for _, langItem := range langs {
336+
langItemSplit := strings.Split(langItem, ":")
337+
if len(langItemSplit) < 2 {
338+
logs.Error("error: language config value `" + langItem + "` for `i18n_list` format error")
339+
continue
340+
}
341+
lang := langItemSplit[0]
342+
i18nMap[lang] = langItemSplit[1]
324343
if err := i18n.SetMessage(lang, "conf/lang/"+lang+".ini"); err != nil {
325344
logs.Error("Fail to set message file: " + err.Error())
326345
return
327346
}
328347
}
348+
349+
i18nMapBytes, err := json.Marshal(i18nMap)
350+
if err != nil {
351+
logs.Error("error: Fail to marshal i18n map, " + err.Error())
352+
i18nMapBytes = []byte("{}")
353+
}
354+
355+
err = web.AppConfig.Set("i18n_map", string(i18nMapBytes))
356+
if err != nil {
357+
logs.Error("error: Fail to set i18n_map, " + err.Error())
358+
}
329359
}
330360

331361
// 解析命令

conf/app.conf.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,5 @@ workweixin_agentid="${MINDOC_WORKWEIXIN_AGENTID}"
244244
workweixin_secret="${MINDOC_WORKWEIXIN_SECRET}"
245245

246246
# i18n config
247+
i18n_list=zh-cn:简体中文|en-us:English|ru-ru:Русский
247248
default_lang="zh-cn"

conf/lang/ru-ru.ini

Lines changed: 583 additions & 0 deletions
Large diffs are not rendered by default.

controllers/ManagerController.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,19 @@ func (c *ManagerController) Setting() {
483483
for _, item := range options {
484484
c.Data[item.OptionName] = item.OptionValue
485485
}
486+
487+
i18nMapStrs, err := web.AppConfig.String("i18n_map")
488+
if err != nil {
489+
logs.Error("web.AppConfig `i18n_map` not found")
490+
i18nMapStrs = "{}"
491+
}
492+
var i18nMap map[string]string
493+
err = json.Unmarshal([]byte(i18nMapStrs), &i18nMap)
494+
if err != nil {
495+
logs.Error("json `i18nList` Unmarshal fail")
496+
i18nMap = make(map[string]string)
497+
}
498+
c.Data["i18n_map"] = i18nMap
486499
}
487500

488501
// Transfer 转让项目.

go.mod

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,58 +22,32 @@ require (
2222
github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e // indirect
2323
github.com/Unknwon/goconfig v1.0.0 // indirect
2424
github.com/andybalholm/cascadia v1.3.1 // indirect
25-
github.com/astaxie/beego v1.12.1 // indirect
26-
github.com/beego/bee v1.12.3 // indirect
2725
github.com/beorn7/perks v1.0.1 // indirect
2826
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d // indirect
2927
github.com/cespare/xxhash/v2 v2.1.2 // indirect
30-
github.com/cosiner/argv v0.1.0 // indirect
31-
github.com/davecgh/go-spew v1.1.1 // indirect
32-
github.com/flosch/pongo2 v0.0.0-20200529170236-5abacdfa4915 // indirect
33-
github.com/fsnotify/fsnotify v1.4.9 // indirect
34-
github.com/gadelkareem/delve v1.4.2-0.20200619175259-dcd01330766f // indirect
3528
github.com/go-asn1-ber/asn1-ber v1.5.4 // indirect
3629
github.com/go-redis/redis/v7 v7.4.1 // indirect
3730
github.com/go-sql-driver/mysql v1.6.0 // indirect
3831
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
3932
github.com/golang/protobuf v1.5.2 // indirect
4033
github.com/gomodule/redigo v2.0.0+incompatible // indirect
41-
github.com/gorilla/websocket v1.4.2 // indirect
4234
github.com/hashicorp/golang-lru v0.5.4 // indirect
43-
github.com/hashicorp/hcl v1.0.0 // indirect
44-
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
45-
github.com/magiconair/properties v1.8.1 // indirect
46-
github.com/mattn/go-colorable v0.0.9 // indirect
47-
github.com/mattn/go-isatty v0.0.3 // indirect
4835
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
4936
github.com/mitchellh/mapstructure v1.5.0 // indirect
50-
github.com/pelletier/go-toml v1.9.2 // indirect
51-
github.com/peterh/liner v0.0.0-20170317030525-88609521dc4b // indirect
5237
github.com/pkg/errors v0.9.1 // indirect
5338
github.com/prometheus/client_golang v1.13.0 // indirect
5439
github.com/prometheus/client_model v0.2.0 // indirect
5540
github.com/prometheus/common v0.37.0 // indirect
5641
github.com/prometheus/procfs v0.8.0 // indirect
5742
github.com/rivo/uniseg v0.3.4 // indirect
5843
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
59-
github.com/sirupsen/logrus v1.6.0 // indirect
60-
github.com/smartwalle/pongo2render v1.0.1 // indirect
6144
github.com/smartystreets/goconvey v1.7.2 // indirect
62-
github.com/spf13/afero v1.1.2 // indirect
63-
github.com/spf13/cast v1.3.0 // indirect
64-
github.com/spf13/jwalterweatherman v1.0.0 // indirect
65-
github.com/spf13/pflag v1.0.3 // indirect
66-
github.com/spf13/viper v1.7.0 // indirect
67-
github.com/subosito/gotenv v1.2.0 // indirect
68-
go.starlark.net v0.0.0-20190702223751-32f345186213 // indirect
69-
golang.org/x/arch v0.0.0-20190927153633-4e8777c89be4 // indirect
7045
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect
7146
golang.org/x/image v0.5.0 // indirect
7247
golang.org/x/net v0.7.0 // indirect
7348
golang.org/x/sys v0.5.0 // indirect
7449
golang.org/x/text v0.7.0 // indirect
7550
google.golang.org/protobuf v1.28.1 // indirect
76-
gopkg.in/ini.v1 v1.51.0 // indirect
7751
gopkg.in/yaml.v2 v2.4.0 // indirect
7852
gopkg.in/yaml.v3 v3.0.1 // indirect
7953
)

0 commit comments

Comments
 (0)