Merge branch 'feat/language' into 'test'

feat: add default language option

See merge request opensource/answer!174
This commit is contained in:
aichy 2022-11-03 06:48:33 +00:00
commit b597bd43b8
2 changed files with 11 additions and 3 deletions

View File

@ -21,8 +21,13 @@ type LangOption struct {
Value string `json:"value"`
}
// LanguageOptions language
var LanguageOptions []*LangOption
// DefaultLangOption default language option. If user config the language is default, the language option is admin choose.
const DefaultLangOption = "Default"
var (
// LanguageOptions language
LanguageOptions []*LangOption
)
// NewTranslator new a translator
func NewTranslator(c *I18n) (tr i18n.Translator, err error) {
@ -49,6 +54,9 @@ func NewTranslator(c *I18n) (tr i18n.Translator, err error) {
// CheckLanguageIsValid check user input language is valid
func CheckLanguageIsValid(lang string) bool {
if lang == DefaultLangOption {
return true
}
for _, option := range LanguageOptions {
if option.Value == lang {
return true

View File

@ -64,7 +64,7 @@ func (u *LangController) GetUserLangOptions(ctx *gin.Context) {
options := translator.LanguageOptions
if len(siteInterfaceResp.Language) > 0 {
defaultOption := []*translator.LangOption{
{Label: "Default", Value: siteInterfaceResp.Language},
{Label: translator.DefaultLangOption, Value: siteInterfaceResp.Language},
}
options = append(defaultOption, options...)
}