🐛 fix session bug

This commit is contained in:
landaiqing
2024-11-17 20:02:59 +08:00
parent 34c4690f80
commit 78a162a19a
72 changed files with 1304 additions and 453 deletions

View File

@@ -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
}

View File

@@ -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)
// }

View File

@@ -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) {

View File

@@ -1,7 +1,7 @@
package i18n
const I18nKey = "i18n"
const I18nCurrentLangKey = "lang"
const I18nCurrentLangKey = "language"
var (
defaultLangHeaderKey = "Accept-Language"

View File

@@ -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
}