answer/internal/controller/lang_controller.go

73 lines
2.2 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package controller
import (
"encoding/json"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/service"
2022-09-27 17:59:05 +08:00
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/i18n"
)
type LangController struct {
translator i18n.Translator
siteInfoService *service.SiteInfoService
2022-09-27 17:59:05 +08:00
}
// NewLangController new language controller.
func NewLangController(tr i18n.Translator, siteInfoService *service.SiteInfoService) *LangController {
return &LangController{translator: tr, siteInfoService: siteInfoService}
2022-09-27 17:59:05 +08:00
}
// GetLangMapping get language config mapping
// @Summary get language config mapping
// @Description get language config mapping
// @Tags Lang
// @Param Accept-Language header string true "Accept-Language"
// @Produce json
// @Success 200 {object} handler.RespBody{}
// @Router /answer/api/v1/language/config [get]
func (u *LangController) GetLangMapping(ctx *gin.Context) {
data, _ := u.translator.Dump(handler.GetLang(ctx))
var resp map[string]any
_ = json.Unmarshal(data, &resp)
handler.HandleResponse(ctx, nil, resp)
}
// GetAdminLangOptions Get language options
2022-09-27 17:59:05 +08:00
// @Summary Get language options
// @Description Get language options
// @Tags Lang
// @Produce json
// @Success 200 {object} handler.RespBody{}
// @Router /answer/api/v1/language/options [get]
// @Router /answer/admin/api/language/options [get]
func (u *LangController) GetAdminLangOptions(ctx *gin.Context) {
handler.HandleResponse(ctx, nil, translator.LanguageOptions)
}
// GetUserLangOptions Get language options
// @Summary Get language options
// @Description Get language options
// @Tags Lang
// @Produce json
// @Success 200 {object} handler.RespBody{}
// @Router /answer/api/v1/language/options [get]
func (u *LangController) GetUserLangOptions(ctx *gin.Context) {
siteInterfaceResp, err := u.siteInfoService.GetSiteInterface(ctx)
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
options := translator.LanguageOptions
if len(siteInterfaceResp.Language) > 0 {
defaultOption := []*translator.LangOption{
2022-11-03 14:40:43 +08:00
{Label: translator.DefaultLangOption, Value: siteInterfaceResp.Language},
}
options = append(defaultOption, options...)
}
handler.HandleResponse(ctx, nil, options)
2022-09-27 17:59:05 +08:00
}