2024-06-30 11:52:34 +08:00
|
|
|
package langx
|
|
|
|
|
|
|
|
|
|
type options struct {
|
|
|
|
|
defaultCode int
|
|
|
|
|
defaultLang string
|
|
|
|
|
replaceKey string
|
2024-07-09 20:05:53 +08:00
|
|
|
ctxLangKey string
|
2024-06-30 11:52:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func defaultOptions() *options {
|
|
|
|
|
return &options{
|
|
|
|
|
defaultCode: 200,
|
|
|
|
|
defaultLang: "zh",
|
|
|
|
|
replaceKey: "#%s#",
|
2024-07-09 20:05:53 +08:00
|
|
|
ctxLangKey: "language",
|
2024-06-30 11:52:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Option func(*options)
|
|
|
|
|
|
|
|
|
|
func SetDefaultCode(code int) Option {
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
o.defaultCode = code
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 20:05:53 +08:00
|
|
|
// 从ctx里面获取语言的key
|
|
|
|
|
func SetCtxLangKey(key string) Option {
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
o.ctxLangKey = key
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 默认语言
|
2024-06-30 11:52:34 +08:00
|
|
|
func SetDefaultLanguage(lang string) Option {
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
o.defaultLang = lang
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 20:05:53 +08:00
|
|
|
// 替换规则 %s 为占位符
|
2024-06-30 11:52:34 +08:00
|
|
|
func SetReplaceKey(key string) Option {
|
|
|
|
|
return func(o *options) {
|
|
|
|
|
o.replaceKey = key
|
|
|
|
|
}
|
|
|
|
|
}
|