完善
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package langx
|
package langx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -24,6 +25,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置
|
||||||
func InitLangx(ops ...Option) {
|
func InitLangx(ops ...Option) {
|
||||||
for _, opt := range ops {
|
for _, opt := range ops {
|
||||||
opt(l.ops)
|
opt(l.ops)
|
||||||
@@ -57,6 +59,7 @@ func GetCode(key string) int {
|
|||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取翻译
|
||||||
func GetMsg(lang string, key string) string {
|
func GetMsg(lang string, key string) string {
|
||||||
// 找指定语言
|
// 找指定语言
|
||||||
str, ok := l.transMap[lang]
|
str, ok := l.transMap[lang]
|
||||||
@@ -78,6 +81,16 @@ func GetMsg(lang string, key string) string {
|
|||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 从ctx里面获取语言
|
||||||
|
func GetMsgCtx(ctx context.Context, key string) string {
|
||||||
|
ctxVal := ctx.Value(l.ops.ctxLangKey)
|
||||||
|
lang := l.ops.defaultLang
|
||||||
|
if ctxVal != nil {
|
||||||
|
lang = ctxVal.(string)
|
||||||
|
}
|
||||||
|
return GetMsg(lang, key)
|
||||||
|
}
|
||||||
|
|
||||||
// 拼接回复
|
// 拼接回复
|
||||||
func GetFormat(lang string, key string, arr map[string]string) string {
|
func GetFormat(lang string, key string, arr map[string]string) string {
|
||||||
str := GetMsg(lang, key)
|
str := GetMsg(lang, key)
|
||||||
@@ -87,6 +100,7 @@ func GetFormat(lang string, key string, arr map[string]string) string {
|
|||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取默认Code
|
||||||
func GetDefaultCode() int {
|
func GetDefaultCode() int {
|
||||||
return l.ops.defaultCode
|
return l.ops.defaultCode
|
||||||
}
|
}
|
||||||
|
|||||||
+11
@@ -4,6 +4,7 @@ type options struct {
|
|||||||
defaultCode int
|
defaultCode int
|
||||||
defaultLang string
|
defaultLang string
|
||||||
replaceKey string
|
replaceKey string
|
||||||
|
ctxLangKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultOptions() *options {
|
func defaultOptions() *options {
|
||||||
@@ -11,6 +12,7 @@ func defaultOptions() *options {
|
|||||||
defaultCode: 200,
|
defaultCode: 200,
|
||||||
defaultLang: "zh",
|
defaultLang: "zh",
|
||||||
replaceKey: "#%s#",
|
replaceKey: "#%s#",
|
||||||
|
ctxLangKey: "language",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,12 +24,21 @@ func SetDefaultCode(code int) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 从ctx里面获取语言的key
|
||||||
|
func SetCtxLangKey(key string) Option {
|
||||||
|
return func(o *options) {
|
||||||
|
o.ctxLangKey = key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认语言
|
||||||
func SetDefaultLanguage(lang string) Option {
|
func SetDefaultLanguage(lang string) Option {
|
||||||
return func(o *options) {
|
return func(o *options) {
|
||||||
o.defaultLang = lang
|
o.defaultLang = lang
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 替换规则 %s 为占位符
|
||||||
func SetReplaceKey(key string) Option {
|
func SetReplaceKey(key string) Option {
|
||||||
return func(o *options) {
|
return func(o *options) {
|
||||||
o.replaceKey = key
|
o.replaceKey = key
|
||||||
|
|||||||
Reference in New Issue
Block a user