🐛 fix session bug
This commit is contained in:
@@ -10,7 +10,10 @@ func NewBundle(tag language.Tag, configs ...string) *i18n2.Bundle {
|
||||
bundle := i18n2.NewBundle(tag)
|
||||
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
|
||||
for _, file := range configs {
|
||||
bundle.LoadMessageFile(file)
|
||||
_, err := bundle.LoadMessageFile(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return bundle
|
||||
}
|
||||
|
@@ -9,13 +9,6 @@ import (
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
/*
|
||||
type Cache interface {
|
||||
GetLocalizer() (*i18n2.Localizer, bool)
|
||||
SetLocalizer(l *i18n2.Localizer)
|
||||
}
|
||||
*/
|
||||
|
||||
func getLocalizer(ctx context.Context) (*i18n2.Localizer, bool) {
|
||||
v := ctx.Value(I18nKey)
|
||||
if l, b := v.(*i18n2.Localizer); b {
|
||||
@@ -49,14 +42,3 @@ func IsHasI18n(ctx context.Context) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// func isHasI18n(ctx context.Context) bool {
|
||||
// if use, exist := ctx.Value(isUseI18n).(bool); exist {
|
||||
// return use
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
//
|
||||
// func setHasI18n(ctx context.Context, use bool) context.Context {
|
||||
// return context.WithValue(ctx, isUseI18n, use)
|
||||
// }
|
||||
|
@@ -7,37 +7,24 @@ import (
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func FormatText(ctx context.Context, msgId string, defaultText string) string {
|
||||
return FormatTextWithData(ctx, msgId, defaultText, nil)
|
||||
func FormatText(ctx context.Context, msgId string) string {
|
||||
return FormatTextWithData(ctx, msgId)
|
||||
}
|
||||
|
||||
func FormatTextWithData(ctx context.Context, msgId string, defaultText string, args map[string]interface{}) string {
|
||||
return FormatMessage(ctx, &i18n2.Message{
|
||||
ID: msgId,
|
||||
Other: defaultText,
|
||||
}, args)
|
||||
}
|
||||
|
||||
func FormatMessage(ctx context.Context, message *i18n2.Message, args map[string]interface{}) string {
|
||||
if localizer, ok := getLocalizer(ctx); ok {
|
||||
return localizer.MustLocalize(&i18n2.LocalizeConfig{
|
||||
DefaultMessage: message,
|
||||
TemplateData: args,
|
||||
})
|
||||
func FormatTextWithData(ctx context.Context, msgId string) string {
|
||||
hasI18n := IsHasI18n(ctx)
|
||||
if !hasI18n {
|
||||
return ""
|
||||
}
|
||||
return formatInternalMessage(message, args)
|
||||
}
|
||||
|
||||
func formatInternalMessage(message *i18n2.Message, args map[string]interface{}) string {
|
||||
if args == nil {
|
||||
return message.Other
|
||||
localizer, ok := getLocalizer(ctx)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
tpl := i18n2.NewMessageTemplate(message)
|
||||
msg, err := tpl.Execute("other", args, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
localizeConfig := &i18n2.LocalizeConfig{
|
||||
MessageID: msgId,
|
||||
}
|
||||
return msg
|
||||
localize := localizer.MustLocalize(localizeConfig)
|
||||
return localize
|
||||
}
|
||||
|
||||
func FetchCurrentLanguageFromCtx(ctx context.Context) (*language.Tag, bool) {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package i18n
|
||||
|
||||
const I18nKey = "i18n"
|
||||
const I18nCurrentLangKey = "lang"
|
||||
const I18nCurrentLangKey = "language"
|
||||
|
||||
var (
|
||||
defaultLangHeaderKey = "Accept-Language"
|
||||
|
@@ -5,10 +5,13 @@ import "golang.org/x/text/language"
|
||||
func MatchCurrentLanguageTag(accept string, supportTags []language.Tag) language.Tag {
|
||||
langTags, _, err := language.ParseAcceptLanguage(accept)
|
||||
if err != nil {
|
||||
langTags = []language.Tag{language.English}
|
||||
langTags = []language.Tag{language.Chinese}
|
||||
}
|
||||
var matcher = language.NewMatcher(supportTags)
|
||||
_, i, _ := matcher.Match(langTags...)
|
||||
tag := supportTags[i]
|
||||
if tag == language.Und {
|
||||
tag = language.Chinese
|
||||
}
|
||||
return tag
|
||||
}
|
||||
|
Reference in New Issue
Block a user