diff --git a/internal/base/handler/lang.go b/internal/base/handler/lang.go index a6b2122c..4f4a0763 100644 --- a/internal/base/handler/lang.go +++ b/internal/base/handler/lang.go @@ -1,6 +1,8 @@ package handler import ( + "context" + "github.com/answerdev/answer/internal/base/constant" "github.com/gin-gonic/gin" "github.com/segmentfault/pacman/i18n" @@ -18,3 +20,12 @@ func GetLang(ctx *gin.Context) i18n.Language { return i18n.DefaultLang } } + +// GetLangByCtx get language from header +func GetLangByCtx(ctx context.Context) i18n.Language { + acceptLanguage, ok := ctx.Value(constant.AcceptLanguageFlag).(i18n.Language) + if ok { + return acceptLanguage + } + return i18n.DefaultLang +} diff --git a/internal/service/question_service.go b/internal/service/question_service.go index 4478d37f..536f8c41 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -7,6 +7,7 @@ import ( "time" "github.com/answerdev/answer/internal/base/constant" + "github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/base/translator" "github.com/answerdev/answer/internal/base/validator" @@ -129,7 +130,7 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question errorlist := make([]*validator.FormErrorField, 0) errorlist = append(errorlist, &validator.FormErrorField{ ErrorField: "tags", - ErrorMsg: reason.RecommendTagEnter, + ErrorMsg: translator.GlobalTrans.Tr(handler.GetLangByCtx(ctx), reason.RecommendTagEnter), }) err = errors.BadRequest(reason.RecommendTagEnter) return errorlist, err @@ -350,7 +351,7 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest errorlist := make([]*validator.FormErrorField, 0) errorlist = append(errorlist, &validator.FormErrorField{ ErrorField: "tags", - ErrorMsg: reason.RecommendTagEnter, + ErrorMsg: translator.GlobalTrans.Tr(handler.GetLangByCtx(ctx), reason.RecommendTagEnter), }) err = errors.BadRequest(reason.RecommendTagEnter) return errorlist, err