A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(i18n): Add more supported languages.
LinkinStare395de3099a4
diff --git a/go.mod b/go.modindex 78fd6031..fd809329 100644--- a/go.mod+++ b/go.mod@@ -27,10 +27,10 @@ require (github.com/mojocn/base64Captcha v1.3.5github.com/ory/dockertest/v3 v3.9.1github.com/robfig/cron/v3 v3.0.1- github.com/segmentfault/pacman v1.0.1- github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221018072427-a15dd1434e05+ github.com/segmentfault/pacman v1.0.2+ github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05- github.com/segmentfault/pacman/contrib/i18n v0.0.0-20221207032920-3662d1e32068+ github.com/segmentfault/pacman/contrib/i18n v0.0.0-20221219081300-f734f4a16aa0github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20221018072427-a15dd1434e05github.com/segmentfault/pacman/contrib/server/http v0.0.0-20221018072427-a15dd1434e05github.com/spf13/cobra v1.6.1diff --git a/go.sum b/go.sumindex dec4922a..4d15461e 100644--- a/go.sum+++ b/go.sum@@ -599,14 +599,14 @@ github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=-github.com/segmentfault/pacman v1.0.1 h1:GFdvPtNxvVVjnDM4ty02D/+4unHwG9PmjcOZSc2wRXE=-github.com/segmentfault/pacman v1.0.1/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=-github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221018072427-a15dd1434e05 h1:rXsXgC/HR7m4V425l9pDBW/qxJv6zCh6pEvvO1ZCNsI=-github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221018072427-a15dd1434e05/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=+github.com/segmentfault/pacman v1.0.2 h1:tXWkEzePiSVQXYwFH3tOuxC1/DJ5ISi35F93lKNGs3o=+github.com/segmentfault/pacman v1.0.2/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=+github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0 h1:4x0qG7H2M3qH7Yo2BhGrVlji1iTmRAWgINY/JyENeHs=+github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05 h1:BlqTgc3/MYKG6vMI2MI+6o+7P4Gy5PXlawu185wPXAk=github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05/go.mod h1:prPjFam7MyZ5b3S9dcDOt2tMPz6kf7C9c243s9zSwPY=-github.com/segmentfault/pacman/contrib/i18n v0.0.0-20221207032920-3662d1e32068 h1:ln/qgrC62e7/XHGPiikWFV4dyYgCaWeZYkmSGqrHZp4=-github.com/segmentfault/pacman/contrib/i18n v0.0.0-20221207032920-3662d1e32068/go.mod h1:7QcRmnV7OYq4hNOOCWXT5HXnN/u756JUsqIW0Bw8n9E=+github.com/segmentfault/pacman/contrib/i18n v0.0.0-20221219081300-f734f4a16aa0 h1:zaAwBSpwUVrV2BBs1f1hfkv0rY/KdZLyKK8U9NKiurI=+github.com/segmentfault/pacman/contrib/i18n v0.0.0-20221219081300-f734f4a16aa0/go.mod h1:7QcRmnV7OYq4hNOOCWXT5HXnN/u756JUsqIW0Bw8n9E=github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20221018072427-a15dd1434e05 h1:jcGZU2juv0L3eFEkuZYV14ESLUlWfGMWnP0mjOfrSZc=github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20221018072427-a15dd1434e05/go.mod h1:L4GqtXLoR73obTYqUQIzfkm8NG8pvZafxFb6KZFSSHk=github.com/segmentfault/pacman/contrib/server/http v0.0.0-20221018072427-a15dd1434e05 h1:91is1nKNbfTOl8CvMYiFgg4c5Vmol+5mVmMV/jDXD+A=diff --git a/i18n/i18n.yaml b/i18n/i18n.yamlindex 13a6c522..bcbfaa9c 100644--- a/i18n/i18n.yaml+++ b/i18n/i18n.yaml@@ -2,5 +2,25 @@language_options:- label: "简体中文(CN)"value: "zh_CN"+ - label: "繁體中文(CN)"+ value: "zh_TW"- label: "English(US)"value: "en_US"+ - label: "Deutsch(DE)"+ value: "de_DE"+ - label: "Español(ES)"+ value: "es_ES"+ - label: "Français(FR)"+ value: "fr_FR"+ - label: "Italiano(IT)"+ value: "it_IT"+ - label: "日本語(JA)"+ value: "ja_JP"+ - label: "한국어(KO)"+ value: "ko_KR"+ - label: "Português(PT)"+ value: "pt_PT"+ - label: "Русский(RU)"+ value: "ru_RU"+ - label: "Tiếng Việt(VI)"+ value: "vi_VN"diff --git a/internal/base/handler/lang.go b/internal/base/handler/lang.goindex 4f4a0763..20e2b38c 100644--- a/internal/base/handler/lang.go+++ b/internal/base/handler/lang.go@@ -11,14 +11,10 @@ import (// GetLang get language from headerfunc GetLang(ctx *gin.Context) i18n.Language {acceptLanguage := ctx.GetHeader(constant.AcceptLanguageFlag)- switch i18n.Language(acceptLanguage) {- case i18n.LanguageChinese:- return i18n.LanguageChinese- case i18n.LanguageEnglish:- return i18n.LanguageEnglish- default:- return i18n.DefaultLang+ if len(acceptLanguage) == 0 {+ return i18n.DefaultLanguage}+ return i18n.Language(acceptLanguage)}// GetLangByCtx get language from header@@ -27,5 +23,5 @@ func GetLangByCtx(ctx context.Context) i18n.Language {if ok {return acceptLanguage}- return i18n.DefaultLang+ return i18n.DefaultLanguage}diff --git a/internal/base/validator/validator.go b/internal/base/validator/validator.goindex 9c9b8017..0da2f6ae 100644--- a/internal/base/validator/validator.go+++ b/internal/base/validator/validator.go@@ -2,23 +2,65 @@ package validatorimport ("errors"+ "fmt""reflect""strings""github.com/answerdev/answer/internal/base/reason""github.com/answerdev/answer/internal/base/translator""github.com/go-playground/locales"+ german "github.com/go-playground/locales/de"english "github.com/go-playground/locales/en"- zhongwen "github.com/go-playground/locales/zh"+ spanish "github.com/go-playground/locales/es"+ french "github.com/go-playground/locales/fr"+ italian "github.com/go-playground/locales/it"+ japanese "github.com/go-playground/locales/ja"+ korean "github.com/go-playground/locales/ko"+ portuguese "github.com/go-playground/locales/pt"+ russian "github.com/go-playground/locales/ru"+ vietnamese "github.com/go-playground/locales/vi"+ chinese "github.com/go-playground/locales/zh"+ chineseTraditional "github.com/go-playground/locales/zh_Hant_TW"ut "github.com/go-playground/universal-translator""github.com/go-playground/validator/v10""github.com/go-playground/validator/v10/translations/en"+ "github.com/go-playground/validator/v10/translations/es"+ "github.com/go-playground/validator/v10/translations/fr"+ "github.com/go-playground/validator/v10/translations/it"+ "github.com/go-playground/validator/v10/translations/ja"+ "github.com/go-playground/validator/v10/translations/pt"+ "github.com/go-playground/validator/v10/translations/ru"+ "github.com/go-playground/validator/v10/translations/vi""github.com/go-playground/validator/v10/translations/zh"+ "github.com/go-playground/validator/v10/translations/zh_tw"myErrors "github.com/segmentfault/pacman/errors""github.com/segmentfault/pacman/i18n""github.com/segmentfault/pacman/log")+type TranslatorLocal struct {+ La i18n.Language+ Lo locales.Translator+ RegisterFunc func(v *validator.Validate, trans ut.Translator) (err error)+}++var (+ allLanguageTranslators = []*TranslatorLocal{+ {La: i18n.LanguageChinese, Lo: chinese.New(), RegisterFunc: zh.RegisterDefaultTranslations},+ {La: i18n.LanguageChineseTraditional, Lo: chineseTraditional.New(), RegisterFunc: zh_tw.RegisterDefaultTranslations},+ {La: i18n.LanguageEnglish, Lo: english.New(), RegisterFunc: en.RegisterDefaultTranslations},+ {La: i18n.LanguageGerman, Lo: german.New(), RegisterFunc: nil},+ {La: i18n.LanguageSpanish, Lo: spanish.New(), RegisterFunc: es.RegisterDefaultTranslations},+ {La: i18n.LanguageFrench, Lo: french.New(), RegisterFunc: fr.RegisterDefaultTranslations},+ {La: i18n.LanguageItalian, Lo: italian.New(), RegisterFunc: it.RegisterDefaultTranslations},+ {La: i18n.LanguageJapanese, Lo: japanese.New(), RegisterFunc: ja.RegisterDefaultTranslations},+ {La: i18n.LanguageKorean, Lo: korean.New(), RegisterFunc: nil},+ {La: i18n.LanguagePortuguese, Lo: portuguese.New(), RegisterFunc: pt.RegisterDefaultTranslations},+ {La: i18n.LanguageRussian, Lo: russian.New(), RegisterFunc: ru.RegisterDefaultTranslations},+ {La: i18n.LanguageVietnamese, Lo: vietnamese.New(), RegisterFunc: vi.RegisterDefaultTranslations},+ }+)+// MyValidator my validatortype MyValidator struct {Validate *validator.Validate@@ -36,23 +78,21 @@ type FormErrorField struct {var GlobalValidatorMapping = make(map[string]*MyValidator, 0)func init() {- zhTran, zhVal := getTran(zhongwen.New(), i18n.LanguageChinese.Abbr()), createDefaultValidator(i18n.LanguageChinese)- if err := zh.RegisterDefaultTranslations(zhVal, zhTran); err != nil {- panic(err)- }- GlobalValidatorMapping[i18n.LanguageChinese.Abbr()] = &MyValidator{Validate: zhVal, Tran: zhTran, Lang: i18n.LanguageChinese}-- enTran, enVal := getTran(english.New(), i18n.LanguageEnglish.Abbr()), createDefaultValidator(i18n.LanguageEnglish)- if err := en.RegisterDefaultTranslations(enVal, enTran); err != nil {- panic(err)+ for _, t := range allLanguageTranslators {+ tran, val := getTran(t.Lo), createDefaultValidator(t.La)+ if t.RegisterFunc != nil {+ if err := t.RegisterFunc(val, tran); err != nil {+ panic(err)+ }+ }+ GlobalValidatorMapping[t.La.Abbr()] = &MyValidator{Validate: val, Tran: tran, Lang: t.La}}- GlobalValidatorMapping[i18n.LanguageEnglish.Abbr()] = &MyValidator{Validate: enVal, Tran: enTran, Lang: i18n.LanguageEnglish}}-func getTran(lo locales.Translator, la string) ut.Translator {- tran, ok := ut.New(lo, lo).GetTranslator(la)+func getTran(lo locales.Translator) ut.Translator {+ tran, ok := ut.New(lo, lo).GetTranslator(lo.Locale())if !ok {- panic(ok)+ panic(fmt.Sprintf("not found translator %s", lo.Locale()))}return tran}@@ -83,7 +123,7 @@ func GetValidatorByLang(la string) *MyValidator {if GlobalValidatorMapping[la] != nil {return GlobalValidatorMapping[la]}- return GlobalValidatorMapping[i18n.DefaultLang.Abbr()]+ return GlobalValidatorMapping[i18n.DefaultLanguage.Abbr()]}// Check /