zh_tw translation packages add required_*/alphanumunicode/containsrune/endswith/startswith/e164/unique/jwt/boolean/lowercase/uppercase translations

pull/947/head
kakuilan 3 years ago
parent 3446b576c9
commit c42e1e96d7
  1. 2
      translations/zh/zh.go
  2. 2
      translations/zh/zh_test.go
  3. 124
      translations/zh_tw/zh_tw.go
  4. 88
      translations/zh_tw/zh_tw_test.go

@ -648,7 +648,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:]))
} }
@ -699,7 +698,6 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
if fe.Type() != reflect.TypeOf(time.Time{}) { if fe.Type() != reflect.TypeOf(time.Time{}) {
err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag()) err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
} else { } else {
t, err = ut.T("gt-datetime", fe.Field()) t, err = ut.T("gt-datetime", fe.Field())
} }

@ -1,7 +1,6 @@
package zh package zh
import ( import (
"fmt"
"testing" "testing"
"time" "time"
@ -724,7 +723,6 @@ func TestTranslations(t *testing.T) {
} }
} }
fmt.Printf("===================\n%v ---\n%v ----\n%v \n", t, tt, fe)
NotEqual(t, fe, nil) NotEqual(t, fe, nil)
Equal(t, tt.expected, fe.Translate(trans)) Equal(t, tt.expected, fe.Translate(trans))
} }

