2022-09-27 17:59:05 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/handler"
|
2022-11-02 15:25:27 +08:00
|
|
|
"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 {
|
2022-11-02 15:25:27 +08:00
|
|
|
translator i18n.Translator
|
|
|
|
siteInfoService *service.SiteInfoService
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLangController new language controller.
|
2022-11-02 15:25:27 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-02 15:25:27 +08:00
|
|
|
// 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]
|
2022-11-02 15:25:27 +08:00
|
|
|
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},
|
2022-11-02 15:25:27 +08:00
|
|
|
}
|
|
|
|
options = append(defaultOption, options...)
|
|
|
|
}
|
|
|
|
handler.HandleResponse(ctx, nil, options)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|