enhancement: add zh translations for tag alphaunicode,alphanumunicode,containsrune,startswith,endswith (#799)

pull/801/head
cokezhang 3 years ago committed by GitHub
parent 44c2696cbd
commit 61a982014e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 55
      translations/zh/zh.go
  2. 30
      translations/zh/zh_test.go

@ -1000,6 +1000,16 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0}只能包含字母和数字",
override: false,
},
{
tag: "alphanumunicode",
translation: "{0}只能包含字母数字和Unicode字符",
override: false,
},
{
tag: "alphaunicode",
translation: "{0}只能包含字母和Unicode字符",
override: false,
},
{
tag: "numeric",
translation: "{0}必须是一个有效的数值",
@ -1090,6 +1100,21 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
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",
translation: "{0}不能包含文本'{1}'",
@ -1135,6 +1160,36 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
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",
translation: "{0}必须是一个有效的ISBN编号",

@ -80,6 +80,8 @@ func TestTranslations(t *testing.T) {
LteFieldString string `validate:"ltefield=MaxString"`
AlphaString string `validate:"alpha"`
AlphanumString string `validate:"alphanum"`
AlphanumUnicodeString string `validate:"alphanumunicode"`
AlphaUnicodeString string `validate:"alphaunicode"`
NumericString string `validate:"numeric"`
NumberString string `validate:"number"`
HexadecimalString string `validate:"hexadecimal"`
@ -94,9 +96,12 @@ func TestTranslations(t *testing.T) {
Base64 string `validate:"base64"`
Contains string `validate:"contains=purpose"`
ContainsAny string `validate:"containsany=!@#$"`
ContainsRune string `validate:"containsrune=☻"`
Excludes string `validate:"excludes=text"`
ExcludesAll string `validate:"excludesall=!@#$"`
ExcludesRune string `validate:"excludesrune=☻"`
EndsWith string `validate:"endswith=end"`
StartsWith string `validate:"startswith=start"`
ISBN string `validate:"isbn"`
ISBN10 string `validate:"isbn10"`
ISBN13 string `validate:"isbn13"`
@ -172,6 +177,8 @@ func TestTranslations(t *testing.T) {
test.AlphaString = "abc3"
test.AlphanumString = "abc3!"
test.AlphanumUnicodeString = "abc3啊!"
test.AlphaUnicodeString = "abc3啊"
test.NumericString = "12E.00"
test.NumberString = "12E"
@ -179,6 +186,9 @@ func TestTranslations(t *testing.T) {
test.ExcludesAll = "This is Great!"
test.ExcludesRune = "Love it ☻"
test.EndsWith = "this is some test text"
test.StartsWith = "this is some test text"
test.ASCII = "カタカナ"
test.PrintableASCII = "カタカナ"
@ -329,6 +339,14 @@ func TestTranslations(t *testing.T) {
ns: "Test.ISBN13",
expected: "ISBN13必须是一个有效的ISBN-13编号",
},
{
ns: "Test.EndsWith",
expected: "EndsWith必须以文本'end'结尾",
},
{
ns: "Test.StartsWith",
expected: "StartsWith必须以文本'start'开头",
},
{
ns: "Test.Excludes",
expected: "Excludes不能包含文本'text'",
@ -341,6 +359,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.ExcludesRune",
expected: "ExcludesRune不能包含'☻'",
},
{
ns: "Test.ContainsRune",
expected: "ContainsRune必须包含字符'☻'",
},
{
ns: "Test.ContainsAny",
expected: "ContainsAny必须包含至少一个以下字符'!@#$'",
@ -397,6 +419,14 @@ func TestTranslations(t *testing.T) {
ns: "Test.NumericString",
expected: "NumericString必须是一个有效的数值",
},
{
ns: "Test.AlphaUnicodeString",
expected: "AlphaUnicodeString只能包含字母和Unicode字符",
},
{
ns: "Test.AlphanumUnicodeString",
expected: "AlphanumUnicodeString只能包含字母数字和Unicode字符",
},
{
ns: "Test.AlphanumString",
expected: "AlphanumString只能包含字母和数字",

Loading…
Cancel
Save