Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zh,zh_tw datetime translation #609

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions translations/zh_tw/zh_tw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion translations/zh_tw/zh_tw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 {
Expand Down