diff --git a/translations/zh/zh.go b/translations/zh/zh.go index c3032b7..cbd466d 100644 --- a/translations/zh/zh.go +++ b/translations/zh/zh.go @@ -1308,6 +1308,21 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return s }, }, + { + tag: "datetime", + translation: "{0}与{1}格式不匹配", + override: false, + customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { + + t, err := ut.T(fe.Tag(), fe.Field(), fe.Param()) + if err != nil { + log.Printf("警告: 翻译字段错误: %#v", fe) + return fe.(error).Error() + } + + return t + }, + }, } for _, t := range translations { diff --git a/translations/zh/zh_test.go b/translations/zh/zh_test.go index 26a21ae..7ada56d 100644 --- a/translations/zh/zh_test.go +++ b/translations/zh/zh_test.go @@ -4,9 +4,9 @@ import ( "testing" "time" + . "github.com/go-playground/assert/v2" zhongwen "github.com/go-playground/locales/zh" ut "github.com/go-playground/universal-translator" - . "github.com/go-playground/assert/v2" "github.com/go-playground/validator/v10" ) @@ -138,6 +138,7 @@ func TestTranslations(t *testing.T) { StrPtrGte *string `validate:"gte=10"` OneOfString string `validate:"oneof=red green"` OneOfInt int `validate:"oneof=5 63"` + Datetime string `validate:"datetime=2006-01-02"` } var test Test @@ -184,6 +185,8 @@ func TestTranslations(t *testing.T) { test.StrPtrMaxLen = &s test.StrPtrLen = &s + test.Datetime = "2008-Feb-01" + err = validate.Struct(test) NotEqual(t, err, nil) @@ -614,6 +617,10 @@ func TestTranslations(t *testing.T) { ns: "Test.OneOfInt", expected: "OneOfInt必须是[5 63]中的一个", }, + { + ns: "Test.Datetime", + expected: "Datetime与2006-01-02格式不匹配", + }, } for _, tt := range tests { diff --git a/translations/zh_tw/zh_tw.go b/translations/zh_tw/zh_tw.go index cf6f933..391a010 100644 --- a/translations/zh_tw/zh_tw.go +++ b/translations/zh_tw/zh_tw.go @@ -1304,6 +1304,21 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return s }, }, + { + tag: "datetime", + translation: "{0}與{1}格式不匹配", + override: false, + customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { + + t, err := ut.T(fe.Tag(), fe.Field(), fe.Param()) + if err != nil { + log.Printf("警告: 翻譯欄位錯誤: %#v", fe) + return fe.(error).Error() + } + + return t + }, + }, } for _, t := range translations { diff --git a/translations/zh_tw/zh_tw_test.go b/translations/zh_tw/zh_tw_test.go index a4463a2..cf60d1d 100644 --- a/translations/zh_tw/zh_tw_test.go +++ b/translations/zh_tw/zh_tw_test.go @@ -4,9 +4,9 @@ import ( "testing" "time" + . "github.com/go-playground/assert/v2" zhongwen "github.com/go-playground/locales/zh_Hant_TW" ut "github.com/go-playground/universal-translator" - . "github.com/go-playground/assert/v2" "github.com/go-playground/validator/v10" ) @@ -138,6 +138,7 @@ func TestTranslations(t *testing.T) { StrPtrGte *string `validate:"gte=10"` OneOfString string `validate:"oneof=red green"` OneOfInt int `validate:"oneof=5 63"` + Datetime string `validate:"datetime=2006-01-02"` } var test Test @@ -184,6 +185,8 @@ func TestTranslations(t *testing.T) { test.StrPtrMaxLen = &s test.StrPtrLen = &s + test.Datetime = "2008-Feb-01" + err = validate.Struct(test) NotEqual(t, err, nil) @@ -614,6 +617,10 @@ func TestTranslations(t *testing.T) { ns: "Test.OneOfInt", expected: "OneOfInt必須是[5 63]中的一個", }, + { + ns: "Test.Datetime", + expected: "Datetime與2006-01-02格式不匹配", + }, } for _, tt := range tests {