From 6fdc004dc69122800557c0cb5ff5d8b93896941f Mon Sep 17 00:00:00 2001 From: kakuilan Date: Fri, 20 May 2022 14:55:23 +0800 Subject: [PATCH] zh/zh_tw/ko customTransFunc code reuse --- translations/ko/ko.go | 445 +++++++++--------------------------- translations/zh/zh.go | 445 +++++++++--------------------------- translations/zh_tw/zh_tw.go | 445 +++++++++--------------------------- 3 files changed, 315 insertions(+), 1020 deletions(-) diff --git a/translations/ko/ko.go b/translations/ko/ko.go index 8bd9e11..e9c75bb 100644 --- a/translations/ko/ko.go +++ b/translations/ko/ko.go @@ -16,7 +16,6 @@ import ( // RegisterDefaultTranslations registers a set of default translations // for all built in tag's in validator; you may add your own as desired. func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) { - translations := []struct { tag string translation string @@ -62,7 +61,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "len", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("len-string", "{0}길이는{1}이어야합니다", false); err != nil { return } @@ -91,10 +89,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -153,7 +149,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "min", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("min-string", "{0}길이는{1}이상이어야합니다", false); err != nil { return } @@ -182,10 +177,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -208,7 +201,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er switch kind { case reflect.String: - var c string c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits)) @@ -244,7 +236,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "max", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("max-string", "{0}길이는{1}을초과할수없습니다", false); err != nil { return } @@ -273,10 +264,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -333,39 +322,20 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er }, }, { - tag: "eq", - 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 { - fmt.Printf("경고: 번역필드오류: %#v", fe) - return fe.(error).Error() - } - - return t - }, + tag: "eq", + translation: "{0}은{1}과같지않습니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ne", - 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 { - fmt.Printf("경고: 번역필드오류: %#v", fe) - return fe.(error).Error() - } - - return t - }, + tag: "ne", + translation: "{0}은{1}과같지않아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "lt", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("lt-string", "{0}길이는{1}보다작아야합니다", false); err != nil { return } @@ -399,10 +369,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -410,7 +378,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -485,7 +452,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "lte", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("lte-string", "{0}길이는{1}을초과할수없습니다", false); err != nil { return } @@ -529,7 +495,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -604,7 +569,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "gt", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("gt-string", "{0}길이는{1}보다커야합니다", false); err != nil { return } @@ -640,7 +604,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -722,7 +685,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "gte", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("gte-string", "{0}길이는{1}이상이어야합니다", false); err != nil { return } @@ -758,7 +720,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -766,7 +727,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -839,184 +799,76 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er }, }, { - tag: "eqfield", - 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 - }, + tag: "eqfield", + translation: "{0}은{1}과같아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "eqcsfield", - 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 - }, + tag: "eqcsfield", + translation: "{0}은{1}과같아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "necsfield", - 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 - }, + tag: "necsfield", + translation: "{0}은{1}과같지않아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtcsfield", - 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 - }, + tag: "gtcsfield", + translation: "{0}은{1}보다커야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtecsfield", - 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 - }, + tag: "gtecsfield", + translation: "{0}은{1}보다크거나같아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltcsfield", - 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 - }, + tag: "ltcsfield", + translation: "{0}은{1}보다작아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltecsfield", - 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 - }, + tag: "ltecsfield", + translation: "{0}은{1}보다작거나같아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "nefield", - 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 - }, + tag: "nefield", + translation: "{0}은{1}과같지않아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtfield", - 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 - }, + tag: "gtfield", + translation: "{0}은{1}보다커야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtefield", - 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 - }, + tag: "gtefield", + translation: "{0}은{1}보다크거나같아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltfield", - 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 - }, + tag: "ltfield", + translation: "{0}은{1}보다작아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltefield", - 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 - }, + tag: "ltefield", + translation: "{0}은{1}보다작거나같아야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "alpha", @@ -1104,124 +956,52 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - tag: "contains", - 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 - }, + tag: "contains", + translation: "{0}은텍스트를포함해야합니다'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "containsany", - 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 - }, + tag: "containsany", + translation: "{0}은하나이상의문자를포함해야합니다'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "containsrune", - 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 - }, + tag: "containsrune", + translation: "{0}은문자를포함해야합니다'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludes", - 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 - }, + tag: "excludes", + translation: "{0}은텍스트를포함할수없습니다'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludesall", - 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 - }, + tag: "excludesall", + translation: "{0}은다음문자중하나를포함할수없습니다'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludesrune", - 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 - }, + tag: "excludesrune", + translation: "{0}은'{1}'을포함할수없습니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "endswith", - 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 - }, + tag: "endswith", + translation: "{0}텍스트'{1}'으로끝나야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "startswith", - 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 - }, + tag: "startswith", + translation: "{0}텍스트'{1}'으로시작해야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "isbn", @@ -1394,17 +1174,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - tag: "oneof", - translation: "{0}은[{1}]중하나여야합니다", - override: false, - customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - s, err := ut.T(fe.Tag(), fe.Field(), fe.Param()) - if err != nil { - log.Printf("경고: 번역필드오류: %#v", fe) - return fe.(error).Error() - } - return s - }, + tag: "oneof", + translation: "{0}은[{1}]중하나여야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "json", @@ -1427,19 +1200,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - 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 - }, + tag: "datetime", + translation: "{0}의형식은{1}이어야합니다", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "boolean", @@ -1449,7 +1213,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } for _, t := range translations { - if t.customTransFunc != nil && t.customRegisFunc != nil { err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc) @@ -1475,26 +1238,28 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc { - return func(ut ut.Translator) (err error) { - if err = ut.Add(tag, translation, override); err != nil { return } - return - } - } func translateFunc(ut ut.Translator, fe validator.FieldError) string { - t, err := ut.T(fe.Tag(), fe.Field()) if err != nil { log.Printf("경고: 번역필드오류: %#v", fe) return fe.(error).Error() } - return t } + +func customTransFuncV1(ut ut.Translator, fe validator.FieldError) string { + s, err := ut.T(fe.Tag(), fe.Field(), fe.Param()) + if err != nil { + log.Printf("경고: 번역필드오류: %#v", fe) + return fe.(error).Error() + } + return s +} diff --git a/translations/zh/zh.go b/translations/zh/zh.go index c748587..5463476 100644 --- a/translations/zh/zh.go +++ b/translations/zh/zh.go @@ -16,7 +16,6 @@ import ( // RegisterDefaultTranslations registers a set of default translations // for all built in tag's in validator; you may add your own as desired. func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) { - translations := []struct { tag string translation string @@ -62,7 +61,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "len", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("len-string", "{0}长度必须是{1}", false); err != nil { return } @@ -91,10 +89,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -153,7 +149,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "min", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("min-string", "{0}长度必须至少为{1}", false); err != nil { return } @@ -182,10 +177,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -208,7 +201,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er switch kind { case reflect.String: - var c string c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits)) @@ -244,7 +236,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "max", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("max-string", "{0}长度不能超过{1}", false); err != nil { return } @@ -273,10 +264,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -333,39 +322,20 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er }, }, { - tag: "eq", - 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 { - fmt.Printf("警告: 翻译字段错误: %#v", fe) - return fe.(error).Error() - } - - return t - }, + tag: "eq", + translation: "{0}不等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ne", - 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 { - fmt.Printf("警告: 翻译字段错误: %#v", fe) - return fe.(error).Error() - } - - return t - }, + tag: "ne", + translation: "{0}不能等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "lt", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("lt-string", "{0}长度必须小于{1}", false); err != nil { return } @@ -399,10 +369,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -410,7 +378,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -485,7 +452,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "lte", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("lte-string", "{0}长度不能超过{1}", false); err != nil { return } @@ -529,7 +495,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -604,7 +569,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "gt", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("gt-string", "{0}长度必须大于{1}", false); err != nil { return } @@ -640,7 +604,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -722,7 +685,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "gte", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("gte-string", "{0}长度必须至少为{1}", false); err != nil { return } @@ -758,7 +720,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -766,7 +727,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -839,184 +799,76 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er }, }, { - tag: "eqfield", - 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 - }, + tag: "eqfield", + translation: "{0}必须等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "eqcsfield", - 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 - }, + tag: "eqcsfield", + translation: "{0}必须等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "necsfield", - 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 - }, + tag: "necsfield", + translation: "{0}不能等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtcsfield", - 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 - }, + tag: "gtcsfield", + translation: "{0}必须大于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtecsfield", - 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 - }, + tag: "gtecsfield", + translation: "{0}必须大于或等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltcsfield", - 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 - }, + tag: "ltcsfield", + translation: "{0}必须小于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltecsfield", - 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 - }, + tag: "ltecsfield", + translation: "{0}必须小于或等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "nefield", - 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 - }, + tag: "nefield", + translation: "{0}不能等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtfield", - 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 - }, + tag: "gtfield", + translation: "{0}必须大于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtefield", - 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 - }, + tag: "gtefield", + translation: "{0}必须大于或等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltfield", - 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 - }, + tag: "ltfield", + translation: "{0}必须小于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltefield", - 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 - }, + tag: "ltefield", + translation: "{0}必须小于或等于{1}", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "alpha", @@ -1104,124 +956,52 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - tag: "contains", - 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 - }, + tag: "contains", + translation: "{0}必须包含文本'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "containsany", - 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 - }, + tag: "containsany", + translation: "{0}必须包含至少一个以下字符'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "containsrune", - 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 - }, + tag: "containsrune", + translation: "{0}必须包含字符'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludes", - 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 - }, + tag: "excludes", + translation: "{0}不能包含文本'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludesall", - 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 - }, + tag: "excludesall", + translation: "{0}不能包含以下任何字符'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludesrune", - 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 - }, + tag: "excludesrune", + translation: "{0}不能包含'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "endswith", - 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 - }, + tag: "endswith", + translation: "{0}必须以文本'{1}'结尾", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "startswith", - 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 - }, + tag: "startswith", + translation: "{0}必须以文本'{1}'开头", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "isbn", @@ -1394,17 +1174,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - tag: "oneof", - translation: "{0}必须是[{1}]中的一个", - override: false, - customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - s, err := ut.T(fe.Tag(), fe.Field(), fe.Param()) - if err != nil { - log.Printf("警告: 翻译字段错误: %#v", fe) - return fe.(error).Error() - } - return s - }, + tag: "oneof", + translation: "{0}必须是[{1}]中的一个", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "json", @@ -1427,19 +1200,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - 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 - }, + tag: "datetime", + translation: "{0}的格式必须是{1}", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "boolean", @@ -1449,7 +1213,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } for _, t := range translations { - if t.customTransFunc != nil && t.customRegisFunc != nil { err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc) @@ -1475,26 +1238,28 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc { - return func(ut ut.Translator) (err error) { - if err = ut.Add(tag, translation, override); err != nil { return } - return - } - } func translateFunc(ut ut.Translator, fe validator.FieldError) string { - t, err := ut.T(fe.Tag(), fe.Field()) if err != nil { log.Printf("警告: 翻译字段错误: %#v", fe) return fe.(error).Error() } - return t } + +func customTransFuncV1(ut ut.Translator, fe validator.FieldError) string { + s, err := ut.T(fe.Tag(), fe.Field(), fe.Param()) + if err != nil { + log.Printf("警告: 翻译字段错误: %#v", fe) + return fe.(error).Error() + } + return s +} diff --git a/translations/zh_tw/zh_tw.go b/translations/zh_tw/zh_tw.go index a5b10e4..77f7584 100644 --- a/translations/zh_tw/zh_tw.go +++ b/translations/zh_tw/zh_tw.go @@ -16,7 +16,6 @@ import ( // RegisterDefaultTranslations registers a set of default translations // for all built in tag's in validator; you may add your own as desired. func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) { - translations := []struct { tag string translation string @@ -62,7 +61,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "len", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("len-string", "{0}長度必須為{1}", false); err != nil { return } @@ -91,10 +89,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -153,7 +149,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "min", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("min-string", "{0}長度必須至少為{1}", false); err != nil { return } @@ -182,10 +177,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -208,7 +201,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er switch kind { case reflect.String: - var c string c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits)) @@ -244,7 +236,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "max", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("max-string", "{0}長度不能超過{1}", false); err != nil { return } @@ -273,10 +264,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string @@ -333,39 +322,20 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er }, }, { - tag: "eq", - 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 { - fmt.Printf("警告: 翻譯欄位錯誤: %#v", fe) - return fe.(error).Error() - } - - return t - }, + tag: "eq", + translation: "{0}不等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ne", - 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 { - fmt.Printf("警告: 翻譯欄位錯誤: %#v", fe) - return fe.(error).Error() - } - - return t - }, + tag: "ne", + translation: "{0}不能等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "lt", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("lt-string", "{0}長度必須小於{1}", false); err != nil { return } @@ -399,10 +369,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } return - }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -410,7 +378,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -485,7 +452,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "lte", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("lte-string", "{0}長度不能超過{1}", false); err != nil { return } @@ -529,7 +495,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -604,7 +569,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "gt", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("gt-string", "{0}長度必須大於{1}", false); err != nil { return } @@ -640,7 +604,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -722,7 +685,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er { tag: "gte", customRegisFunc: func(ut ut.Translator) (err error) { - if err = ut.Add("gte-string", "{0}長度必須至少為{1}", false); err != nil { return } @@ -758,7 +720,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return }, customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - var err error var t string var f64 float64 @@ -766,7 +727,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er var kind reflect.Kind fn := func() (err error) { - if idx := strings.Index(fe.Param(), "."); idx != -1 { digits = uint64(len(fe.Param()[idx+1:])) } @@ -839,184 +799,76 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er }, }, { - tag: "eqfield", - 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 - }, + tag: "eqfield", + translation: "{0}必須等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "eqcsfield", - 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 - }, + tag: "eqcsfield", + translation: "{0}必須等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "necsfield", - 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 - }, + tag: "necsfield", + translation: "{0}不能等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtcsfield", - 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 - }, + tag: "gtcsfield", + translation: "{0}必須大於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtecsfield", - 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 - }, + tag: "gtecsfield", + translation: "{0}必須大於或等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltcsfield", - 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 - }, + tag: "ltcsfield", + translation: "{0}必須小於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltecsfield", - 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 - }, + tag: "ltecsfield", + translation: "{0}必須小於或等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "nefield", - 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 - }, + tag: "nefield", + translation: "{0}不能等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtfield", - 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 - }, + tag: "gtfield", + translation: "{0}必須大於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "gtefield", - 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 - }, + tag: "gtefield", + translation: "{0}必須大於或等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltfield", - 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 - }, + tag: "ltfield", + translation: "{0}必須小於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "ltefield", - 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 - }, + tag: "ltefield", + translation: "{0}必須小於或等於{1}", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "alpha", @@ -1104,124 +956,52 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - tag: "contains", - 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 - }, + tag: "contains", + translation: "{0}必須包含文字'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "containsany", - 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 - }, + tag: "containsany", + translation: "{0}必須包含至少一個以下字元'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "containsrune", - 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 - }, + tag: "containsrune", + translation: "{0}必須包含字元'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludes", - 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 - }, + tag: "excludes", + translation: "{0}不能包含文字'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludesall", - 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 - }, + tag: "excludesall", + translation: "{0}不能包含以下任何字元'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "excludesrune", - 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 - }, + tag: "excludesrune", + translation: "{0}不能包含'{1}'", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "endswith", - 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 - }, + tag: "endswith", + translation: "{0}必須以文本'{1}'結尾", + override: false, + customTransFunc: customTransFuncV1, }, { - tag: "startswith", - 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 - }, + tag: "startswith", + translation: "{0}必須以文本'{1}'開頭", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "isbn", @@ -1394,17 +1174,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - tag: "oneof", - translation: "{0}必須是[{1}]中的一個", - override: false, - customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { - s, err := ut.T(fe.Tag(), fe.Field(), fe.Param()) - if err != nil { - log.Printf("警告: 翻譯欄位錯誤: %#v", fe) - return fe.(error).Error() - } - return s - }, + tag: "oneof", + translation: "{0}必須是[{1}]中的一個", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "json", @@ -1427,19 +1200,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er override: false, }, { - 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 - }, + tag: "datetime", + translation: "{0}的格式必須是{1}", + override: false, + customTransFunc: customTransFuncV1, }, { tag: "boolean", @@ -1449,7 +1213,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } for _, t := range translations { - if t.customTransFunc != nil && t.customRegisFunc != nil { err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc) @@ -1475,26 +1238,28 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er } func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc { - return func(ut ut.Translator) (err error) { - if err = ut.Add(tag, translation, override); err != nil { return } - return - } - } func translateFunc(ut ut.Translator, fe validator.FieldError) string { - t, err := ut.T(fe.Tag(), fe.Field()) if err != nil { log.Printf("警告: 翻譯欄位錯誤: %#v", fe) return fe.(error).Error() } - return t } + +func customTransFuncV1(ut ut.Translator, fe validator.FieldError) string { + s, err := ut.T(fe.Tag(), fe.Field(), fe.Param()) + if err != nil { + log.Printf("警告: 翻譯欄位錯誤: %#v", fe) + return fe.(error).Error() + } + return s +}