Skip to content

Commit 0065e35

Browse files
liruohrhLinkinStars
authored andcommitted
fix: get right lang
1 parent bf2127d commit 0065e35

File tree

15 files changed

+44
-57
lines changed

15 files changed

+44
-57
lines changed

internal/base/handler/handler.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"errors"
2424
"net/http"
2525

26-
"github.com/apache/answer/internal/base/constant"
2726
"github.com/apache/answer/internal/base/reason"
2827
"github.com/apache/answer/internal/base/validator"
2928
"github.com/gin-gonic/gin"
@@ -33,7 +32,7 @@ import (
3332

3433
// HandleResponse Handle response body
3534
func HandleResponse(ctx *gin.Context, err error, data any) {
36-
lang := GetLang(ctx)
35+
lang := GetLangByCtx(ctx)
3736
// no error
3837
if err == nil {
3938
ctx.JSON(http.StatusOK, NewRespBodyData(http.StatusOK, reason.Success, data).TrMsg(lang))
@@ -63,8 +62,7 @@ func HandleResponse(ctx *gin.Context, err error, data any) {
6362

6463
// BindAndCheck bind request and check
6564
func BindAndCheck(ctx *gin.Context, data any) bool {
66-
lang := GetLang(ctx)
67-
ctx.Set(constant.AcceptLanguageFlag, lang)
65+
lang := GetLangByCtx(ctx)
6866
if err := ctx.ShouldBind(data); err != nil {
6967
log.Errorf("http_handle BindAndCheck fail, %s", err.Error())
7068
HandleResponse(ctx, myErrors.New(http.StatusBadRequest, reason.RequestFormatError), nil)
@@ -81,7 +79,7 @@ func BindAndCheck(ctx *gin.Context, data any) bool {
8179

8280
// BindAndCheckReturnErr bind request and check
8381
func BindAndCheckReturnErr(ctx *gin.Context, data any) (errFields []*validator.FormErrorField) {
84-
lang := GetLang(ctx)
82+
lang := GetLangByCtx(ctx)
8583
if err := ctx.ShouldBind(data); err != nil {
8684
log.Errorf("http_handle BindAndCheck fail, %s", err.Error())
8785
HandleResponse(ctx, myErrors.New(http.StatusBadRequest, reason.RequestFormatError), nil)

internal/base/handler/lang.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,9 @@ import (
2323
"context"
2424

2525
"github.com/apache/answer/internal/base/constant"
26-
"github.com/gin-gonic/gin"
2726
"github.com/segmentfault/pacman/i18n"
2827
)
2928

30-
// GetLang get language from header
31-
func GetLang(ctx *gin.Context) i18n.Language {
32-
acceptLanguage := ctx.GetHeader(constant.AcceptLanguageFlag)
33-
if len(acceptLanguage) == 0 {
34-
return i18n.DefaultLanguage
35-
}
36-
return i18n.Language(acceptLanguage)
37-
}
38-
3929
// GetLangByCtx get language from header
4030
func GetLangByCtx(ctx context.Context) i18n.Language {
4131
acceptLanguage, ok := ctx.Value(constant.AcceptLanguageContextKey).(i18n.Language)

internal/base/middleware/accept_language.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strings"
2424

2525
"github.com/apache/answer/internal/base/constant"
26-
"github.com/apache/answer/internal/base/handler"
2726
"github.com/apache/answer/internal/base/translator"
2827
"github.com/gin-gonic/gin"
2928
"github.com/segmentfault/pacman/i18n"
@@ -33,8 +32,8 @@ import (
3332
// ExtractAndSetAcceptLanguage extract accept language from header and set to context
3433
func ExtractAndSetAcceptLanguage(ctx *gin.Context) {
3534
// The language of our front-end configuration, like en_US
36-
lang := handler.GetLang(ctx)
37-
tag, _, err := language.ParseAcceptLanguage(string(lang))
35+
acceptLanguage := ctx.GetHeader(constant.AcceptLanguageFlag)
36+
tag, _, err := language.ParseAcceptLanguage(acceptLanguage)
3837
if err != nil || len(tag) == 0 {
3938
ctx.Set(constant.AcceptLanguageFlag, i18n.LanguageEnglish)
4039
return

internal/controller/answer_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (ac *AnswerController) RemoveAnswer(ctx *gin.Context) {
8989
if !captchaPass {
9090
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
9191
ErrorField: "captcha_code",
92-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
92+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
9393
})
9494
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
9595
return
@@ -225,7 +225,7 @@ func (ac *AnswerController) AddAnswer(ctx *gin.Context) {
225225
if !captchaPass {
226226
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
227227
ErrorField: "captcha_code",
228-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
228+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
229229
})
230230
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
231231
return
@@ -325,7 +325,7 @@ func (ac *AnswerController) UpdateAnswer(ctx *gin.Context) {
325325
if !captchaPass {
326326
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
327327
ErrorField: "captcha_code",
328-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
328+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
329329
})
330330
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
331331
return

internal/controller/comment_controller.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (cc *CommentController) AddComment(ctx *gin.Context) {
106106
if !captchaPass {
107107
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
108108
ErrorField: "captcha_code",
109-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
109+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
110110
})
111111
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
112112
return
@@ -154,7 +154,7 @@ func (cc *CommentController) RemoveComment(ctx *gin.Context) {
154154
if !captchaPass {
155155
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
156156
ErrorField: "captcha_code",
157-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
157+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
158158
})
159159
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
160160
return
@@ -215,7 +215,7 @@ func (cc *CommentController) UpdateComment(ctx *gin.Context) {
215215
if !captchaPass {
216216
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
217217
ErrorField: "captcha_code",
218-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
218+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
219219
})
220220
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
221221
return

internal/controller/lang_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewLangController(tr i18n.Translator, siteInfoService siteinfo_common.SiteI
4848
// @Success 200 {object} handler.RespBody{}
4949
// @Router /answer/api/v1/language/config [get]
5050
func (u *LangController) GetLangMapping(ctx *gin.Context) {
51-
data, _ := u.translator.Dump(handler.GetLang(ctx))
51+
data, _ := u.translator.Dump(handler.GetLangByCtx(ctx))
5252
var resp map[string]any
5353
_ = json.Unmarshal(data, &resp)
5454
handler.HandleResponse(ctx, nil, resp)

internal/controller/question_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (qc *QuestionController) RemoveQuestion(ctx *gin.Context) {
9494
if !captchaPass {
9595
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
9696
ErrorField: "captcha_code",
97-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
97+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
9898
})
9999
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
100100
return
@@ -419,7 +419,7 @@ func (qc *QuestionController) AddQuestion(ctx *gin.Context) {
419419
if !captchaPass {
420420
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
421421
ErrorField: "captcha_code",
422-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
422+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
423423
})
424424
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
425425
return
@@ -445,7 +445,7 @@ func (qc *QuestionController) AddQuestion(ctx *gin.Context) {
445445
return
446446
}
447447
if !req.CanAddTag && hasNewTag {
448-
lang := handler.GetLang(ctx)
448+
lang := handler.GetLangByCtx(ctx)
449449
msg := translator.TrWithData(lang, reason.NoEnoughRankToOperate, &schema.PermissionTrTplData{Rank: requireRanks[6]})
450450
handler.HandleResponse(ctx, errors.Forbidden(reason.NoEnoughRankToOperate).WithMsg(msg), nil)
451451
return
@@ -524,7 +524,7 @@ func (qc *QuestionController) AddQuestionByAnswer(ctx *gin.Context) {
524524
if !captchaPass {
525525
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
526526
ErrorField: "captcha_code",
527-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
527+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
528528
})
529529
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
530530
return
@@ -646,7 +646,7 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
646646
if !captchaPass {
647647
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
648648
ErrorField: "captcha_code",
649-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
649+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
650650
})
651651
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
652652
return
@@ -681,7 +681,7 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
681681
return
682682
}
683683
if !req.CanAddTag && hasNewTag {
684-
lang := handler.GetLang(ctx)
684+
lang := handler.GetLangByCtx(ctx)
685685
msg := translator.TrWithData(lang, reason.NoEnoughRankToOperate, &schema.PermissionTrTplData{Rank: requireRanks[4]})
686686
handler.HandleResponse(ctx, errors.Forbidden(reason.NoEnoughRankToOperate).WithMsg(msg), nil)
687687
return
@@ -765,7 +765,7 @@ func (qc *QuestionController) UpdateQuestionInviteUser(ctx *gin.Context) {
765765
if !captchaPass {
766766
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
767767
ErrorField: "captcha_code",
768-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
768+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
769769
})
770770
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
771771
return

internal/controller/report_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (rc *ReportController) AddReport(ctx *gin.Context) {
7979
if !captchaPass {
8080
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
8181
ErrorField: "captcha_code",
82-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
82+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
8383
})
8484
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
8585
return

internal/controller/search_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (sc *SearchController) Search(ctx *gin.Context) {
7878
if !captchaPass {
7979
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
8080
ErrorField: "captcha_code",
81-
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
81+
ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
8282
})
8383
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
8484
return

internal/controller/template_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (tc *TemplateController) QuestionList(ctx *gin.Context) {
206206
UrlUseTitle := siteInfo.SiteSeo.Permalink == constant.PermalinkQuestionIDAndTitle ||
207207
siteInfo.SiteSeo.Permalink == constant.PermalinkQuestionIDAndTitleByShortID
208208

209-
siteInfo.Title = fmt.Sprintf("%s - %s", translator.Tr(handler.GetLang(ctx), constant.QuestionsTitleTrKey), siteInfo.General.Name)
209+
siteInfo.Title = fmt.Sprintf("%s - %s", translator.Tr(handler.GetLangByCtx(ctx), constant.QuestionsTitleTrKey), siteInfo.General.Name)
210210
tc.html(ctx, http.StatusOK, "question.html", siteInfo, gin.H{
211211
"data": data,
212212
"useTitle": UrlUseTitle,
@@ -461,7 +461,7 @@ func (tc *TemplateController) TagList(ctx *gin.Context) {
461461
if req.Page > 1 {
462462
siteInfo.Canonical = fmt.Sprintf("%s/tags?page=%d", siteInfo.General.SiteUrl, req.Page)
463463
}
464-
siteInfo.Title = fmt.Sprintf("%s - %s", translator.Tr(handler.GetLang(ctx), constant.TagsListTitleTrKey), siteInfo.General.Name)
464+
siteInfo.Title = fmt.Sprintf("%s - %s", translator.Tr(handler.GetLangByCtx(ctx), constant.TagsListTitleTrKey), siteInfo.General.Name)
465465
tc.html(ctx, http.StatusOK, "tags.html", siteInfo, gin.H{
466466
"page": page,
467467
"data": data,
@@ -492,14 +492,14 @@ func (tc *TemplateController) TagInfo(ctx *gin.Context) {
492492
}
493493
siteInfo.Description = htmltext.FetchExcerpt(tagInfo.ParsedText, "...", 240)
494494
if len(tagInfo.ParsedText) == 0 {
495-
siteInfo.Description = translator.Tr(handler.GetLang(ctx), constant.TagHasNoDescription)
495+
siteInfo.Description = translator.Tr(handler.GetLangByCtx(ctx), constant.TagHasNoDescription)
496496
}
497497
siteInfo.Keywords = tagInfo.DisplayName
498498

499499
UrlUseTitle := siteInfo.SiteSeo.Permalink == constant.PermalinkQuestionIDAndTitle ||
500500
siteInfo.SiteSeo.Permalink == constant.PermalinkQuestionIDAndTitleByShortID
501501

502-
siteInfo.Title = fmt.Sprintf("'%s' %s - %s", tagInfo.DisplayName, translator.Tr(handler.GetLang(ctx), constant.QuestionsTitleTrKey), siteInfo.General.Name)
502+
siteInfo.Title = fmt.Sprintf("'%s' %s - %s", tagInfo.DisplayName, translator.Tr(handler.GetLangByCtx(ctx), constant.QuestionsTitleTrKey), siteInfo.General.Name)
503503
tc.html(ctx, http.StatusOK, "tag-detail.html", siteInfo, gin.H{
504504
"tag": tagInfo,
505505
"questionList": questionList,
@@ -597,7 +597,7 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI
597597
data["title"] = siteInfo.General.Name
598598
}
599599
data["description"] = siteInfo.Description
600-
data["language"] = handler.GetLang(ctx)
600+
data["language"] = handler.GetLangByCtx(ctx)
601601
data["timezone"] = siteInfo.Interface.TimeZone
602602
language := strings.ReplaceAll(siteInfo.Interface.Language, "_", "-")
603603
data["lang"] = language

0 commit comments

Comments
 (0)