@ -29,6 +29,36 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0}為必填欄位", translation: "{0}為必填欄位",
override: false, override: false,
}, },
{
tag: "required_if",
translation: "{0}為必填欄位",
override: false,
},
{
tag: "required_unless",
translation: "{0}為必填欄位",
override: false,
},
{
tag: "required_with",
translation: "{0}為必填欄位",
override: false,
},
{
tag: "required_with_all",
translation: "{0}為必填欄位",
override: false,
},
{
tag: "required_without",
translation: "{0}為必填欄位",
override: false,
},
{
tag: "required_without_all",
translation: "{0}為必填欄位",
override: false,
},
{ {
tag: "len", tag: "len",
customRegisFunc: func(ut ut.Translator) (err error) { customRegisFunc: func(ut ut.Translator) (err error) {
@ -621,7 +651,9 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
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:]))
} }
f64, err = strconv.ParseFloat(fe.Param(), 64) f64, err = strconv.ParseFloat(fe.Param(), 64)
return return
} }
@ -996,6 +1028,16 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0}只能包含字母和數字", translation: "{0}只能包含字母和數字",
override: false, override: false,
}, },
{
tag: "alphanumunicode",
translation: "{0}只能包含字母數字和Unicode字元",
override: false,
},
{
tag: "alphaunicode",
translation: "{0}只能包含字母和Unicode字元",
override: false,
},
{ {
tag: "numeric", tag: "numeric",
translation: "{0}必須是一個有效的數值", translation: "{0}必須是一個有效的數值",
@ -1036,6 +1078,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0}必須是一個有效的HSLA顏色", translation: "{0}必須是一個有效的HSLA顏色",
override: false, override: false,
}, },
{
tag: "e164",
translation: "{0}必須是一個有效的E.164手機號",
override: false,
},
{ {
tag: "email", tag: "email",
translation: "{0}必須是一個有效的信箱", translation: "{0}必須是一個有效的信箱",
@ -1086,6 +1133,21 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
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: "excludes", tag: "excludes",
translation: "{0}不能包含文字'{1}'", translation: "{0}不能包含文字'{1}'",
@ -1131,6 +1193,36 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
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: "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: "isbn", tag: "isbn",
translation: "{0}必須是一個有效的ISBN編號", translation: "{0}必須是一個有效的ISBN編號",
@ -1291,6 +1383,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0}必須是一個有效的MAC地址", translation: "{0}必須是一個有效的MAC地址",
override: false, override: false,
}, },
{
tag: "unique",
translation: "{0}字段內的值必須唯一",
override: false,
},
{ {
tag: "iscolor", tag: "iscolor",
translation: "{0}必須是一個有效的顏色", translation: "{0}必須是一個有效的顏色",
@ -1309,9 +1406,29 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return s return s
}, },
}, },
{
tag: "json",
translation: "{0}必須是一個有效的JSON字符串",
override: false,
},
{
tag: "jwt",
translation: "{0}必須是一個有效的JWT字符串",
override: false,
},
{
tag: "lowercase",
translation: "{0}必須是小寫字母",
override: false,
},
{
tag: "uppercase",
translation: "{0}必須是大寫字母",
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: func(ut ut.Translator, fe validator.FieldError) string {
@ -1324,6 +1441,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "boolean",
translation: "{0}必須是一個有效的布爾值",
override: false,
},
} }
for _, t := range translations { for _, t := range translations {

@ -80,6 +80,8 @@ func TestTranslations(t *testing.T) {
LteFieldString string `validate:"ltefield=MaxString"` LteFieldString string `validate:"ltefield=MaxString"`
AlphaString string `validate:"alpha"` AlphaString string `validate:"alpha"`
AlphanumString string `validate:"alphanum"` AlphanumString string `validate:"alphanum"`
AlphanumUnicodeString string `validate:"alphanumunicode"`
AlphaUnicodeString string `validate:"alphaunicode"`
NumericString string `validate:"numeric"` NumericString string `validate:"numeric"`
NumberString string `validate:"number"` NumberString string `validate:"number"`
HexadecimalString string `validate:"hexadecimal"` HexadecimalString string `validate:"hexadecimal"`
@ -88,15 +90,19 @@ func TestTranslations(t *testing.T) {
RGBAColorString string `validate:"rgba"` RGBAColorString string `validate:"rgba"`
HSLColorString string `validate:"hsl"` HSLColorString string `validate:"hsl"`
HSLAColorString string `validate:"hsla"` HSLAColorString string `validate:"hsla"`
E164 string `validate:"e164"`
Email string `validate:"email"` Email string `validate:"email"`
URL string `validate:"url"` URL string `validate:"url"`
URI string `validate:"uri"` URI string `validate:"uri"`
Base64 string `validate:"base64"` Base64 string `validate:"base64"`
Contains string `validate:"contains=purpose"` Contains string `validate:"contains=purpose"`
ContainsAny string `validate:"containsany=!@#$"` ContainsAny string `validate:"containsany=!@#$"`
ContainsRune string `validate:"containsrune=☻"`
Excludes string `validate:"excludes=text"` Excludes string `validate:"excludes=text"`
ExcludesAll string `validate:"excludesall=!@#$"` ExcludesAll string `validate:"excludesall=!@#$"`
ExcludesRune string `validate:"excludesrune=☻"` ExcludesRune string `validate:"excludesrune=☻"`
EndsWith string `validate:"endswith=end"`
StartsWith string `validate:"startswith=start"`
ISBN string `validate:"isbn"` ISBN string `validate:"isbn"`
ISBN10 string `validate:"isbn10"` ISBN10 string `validate:"isbn10"`
ISBN13 string `validate:"isbn13"` ISBN13 string `validate:"isbn13"`
@ -139,7 +145,15 @@ func TestTranslations(t *testing.T) {
StrPtrGte *string `validate:"gte=10"` StrPtrGte *string `validate:"gte=10"`
OneOfString string `validate:"oneof=red green"` OneOfString string `validate:"oneof=red green"`
OneOfInt int `validate:"oneof=5 63"` OneOfInt int `validate:"oneof=5 63"`
UniqueSlice []string `validate:"unique"`
UniqueArray [3]string `validate:"unique"`
UniqueMap map[string]string `validate:"unique"`
JSONString string `validate:"json"`
JWTString string `validate:"jwt"`
LowercaseString string `validate:"lowercase"`
UppercaseString string `validate:"uppercase"`
Datetime string `validate:"datetime=2006-01-02"` Datetime string `validate:"datetime=2006-01-02"`
BooleanString string `validate:"boolean"`
} }
var test Test var test Test
@ -170,6 +184,8 @@ func TestTranslations(t *testing.T) {
test.AlphaString = "abc3" test.AlphaString = "abc3"
test.AlphanumString = "abc3!" test.AlphanumString = "abc3!"
test.AlphanumUnicodeString = "abc3啊!"
test.AlphaUnicodeString = "abc3啊"
test.NumericString = "12E.00" test.NumericString = "12E.00"
test.NumberString = "12E" test.NumberString = "12E"
@ -177,6 +193,9 @@ func TestTranslations(t *testing.T) {
test.ExcludesAll = "This is Great!" test.ExcludesAll = "This is Great!"
test.ExcludesRune = "Love it ☻" test.ExcludesRune = "Love it ☻"
test.EndsWith = "this is some test text"
test.StartsWith = "this is some test text"
test.ASCII = "カタカナ" test.ASCII = "カタカナ"
test.PrintableASCII = "カタカナ" test.PrintableASCII = "カタカナ"
@ -186,7 +205,16 @@ func TestTranslations(t *testing.T) {
test.StrPtrMaxLen = &s test.StrPtrMaxLen = &s
test.StrPtrLen = &s test.StrPtrLen = &s
test.Datetime = "2008-Feb-01" test.JSONString = "{\"foo\":\"bar\",}"
test.LowercaseString = "ABCDEFG"
test.UppercaseString = "abcdefg"
test.UniqueSlice = []string{"1234", "1234"}
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
test.Datetime = "20060102"
test.BooleanString = "A"
err = validate.Struct(test) err = validate.Struct(test)
NotEqual(t, err, nil) NotEqual(t, err, nil)
@ -326,6 +354,14 @@ func TestTranslations(t *testing.T) {
ns: "Test.ISBN13", ns: "Test.ISBN13",
expected: "ISBN13必須是一個有效的ISBN-13編號", expected: "ISBN13必須是一個有效的ISBN-13編號",
}, },
{
ns: "Test.EndsWith",
expected: "EndsWith必須以文本'end'結尾",
},
{
ns: "Test.StartsWith",
expected: "StartsWith必須以文本'start'開頭",
},
{ {
ns: "Test.Excludes", ns: "Test.Excludes",
expected: "Excludes不能包含文字'text'", expected: "Excludes不能包含文字'text'",
@ -338,6 +374,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.ExcludesRune", ns: "Test.ExcludesRune",
expected: "ExcludesRune不能包含'☻'", expected: "ExcludesRune不能包含'☻'",
}, },
{
ns: "Test.ContainsRune",
expected: "ContainsRune必須包含字元'☻'",
},
{ {
ns: "Test.ContainsAny", ns: "Test.ContainsAny",
expected: "ContainsAny必須包含至少一個以下字元'!@#$'", expected: "ContainsAny必須包含至少一個以下字元'!@#$'",
@ -350,6 +390,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.Base64", ns: "Test.Base64",
expected: "Base64必須是一個有效的Base64字元串", expected: "Base64必須是一個有效的Base64字元串",
}, },
{
ns: "Test.E164",
expected: "E164必須是一個有效的E.164手機號",
},
{ {
ns: "Test.Email", ns: "Test.Email",
expected: "Email必須是一個有效的信箱", expected: "Email必須是一個有效的信箱",
@ -394,6 +438,14 @@ func TestTranslations(t *testing.T) {
ns: "Test.NumericString", ns: "Test.NumericString",
expected: "NumericString必須是一個有效的數值", expected: "NumericString必須是一個有效的數值",
}, },
{
ns: "Test.AlphaUnicodeString",
expected: "AlphaUnicodeString只能包含字母和Unicode字元",
},
{
ns: "Test.AlphanumUnicodeString",
expected: "AlphanumUnicodeString只能包含字母數字和Unicode字元",
},
{ {
ns: "Test.AlphanumString", ns: "Test.AlphanumString",
expected: "AlphanumString只能包含字母和數字", expected: "AlphanumString只能包含字母和數字",
@ -622,9 +674,41 @@ func TestTranslations(t *testing.T) {
ns: "Test.OneOfInt", ns: "Test.OneOfInt",
expected: "OneOfInt必須是[5 63]中的一個", expected: "OneOfInt必須是[5 63]中的一個",
}, },
{
ns: "Test.UniqueSlice",
expected: "UniqueSlice字段內的值必須唯一",
},
{
ns: "Test.UniqueArray",
expected: "UniqueArray字段內的值必須唯一",
},
{
ns: "Test.UniqueMap",
expected: "UniqueMap字段內的值必須唯一",
},
{
ns: "Test.JSONString",
expected: "JSONString必須是一個有效的JSON字符串",
},
{
ns: "Test.JWTString",
expected: "JWTString必須是一個有效的JWT字符串",
},
{
ns: "Test.LowercaseString",
expected: "LowercaseString必須是小寫字母",
},
{
ns: "Test.UppercaseString",
expected: "UppercaseString必須是大寫字母",
},
{ {
ns: "Test.Datetime", ns: "Test.Datetime",
expected: "Datetime與2006-01-02格式不匹配", expected: "Datetime的格式必須是2006-01-02",
},
{
ns: "Test.BooleanString",
expected: "BooleanString必須是一個有效的布爾值",
}, },
} }

Loading…
Cancel
Save