zh/zh_tw/ko customTransFunc code reuse

pull/947/head
kakuilan 3 years ago
parent 04582ec18f
commit 6fdc004dc6
  1. 445
      translations/ko/ko.go
  2. 445
      translations/zh/zh.go
  3. 445
      translations/zh_tw/zh_tw.go

@ -16,7 +16,6 @@ import (
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations
// for all built in tag's in validator; you may add your own as desired. // 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) { func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) {
translations := []struct { translations := []struct {
tag string tag string
translation string translation string
@ -62,7 +61,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "len", tag: "len",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("len-string", "{0}길이는{1}이어야합니다", false); err != nil { if err = ut.Add("len-string", "{0}길이는{1}이어야합니다", false); err != nil {
return return
} }
@ -91,10 +89,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -153,7 +149,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "min", tag: "min",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("min-string", "{0}길이는{1}이상이어야합니다", false); err != nil { if err = ut.Add("min-string", "{0}길이는{1}이상이어야합니다", false); err != nil {
return return
} }
@ -182,10 +177,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -208,7 +201,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
switch kind { switch kind {
case reflect.String: case reflect.String:
var c string var c string
c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits)) 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", tag: "max",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("max-string", "{0}길이는{1}을초과할수없습니다", false); err != nil { if err = ut.Add("max-string", "{0}길이는{1}을초과할수없습니다", false); err != nil {
return return
} }
@ -273,10 +264,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -333,39 +322,20 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
}, },
}, },
{ {
tag: "eq", tag: "eq",
translation: "{0}은{1}과같지않습니다", translation: "{0}은{1}과같지않습니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ne",
translation: "{0}은{1}과같지않아야합니다", translation: "{0}은{1}과같지않아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
fmt.Printf("경고: 번역필드오류: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "lt", tag: "lt",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("lt-string", "{0}길이는{1}보다작아야합니다", false); err != nil { if err = ut.Add("lt-string", "{0}길이는{1}보다작아야합니다", false); err != nil {
return return
} }
@ -399,10 +369,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -410,7 +378,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "lte",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("lte-string", "{0}길이는{1}을초과할수없습니다", false); err != nil { if err = ut.Add("lte-string", "{0}길이는{1}을초과할수없습니다", false); err != nil {
return return
} }
@ -529,7 +495,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "gt",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("gt-string", "{0}길이는{1}보다커야합니다", false); err != nil { if err = ut.Add("gt-string", "{0}길이는{1}보다커야합니다", false); err != nil {
return return
} }
@ -640,7 +604,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -722,7 +685,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "gte", tag: "gte",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("gte-string", "{0}길이는{1}이상이어야합니다", false); err != nil { if err = ut.Add("gte-string", "{0}길이는{1}이상이어야합니다", false); err != nil {
return return
} }
@ -758,7 +720,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -766,7 +727,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "eqfield",
translation: "{0}은{1}과같아야합니다", translation: "{0}은{1}과같아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "eqcsfield",
translation: "{0}은{1}과같아야합니다", translation: "{0}은{1}과같아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "necsfield",
translation: "{0}은{1}과같지않아야합니다", translation: "{0}은{1}과같지않아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtcsfield",
translation: "{0}은{1}보다커야합니다", translation: "{0}은{1}보다커야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtecsfield",
translation: "{0}은{1}보다크거나같아야합니다", translation: "{0}은{1}보다크거나같아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltcsfield",
translation: "{0}은{1}보다작아야합니다", translation: "{0}은{1}보다작아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltecsfield",
translation: "{0}은{1}보다작거나같아야합니다", translation: "{0}은{1}보다작거나같아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "nefield",
translation: "{0}은{1}과같지않아야합니다", translation: "{0}은{1}과같지않아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtfield",
translation: "{0}은{1}보다커야합니다", translation: "{0}은{1}보다커야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtefield",
translation: "{0}은{1}보다크거나같아야합니다", translation: "{0}은{1}보다크거나같아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltfield",
translation: "{0}은{1}보다작아야합니다", translation: "{0}은{1}보다작아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltefield",
translation: "{0}은{1}보다작거나같아야합니다", translation: "{0}은{1}보다작거나같아야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("경고: 번역필드오류: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "alpha", tag: "alpha",
@ -1104,124 +956,52 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "contains", tag: "contains",
translation: "{0}은텍스트를포함해야합니다'{1}'", translation: "{0}은텍스트를포함해야합니다'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "containsany",
translation: "{0}은하나이상의문자를포함해야합니다'{1}'", translation: "{0}은하나이상의문자를포함해야합니다'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "containsrune",
translation: "{0}은문자를포함해야합니다'{1}'", translation: "{0}은문자를포함해야합니다'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludes",
translation: "{0}은텍스트를포함할수없습니다'{1}'", translation: "{0}은텍스트를포함할수없습니다'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludesall",
translation: "{0}은다음문자중하나를포함할수없습니다'{1}'", translation: "{0}은다음문자중하나를포함할수없습니다'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludesrune",
translation: "{0}은'{1}'을포함할수없습니다", translation: "{0}은'{1}'을포함할수없습니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "endswith",
translation: "{0}텍스트'{1}'으로끝나야합니다", translation: "{0}텍스트'{1}'으로끝나야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "startswith",
translation: "{0}텍스트'{1}'으로시작해야합니다", translation: "{0}텍스트'{1}'으로시작해야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("경고: 번역필드오류: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "isbn", tag: "isbn",
@ -1394,17 +1174,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "oneof", tag: "oneof",
translation: "{0}은[{1}]중하나여야합니다", translation: "{0}은[{1}]중하나여야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
s, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("경고: 번역필드오류: %#v", fe)
return fe.(error).Error()
}
return s
},
}, },
{ {
tag: "json", tag: "json",
@ -1427,19 +1200,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "datetime", tag: "datetime",
translation: "{0}의형식은{1}이어야합니다", translation: "{0}의형식은{1}이어야합니다",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("경고: 번역필드오류: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "boolean", tag: "boolean",
@ -1449,7 +1213,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
for _, t := range translations { for _, t := range translations {
if t.customTransFunc != nil && t.customRegisFunc != nil { if t.customTransFunc != nil && t.customRegisFunc != nil {
err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc) 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 { func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc {
return func(ut ut.Translator) (err error) { return func(ut ut.Translator) (err error) {
if err = ut.Add(tag, translation, override); err != nil { if err = ut.Add(tag, translation, override); err != nil {
return return
} }
return return
} }
} }
func translateFunc(ut ut.Translator, fe validator.FieldError) string { func translateFunc(ut ut.Translator, fe validator.FieldError) string {
t, err := ut.T(fe.Tag(), fe.Field()) t, err := ut.T(fe.Tag(), fe.Field())
if err != nil { if err != nil {
log.Printf("경고: 번역필드오류: %#v", fe) log.Printf("경고: 번역필드오류: %#v", fe)
return fe.(error).Error() return fe.(error).Error()
} }
return t 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
}

@ -16,7 +16,6 @@ import (
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations
// for all built in tag's in validator; you may add your own as desired. // 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) { func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) {
translations := []struct { translations := []struct {
tag string tag string
translation string translation string
@ -62,7 +61,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "len", tag: "len",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("len-string", "{0}长度必须是{1}", false); err != nil { if err = ut.Add("len-string", "{0}长度必须是{1}", false); err != nil {
return return
} }
@ -91,10 +89,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -153,7 +149,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "min", tag: "min",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("min-string", "{0}长度必须至少为{1}", false); err != nil { if err = ut.Add("min-string", "{0}长度必须至少为{1}", false); err != nil {
return return
} }
@ -182,10 +177,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -208,7 +201,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
switch kind { switch kind {
case reflect.String: case reflect.String:
var c string var c string
c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits)) 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", tag: "max",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("max-string", "{0}长度不能超过{1}", false); err != nil { if err = ut.Add("max-string", "{0}长度不能超过{1}", false); err != nil {
return return
} }
@ -273,10 +264,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -333,39 +322,20 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
}, },
}, },
{ {
tag: "eq", tag: "eq",
translation: "{0}不等于{1}", translation: "{0}不等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ne",
translation: "{0}不能等于{1}", translation: "{0}不能等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
fmt.Printf("警告: 翻译字段错误: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "lt", tag: "lt",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("lt-string", "{0}长度必须小于{1}", false); err != nil { if err = ut.Add("lt-string", "{0}长度必须小于{1}", false); err != nil {
return return
} }
@ -399,10 +369,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -410,7 +378,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "lte",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("lte-string", "{0}长度不能超过{1}", false); err != nil { if err = ut.Add("lte-string", "{0}长度不能超过{1}", false); err != nil {
return return
} }
@ -529,7 +495,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "gt",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("gt-string", "{0}长度必须大于{1}", false); err != nil { if err = ut.Add("gt-string", "{0}长度必须大于{1}", false); err != nil {
return return
} }
@ -640,7 +604,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -722,7 +685,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "gte", tag: "gte",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("gte-string", "{0}长度必须至少为{1}", false); err != nil { if err = ut.Add("gte-string", "{0}长度必须至少为{1}", false); err != nil {
return return
} }
@ -758,7 +720,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -766,7 +727,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "eqfield",
translation: "{0}必须等于{1}", translation: "{0}必须等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "eqcsfield",
translation: "{0}必须等于{1}", translation: "{0}必须等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "necsfield",
translation: "{0}不能等于{1}", translation: "{0}不能等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtcsfield",
translation: "{0}必须大于{1}", translation: "{0}必须大于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtecsfield",
translation: "{0}必须大于或等于{1}", translation: "{0}必须大于或等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltcsfield",
translation: "{0}必须小于{1}", translation: "{0}必须小于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltecsfield",
translation: "{0}必须小于或等于{1}", translation: "{0}必须小于或等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "nefield",
translation: "{0}不能等于{1}", translation: "{0}不能等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtfield",
translation: "{0}必须大于{1}", translation: "{0}必须大于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtefield",
translation: "{0}必须大于或等于{1}", translation: "{0}必须大于或等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltfield",
translation: "{0}必须小于{1}", translation: "{0}必须小于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltefield",
translation: "{0}必须小于或等于{1}", translation: "{0}必须小于或等于{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("警告: 翻译字段错误: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "alpha", tag: "alpha",
@ -1104,124 +956,52 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "contains", tag: "contains",
translation: "{0}必须包含文本'{1}'", translation: "{0}必须包含文本'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "containsany",
translation: "{0}必须包含至少一个以下字符'{1}'", translation: "{0}必须包含至少一个以下字符'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "containsrune",
translation: "{0}必须包含字符'{1}'", translation: "{0}必须包含字符'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludes",
translation: "{0}不能包含文本'{1}'", translation: "{0}不能包含文本'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludesall",
translation: "{0}不能包含以下任何字符'{1}'", translation: "{0}不能包含以下任何字符'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludesrune",
translation: "{0}不能包含'{1}'", translation: "{0}不能包含'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "endswith",
translation: "{0}必须以文本'{1}'结尾", translation: "{0}必须以文本'{1}'结尾",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "startswith",
translation: "{0}必须以文本'{1}'开头", translation: "{0}必须以文本'{1}'开头",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("警告: 翻译字段错误: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "isbn", tag: "isbn",
@ -1394,17 +1174,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "oneof", tag: "oneof",
translation: "{0}必须是[{1}]中的一个", translation: "{0}必须是[{1}]中的一个",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
s, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("警告: 翻译字段错误: %#v", fe)
return fe.(error).Error()
}
return s
},
}, },
{ {
tag: "json", tag: "json",
@ -1427,19 +1200,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "datetime", tag: "datetime",
translation: "{0}的格式必须是{1}", translation: "{0}的格式必须是{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("警告: 翻译字段错误: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "boolean", tag: "boolean",
@ -1449,7 +1213,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
for _, t := range translations { for _, t := range translations {
if t.customTransFunc != nil && t.customRegisFunc != nil { if t.customTransFunc != nil && t.customRegisFunc != nil {
err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc) 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 { func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc {
return func(ut ut.Translator) (err error) { return func(ut ut.Translator) (err error) {
if err = ut.Add(tag, translation, override); err != nil { if err = ut.Add(tag, translation, override); err != nil {
return return
} }
return return
} }
} }
func translateFunc(ut ut.Translator, fe validator.FieldError) string { func translateFunc(ut ut.Translator, fe validator.FieldError) string {
t, err := ut.T(fe.Tag(), fe.Field()) t, err := ut.T(fe.Tag(), fe.Field())
if err != nil { if err != nil {
log.Printf("警告: 翻译字段错误: %#v", fe) log.Printf("警告: 翻译字段错误: %#v", fe)
return fe.(error).Error() return fe.(error).Error()
} }
return t 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
}

@ -16,7 +16,6 @@ import (
// RegisterDefaultTranslations registers a set of default translations // RegisterDefaultTranslations registers a set of default translations
// for all built in tag's in validator; you may add your own as desired. // 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) { func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) {
translations := []struct { translations := []struct {
tag string tag string
translation string translation string
@ -62,7 +61,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "len", tag: "len",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("len-string", "{0}長度必須為{1}", false); err != nil { if err = ut.Add("len-string", "{0}長度必須為{1}", false); err != nil {
return return
} }
@ -91,10 +89,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -153,7 +149,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "min", tag: "min",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("min-string", "{0}長度必須至少為{1}", false); err != nil { if err = ut.Add("min-string", "{0}長度必須至少為{1}", false); err != nil {
return return
} }
@ -182,10 +177,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -208,7 +201,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
switch kind { switch kind {
case reflect.String: case reflect.String:
var c string var c string
c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits)) 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", tag: "max",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("max-string", "{0}長度不能超過{1}", false); err != nil { if err = ut.Add("max-string", "{0}長度不能超過{1}", false); err != nil {
return return
} }
@ -273,10 +264,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
@ -333,39 +322,20 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
}, },
}, },
{ {
tag: "eq", tag: "eq",
translation: "{0}不等於{1}", translation: "{0}不等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ne",
translation: "{0}不能等於{1}", translation: "{0}不能等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
fmt.Printf("警告: 翻譯欄位錯誤: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "lt", tag: "lt",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("lt-string", "{0}長度必須小於{1}", false); err != nil { if err = ut.Add("lt-string", "{0}長度必須小於{1}", false); err != nil {
return return
} }
@ -399,10 +369,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -410,7 +378,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "lte",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("lte-string", "{0}長度不能超過{1}", false); err != nil { if err = ut.Add("lte-string", "{0}長度不能超過{1}", false); err != nil {
return return
} }
@ -529,7 +495,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "gt",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("gt-string", "{0}長度必須大於{1}", false); err != nil { if err = ut.Add("gt-string", "{0}長度必須大於{1}", false); err != nil {
return return
} }
@ -640,7 +604,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -722,7 +685,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{ {
tag: "gte", tag: "gte",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("gte-string", "{0}長度必須至少為{1}", false); err != nil { if err = ut.Add("gte-string", "{0}長度必須至少為{1}", false); err != nil {
return return
} }
@ -758,7 +720,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return return
}, },
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
var err error var err error
var t string var t string
var f64 float64 var f64 float64
@ -766,7 +727,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
var kind reflect.Kind var kind reflect.Kind
fn := func() (err error) { fn := func() (err error) {
if idx := strings.Index(fe.Param(), "."); idx != -1 { if idx := strings.Index(fe.Param(), "."); idx != -1 {
digits = uint64(len(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", tag: "eqfield",
translation: "{0}必須等於{1}", translation: "{0}必須等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "eqcsfield",
translation: "{0}必須等於{1}", translation: "{0}必須等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "necsfield",
translation: "{0}不能等於{1}", translation: "{0}不能等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtcsfield",
translation: "{0}必須大於{1}", translation: "{0}必須大於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtecsfield",
translation: "{0}必須大於或等於{1}", translation: "{0}必須大於或等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltcsfield",
translation: "{0}必須小於{1}", translation: "{0}必須小於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltecsfield",
translation: "{0}必須小於或等於{1}", translation: "{0}必須小於或等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "nefield",
translation: "{0}不能等於{1}", translation: "{0}不能等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtfield",
translation: "{0}必須大於{1}", translation: "{0}必須大於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "gtefield",
translation: "{0}必須大於或等於{1}", translation: "{0}必須大於或等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltfield",
translation: "{0}必須小於{1}", translation: "{0}必須小於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "ltefield",
translation: "{0}必須小於或等於{1}", translation: "{0}必須小於或等於{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("警告: 翻譯欄位錯誤: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "alpha", tag: "alpha",
@ -1104,124 +956,52 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "contains", tag: "contains",
translation: "{0}必須包含文字'{1}'", translation: "{0}必須包含文字'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "containsany",
translation: "{0}必須包含至少一個以下字元'{1}'", translation: "{0}必須包含至少一個以下字元'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "containsrune",
translation: "{0}必須包含字元'{1}'", translation: "{0}必須包含字元'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludes",
translation: "{0}不能包含文字'{1}'", translation: "{0}不能包含文字'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludesall",
translation: "{0}不能包含以下任何字元'{1}'", translation: "{0}不能包含以下任何字元'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "excludesrune",
translation: "{0}不能包含'{1}'", translation: "{0}不能包含'{1}'",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "endswith",
translation: "{0}必須以文本'{1}'結尾", translation: "{0}必須以文本'{1}'結尾",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
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", tag: "startswith",
translation: "{0}必須以文本'{1}'開頭", translation: "{0}必須以文本'{1}'開頭",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("警告: 翻譯欄位錯誤: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "isbn", tag: "isbn",
@ -1394,17 +1174,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "oneof", tag: "oneof",
translation: "{0}必須是[{1}]中的一個", translation: "{0}必須是[{1}]中的一個",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
s, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("警告: 翻譯欄位錯誤: %#v", fe)
return fe.(error).Error()
}
return s
},
}, },
{ {
tag: "json", tag: "json",
@ -1427,19 +1200,10 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false, override: false,
}, },
{ {
tag: "datetime", tag: "datetime",
translation: "{0}的格式必須是{1}", translation: "{0}的格式必須是{1}",
override: false, override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string { customTransFunc: customTransFuncV1,
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("警告: 翻譯欄位錯誤: %#v", fe)
return fe.(error).Error()
}
return t
},
}, },
{ {
tag: "boolean", tag: "boolean",
@ -1449,7 +1213,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
} }
for _, t := range translations { for _, t := range translations {
if t.customTransFunc != nil && t.customRegisFunc != nil { if t.customTransFunc != nil && t.customRegisFunc != nil {
err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc) 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 { func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc {
return func(ut ut.Translator) (err error) { return func(ut ut.Translator) (err error) {
if err = ut.Add(tag, translation, override); err != nil { if err = ut.Add(tag, translation, override); err != nil {
return return
} }
return return
} }
} }
func translateFunc(ut ut.Translator, fe validator.FieldError) string { func translateFunc(ut ut.Translator, fe validator.FieldError) string {
t, err := ut.T(fe.Tag(), fe.Field()) t, err := ut.T(fe.Tag(), fe.Field())
if err != nil { if err != nil {
log.Printf("警告: 翻譯欄位錯誤: %#v", fe) log.Printf("警告: 翻譯欄位錯誤: %#v", fe)
return fe.(error).Error() return fe.(error).Error()
} }
return t 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
}

Loading…
Cancel
Save