|
|
|
|
package i18n
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"git.noahlan.cn/noahlan/ntool/nlog"
|
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var atTranslator *Translator
|
|
|
|
|
|
|
|
|
|
func getTranslator() *Translator {
|
|
|
|
|
if atTranslator == nil {
|
|
|
|
|
atTranslator = NewTranslator()
|
|
|
|
|
}
|
|
|
|
|
return atTranslator
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InitWithConfig 通过配置文件初始化I18N
|
|
|
|
|
func InitWithConfig(c Config) {
|
|
|
|
|
Init(
|
|
|
|
|
WithDefaultLanguage(c.Default),
|
|
|
|
|
WithRootPath(c.RootPath),
|
|
|
|
|
WithSortedParameterPrefix(c.SortedParameterPrefix),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Init 初始化I18N
|
|
|
|
|
func Init(opts ...Option) {
|
|
|
|
|
atTranslator = NewTranslator(opts...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ResetOptions 重置Option
|
|
|
|
|
func ResetOptions(opts ...Option) {
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
opt(getTranslator())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithDefaultLanguage 设置默认语言
|
|
|
|
|
func WithDefaultLanguage(lng string) Option {
|
|
|
|
|
return func(translator *Translator) {
|
|
|
|
|
tag, err := language.Parse(lng)
|
|
|
|
|
if err != nil {
|
|
|
|
|
nlog.Errorf("parse lang err: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
translator.DefaultLanguage = tag
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithRootPath 设置监听根目录
|
|
|
|
|
func WithRootPath(rootPath string) Option {
|
|
|
|
|
return func(translator *Translator) {
|
|
|
|
|
translator.RootPath = rootPath
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithFileLoader 设置文件加载器,一般使用默认值即可
|
|
|
|
|
func WithFileLoader(loader Loader) Option {
|
|
|
|
|
return func(translator *Translator) {
|
|
|
|
|
translator.FileLoader = loader
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithTagGetter 设置动态语言Tag获取器
|
|
|
|
|
func WithTagGetter(getter TagGetter) Option {
|
|
|
|
|
return func(translator *Translator) {
|
|
|
|
|
translator.TagGetter = getter
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithLocalizeConfigGetter 设置TransAny的LocalizeConfig获取器,默认会处理string和LocalizeConfig类型
|
|
|
|
|
func WithLocalizeConfigGetter(getter LocalizeConfigGetter) Option {
|
|
|
|
|
return func(translator *Translator) {
|
|
|
|
|
translator.LocalizeConfigGetter = getter
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithSortedParameterPrefix 设置自排序参数前缀,默认为'p'
|
|
|
|
|
func WithSortedParameterPrefix(prefix string) Option {
|
|
|
|
|
return func(translator *Translator) {
|
|
|
|
|
translator.SortedParameterPrefix = prefix
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConfigLanguages 动态配置acceptLanguages
|
|
|
|
|
func ConfigLanguages(acceptLanguages ...string) {
|
|
|
|
|
getTranslator().configLocalizer(acceptLanguages...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Trans by message id
|
|
|
|
|
func Trans(ctx context.Context, messageID string) string {
|
|
|
|
|
return getTranslator().Trans(ctx, messageID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TransWithParam by sorted string parameters
|
|
|
|
|
// params can be string or map[string]any
|
|
|
|
|
// all params must be same.
|
|
|
|
|
func TransWithParam(ctx context.Context, messageID string, params ...any) string {
|
|
|
|
|
if len(params) == 0 {
|
|
|
|
|
return messageID
|
|
|
|
|
}
|
|
|
|
|
p := &MessageWithParameter{MessageID: messageID}
|
|
|
|
|
switch t := params[0].(type) {
|
|
|
|
|
case map[string]any:
|
|
|
|
|
p.ParamsWithName = t
|
|
|
|
|
default:
|
|
|
|
|
p.Params = params
|
|
|
|
|
}
|
|
|
|
|
msg, err := getTranslator().TransAny(ctx, p)
|
|
|
|
|
if err != nil || msg == "" {
|
|
|
|
|
return messageID
|
|
|
|
|
}
|
|
|
|
|
return msg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TransAny by any parameter
|
|
|
|
|
/**
|
|
|
|
|
param is one of these type: messageID, *i18n.LocalizeConfig
|
|
|
|
|
Example:
|
|
|
|
|
MustGetMessage("hello") // messageID is hello
|
|
|
|
|
MustGetMessage(&i18n.LocalizeConfig{
|
|
|
|
|
MessageID: "welcomeWithName",
|
|
|
|
|
TemplateData: map[string]string{
|
|
|
|
|
"name": context.Param("name"),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
*/
|
|
|
|
|
func TransAny(ctx context.Context, param any) (string, error) {
|
|
|
|
|
return getTranslator().TransAny(ctx, param)
|
|
|
|
|
}
|