1 package zh
2
3 import (
4 "fmt"
5 "log"
6 "reflect"
7 "strconv"
8 "strings"
9 "time"
10
11 "github.com/go-playground/locales"
12 ut "github.com/go-playground/universal-translator"
13 "github.com/go-playground/validator/v10"
14 )
15
16
17
18 func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) {
19
20 translations := []struct {
21 tag string
22 translation string
23 override bool
24 customRegisFunc validator.RegisterTranslationsFunc
25 customTransFunc validator.TranslationFunc
26 }{
27 {
28 tag: "required",
29 translation: "{0}为必填字段",
30 override: false,
31 },
32 {
33 tag: "required_if",
34 translation: "{0}为必填字段",
35 override: false,
36 },
37 {
38 tag: "required_unless",
39 translation: "{0}为必填字段",
40 override: false,
41 },
42 {
43 tag: "required_with",
44 translation: "{0}为必填字段",
45 override: false,
46 },
47 {
48 tag: "required_with_all",
49 translation: "{0}为必填字段",
50 override: false,
51 },
52 {
53 tag: "required_without",
54 translation: "{0}为必填字段",
55 override: false,
56 },
57 {
58 tag: "required_without_all",
59 translation: "{0}为必填字段",
60 override: false,
61 },
62 {
63 tag: "excluded_if",
64 translation: "{0}为禁填字段",
65 override: false,
66 },
67 {
68 tag: "excluded_unless",
69 translation: "{0}为禁填字段",
70 override: false,
71 },
72 {
73 tag: "excluded_with",
74 translation: "{0}为禁填字段",
75 override: false,
76 },
77 {
78 tag: "excluded_with_all",
79 translation: "{0}为禁填字段",
80 override: false,
81 },
82 {
83 tag: "excluded_without",
84 translation: "{0}为禁填字段",
85 override: false,
86 },
87 {
88 tag: "excluded_without_all",
89 translation: "{0}为禁填字段",
90 override: false,
91 },
92 {
93 tag: "isdefault",
94 translation: "{0}为禁填字段",
95 override: false,
96 },
97 {
98 tag: "len",
99 customRegisFunc: func(ut ut.Translator) (err error) {
100
101 if err = ut.Add("len-string", "{0}长度必须是{1}", false); err != nil {
102 return
103 }
104
105
106
107
108
109 if err = ut.AddCardinal("len-string-character", "{0}个字符", locales.PluralRuleOther, false); err != nil {
110 return
111 }
112
113 if err = ut.Add("len-number", "{0}必须等于{1}", false); err != nil {
114 return
115 }
116
117 if err = ut.Add("len-items", "{0}必须包含{1}", false); err != nil {
118 return
119 }
120
121
122
123
124 if err = ut.AddCardinal("len-items-item", "{0}项", locales.PluralRuleOther, false); err != nil {
125 return
126 }
127
128 return
129
130 },
131 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
132
133 var err error
134 var t string
135
136 var digits uint64
137 var kind reflect.Kind
138
139 if idx := strings.Index(fe.Param(), "."); idx != -1 {
140 digits = uint64(len(fe.Param()[idx+1:]))
141 }
142
143 f64, err := strconv.ParseFloat(fe.Param(), 64)
144 if err != nil {
145 goto END
146 }
147
148 kind = fe.Kind()
149 if kind == reflect.Ptr {
150 kind = fe.Type().Elem().Kind()
151 }
152
153 switch kind {
154 case reflect.String:
155
156 var c string
157
158 c, err = ut.C("len-string-character", f64, digits, ut.FmtNumber(f64, digits))
159 if err != nil {
160 goto END
161 }
162
163 t, err = ut.T("len-string", fe.Field(), c)
164
165 case reflect.Slice, reflect.Map, reflect.Array:
166 var c string
167
168 c, err = ut.C("len-items-item", f64, digits, ut.FmtNumber(f64, digits))
169 if err != nil {
170 goto END
171 }
172
173 t, err = ut.T("len-items", fe.Field(), c)
174
175 default:
176 t, err = ut.T("len-number", fe.Field(), ut.FmtNumber(f64, digits))
177 }
178
179 END:
180 if err != nil {
181 fmt.Printf("警告: 翻译字段错误: %s", err)
182 return fe.(error).Error()
183 }
184
185 return t
186 },
187 },
188 {
189 tag: "min",
190 customRegisFunc: func(ut ut.Translator) (err error) {
191
192 if err = ut.Add("min-string", "{0}长度必须至少为{1}", false); err != nil {
193 return
194 }
195
196
197
198
199
200 if err = ut.AddCardinal("min-string-character", "{0}个字符", locales.PluralRuleOther, false); err != nil {
201 return
202 }
203
204 if err = ut.Add("min-number", "{0}最小只能为{1}", false); err != nil {
205 return
206 }
207
208 if err = ut.Add("min-items", "{0}必须至少包含{1}", false); err != nil {
209 return
210 }
211
212
213
214
215 if err = ut.AddCardinal("min-items-item", "{0}项", locales.PluralRuleOther, false); err != nil {
216 return
217 }
218
219 return
220
221 },
222 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
223
224 var err error
225 var t string
226
227 var digits uint64
228 var kind reflect.Kind
229
230 if idx := strings.Index(fe.Param(), "."); idx != -1 {
231 digits = uint64(len(fe.Param()[idx+1:]))
232 }
233
234 f64, err := strconv.ParseFloat(fe.Param(), 64)
235 if err != nil {
236 goto END
237 }
238
239 kind = fe.Kind()
240 if kind == reflect.Ptr {
241 kind = fe.Type().Elem().Kind()
242 }
243
244 switch kind {
245 case reflect.String:
246
247 var c string
248
249 c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits))
250 if err != nil {
251 goto END
252 }
253
254 t, err = ut.T("min-string", fe.Field(), c)
255
256 case reflect.Slice, reflect.Map, reflect.Array:
257 var c string
258
259 c, err = ut.C("min-items-item", f64, digits, ut.FmtNumber(f64, digits))
260 if err != nil {
261 goto END
262 }
263
264 t, err = ut.T("min-items", fe.Field(), c)
265
266 default:
267 t, err = ut.T("min-number", fe.Field(), ut.FmtNumber(f64, digits))
268 }
269
270 END:
271 if err != nil {
272 fmt.Printf("警告: 翻译字段错误: %s", err)
273 return fe.(error).Error()
274 }
275
276 return t
277 },
278 },
279 {
280 tag: "max",
281 customRegisFunc: func(ut ut.Translator) (err error) {
282
283 if err = ut.Add("max-string", "{0}长度不能超过{1}", false); err != nil {
284 return
285 }
286
287
288
289
290
291 if err = ut.AddCardinal("max-string-character", "{0}个字符", locales.PluralRuleOther, false); err != nil {
292 return
293 }
294
295 if err = ut.Add("max-number", "{0}必须小于或等于{1}", false); err != nil {
296 return
297 }
298
299 if err = ut.Add("max-items", "{0}最多只能包含{1}", false); err != nil {
300 return
301 }
302
303
304
305
306 if err = ut.AddCardinal("max-items-item", "{0}项", locales.PluralRuleOther, false); err != nil {
307 return
308 }
309
310 return
311
312 },
313 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
314
315 var err error
316 var t string
317
318 var digits uint64
319 var kind reflect.Kind
320
321 if idx := strings.Index(fe.Param(), "."); idx != -1 {
322 digits = uint64(len(fe.Param()[idx+1:]))
323 }
324
325 f64, err := strconv.ParseFloat(fe.Param(), 64)
326 if err != nil {
327 goto END
328 }
329
330 kind = fe.Kind()
331 if kind == reflect.Ptr {
332 kind = fe.Type().Elem().Kind()
333 }
334
335 switch kind {
336 case reflect.String:
337
338 var c string
339
340 c, err = ut.C("max-string-character", f64, digits, ut.FmtNumber(f64, digits))
341 if err != nil {
342 goto END
343 }
344
345 t, err = ut.T("max-string", fe.Field(), c)
346
347 case reflect.Slice, reflect.Map, reflect.Array:
348 var c string
349
350 c, err = ut.C("max-items-item", f64, digits, ut.FmtNumber(f64, digits))
351 if err != nil {
352 goto END
353 }
354
355 t, err = ut.T("max-items", fe.Field(), c)
356
357 default:
358 t, err = ut.T("max-number", fe.Field(), ut.FmtNumber(f64, digits))
359 }
360
361 END:
362 if err != nil {
363 fmt.Printf("警告: 翻译字段错误: %s", err)
364 return fe.(error).Error()
365 }
366
367 return t
368 },
369 },
370 {
371 tag: "eq",
372 translation: "{0}不等于{1}",
373 override: false,
374 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
375
376 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
377 if err != nil {
378 fmt.Printf("警告: 翻译字段错误: %#v", fe)
379 return fe.(error).Error()
380 }
381
382 return t
383 },
384 },
385 {
386 tag: "ne",
387 translation: "{0}不能等于{1}",
388 override: false,
389 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
390
391 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
392 if err != nil {
393 fmt.Printf("警告: 翻译字段错误: %#v", fe)
394 return fe.(error).Error()
395 }
396
397 return t
398 },
399 },
400 {
401 tag: "lt",
402 customRegisFunc: func(ut ut.Translator) (err error) {
403
404 if err = ut.Add("lt-string", "{0}长度必须小于{1}", false); err != nil {
405 return
406 }
407
408
409
410
411
412 if err = ut.AddCardinal("lt-string-character", "{0}个字符", locales.PluralRuleOther, false); err != nil {
413 return
414 }
415
416 if err = ut.Add("lt-number", "{0}必须小于{1}", false); err != nil {
417 return
418 }
419
420 if err = ut.Add("lt-items", "{0}必须包含少于{1}", false); err != nil {
421 return
422 }
423
424
425
426
427
428 if err = ut.AddCardinal("lt-items-item", "{0}项", locales.PluralRuleOther, false); err != nil {
429 return
430 }
431
432 if err = ut.Add("lt-datetime", "{0}必须小于当前日期和时间", false); err != nil {
433 return
434 }
435
436 return
437
438 },
439 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
440
441 var err error
442 var t string
443 var f64 float64
444 var digits uint64
445 var kind reflect.Kind
446
447 fn := func() (err error) {
448
449 if idx := strings.Index(fe.Param(), "."); idx != -1 {
450 digits = uint64(len(fe.Param()[idx+1:]))
451 }
452
453 f64, err = strconv.ParseFloat(fe.Param(), 64)
454
455 return
456 }
457
458 kind = fe.Kind()
459 if kind == reflect.Ptr {
460 kind = fe.Type().Elem().Kind()
461 }
462
463 switch kind {
464 case reflect.String:
465
466 var c string
467
468 err = fn()
469 if err != nil {
470 goto END
471 }
472
473 c, err = ut.C("lt-string-character", f64, digits, ut.FmtNumber(f64, digits))
474 if err != nil {
475 goto END
476 }
477
478 t, err = ut.T("lt-string", fe.Field(), c)
479
480 case reflect.Slice, reflect.Map, reflect.Array:
481 var c string
482
483 err = fn()
484 if err != nil {
485 goto END
486 }
487
488 c, err = ut.C("lt-items-item", f64, digits, ut.FmtNumber(f64, digits))
489 if err != nil {
490 goto END
491 }
492
493 t, err = ut.T("lt-items", fe.Field(), c)
494
495 case reflect.Struct:
496 if fe.Type() != reflect.TypeOf(time.Time{}) {
497 err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
498 } else {
499 t, err = ut.T("lt-datetime", fe.Field())
500 }
501
502 default:
503 err = fn()
504 if err != nil {
505 goto END
506 }
507
508 t, err = ut.T("lt-number", fe.Field(), ut.FmtNumber(f64, digits))
509 }
510
511 END:
512 if err != nil {
513 fmt.Printf("警告: 翻译字段错误: %s", err)
514 return fe.(error).Error()
515 }
516
517 return t
518 },
519 },
520 {
521 tag: "lte",
522 customRegisFunc: func(ut ut.Translator) (err error) {
523
524 if err = ut.Add("lte-string", "{0}长度不能超过{1}", false); err != nil {
525 return
526 }
527
528
529
530
531
532 if err = ut.AddCardinal("lte-string-character", "{0}个字符", locales.PluralRuleOther, false); err != nil {
533 return
534 }
535
536 if err = ut.Add("lte-number", "{0}必须小于或等于{1}", false); err != nil {
537 return
538 }
539
540 if err = ut.Add("lte-items", "{0}最多只能包含{1}", false); err != nil {
541 return
542 }
543
544
545
546
547
548 if err = ut.AddCardinal("lte-items-item", "{0}项", locales.PluralRuleOther, false); err != nil {
549 return
550 }
551
552 if err = ut.Add("lte-datetime", "{0}必须小于或等于当前日期和时间", false); err != nil {
553 return
554 }
555
556 return
557 },
558 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
559
560 var err error
561 var t string
562 var f64 float64
563 var digits uint64
564 var kind reflect.Kind
565
566 fn := func() (err error) {
567
568 if idx := strings.Index(fe.Param(), "."); idx != -1 {
569 digits = uint64(len(fe.Param()[idx+1:]))
570 }
571
572 f64, err = strconv.ParseFloat(fe.Param(), 64)
573
574 return
575 }
576
577 kind = fe.Kind()
578 if kind == reflect.Ptr {
579 kind = fe.Type().Elem().Kind()
580 }
581
582 switch kind {
583 case reflect.String:
584
585 var c string
586
587 err = fn()
588 if err != nil {
589 goto END
590 }
591
592 c, err = ut.C("lte-string-character", f64, digits, ut.FmtNumber(f64, digits))
593 if err != nil {
594 goto END
595 }
596
597 t, err = ut.T("lte-string", fe.Field(), c)
598
599 case reflect.Slice, reflect.Map, reflect.Array:
600 var c string
601
602 err = fn()
603 if err != nil {
604 goto END
605 }
606
607 c, err = ut.C("lte-items-item", f64, digits, ut.FmtNumber(f64, digits))
608 if err != nil {
609 goto END
610 }
611
612 t, err = ut.T("lte-items", fe.Field(), c)
613
614 case reflect.Struct:
615 if fe.Type() != reflect.TypeOf(time.Time{}) {
616 err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
617 } else {
618 t, err = ut.T("lte-datetime", fe.Field())
619 }
620
621 default:
622 err = fn()
623 if err != nil {
624 goto END
625 }
626
627 t, err = ut.T("lte-number", fe.Field(), ut.FmtNumber(f64, digits))
628 }
629
630 END:
631 if err != nil {
632 fmt.Printf("警告: 翻译字段错误: %s", err)
633 return fe.(error).Error()
634 }
635
636 return t
637 },
638 },
639 {
640 tag: "gt",
641 customRegisFunc: func(ut ut.Translator) (err error) {
642
643 if err = ut.Add("gt-string", "{0}长度必须大于{1}", false); err != nil {
644 return
645 }
646
647
648
649
650
651 if err = ut.AddCardinal("gt-string-character", "{0}个字符", locales.PluralRuleOther, false); err != nil {
652 return
653 }
654
655 if err = ut.Add("gt-number", "{0}必须大于{1}", false); err != nil {
656 return
657 }
658
659 if err = ut.Add("gt-items", "{0}必须大于{1}", false); err != nil {
660 return
661 }
662
663
664
665
666
667 if err = ut.AddCardinal("gt-items-item", "{0}项", locales.PluralRuleOther, false); err != nil {
668 return
669 }
670
671 if err = ut.Add("gt-datetime", "{0}必须大于当前日期和时间", false); err != nil {
672 return
673 }
674
675 return
676 },
677 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
678
679 var err error
680 var t string
681 var f64 float64
682 var digits uint64
683 var kind reflect.Kind
684
685 fn := func() (err error) {
686
687 if idx := strings.Index(fe.Param(), "."); idx != -1 {
688 digits = uint64(len(fe.Param()[idx+1:]))
689 }
690
691 f64, err = strconv.ParseFloat(fe.Param(), 64)
692
693 return
694 }
695
696 kind = fe.Kind()
697 if kind == reflect.Ptr {
698 kind = fe.Type().Elem().Kind()
699 }
700
701 switch kind {
702 case reflect.String:
703
704 var c string
705
706 err = fn()
707 if err != nil {
708 goto END
709 }
710
711 c, err = ut.C("gt-string-character", f64, digits, ut.FmtNumber(f64, digits))
712 if err != nil {
713 goto END
714 }
715
716 t, err = ut.T("gt-string", fe.Field(), c)
717
718 case reflect.Slice, reflect.Map, reflect.Array:
719 var c string
720
721 err = fn()
722 if err != nil {
723 goto END
724 }
725
726 c, err = ut.C("gt-items-item", f64, digits, ut.FmtNumber(f64, digits))
727 if err != nil {
728 goto END
729 }
730
731 t, err = ut.T("gt-items", fe.Field(), c)
732
733 case reflect.Struct:
734 if fe.Type() != reflect.TypeOf(time.Time{}) {
735 err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
736 } else {
737
738 t, err = ut.T("gt-datetime", fe.Field())
739 }
740
741 default:
742 err = fn()
743 if err != nil {
744 goto END
745 }
746
747 t, err = ut.T("gt-number", fe.Field(), ut.FmtNumber(f64, digits))
748 }
749
750 END:
751 if err != nil {
752 fmt.Printf("警告: 翻译字段错误: %s", err)
753 return fe.(error).Error()
754 }
755
756 return t
757 },
758 },
759 {
760 tag: "gte",
761 customRegisFunc: func(ut ut.Translator) (err error) {
762
763 if err = ut.Add("gte-string", "{0}长度必须至少为{1}", false); err != nil {
764 return
765 }
766
767
768
769
770
771 if err = ut.AddCardinal("gte-string-character", "{0}个字符", locales.PluralRuleOther, false); err != nil {
772 return
773 }
774
775 if err = ut.Add("gte-number", "{0}必须大于或等于{1}", false); err != nil {
776 return
777 }
778
779 if err = ut.Add("gte-items", "{0}必须至少包含{1}", false); err != nil {
780 return
781 }
782
783
784
785
786
787 if err = ut.AddCardinal("gte-items-item", "{0}项", locales.PluralRuleOther, false); err != nil {
788 return
789 }
790
791 if err = ut.Add("gte-datetime", "{0}必须大于或等于当前日期和时间", false); err != nil {
792 return
793 }
794
795 return
796 },
797 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
798
799 var err error
800 var t string
801 var f64 float64
802 var digits uint64
803 var kind reflect.Kind
804
805 fn := func() (err error) {
806
807 if idx := strings.Index(fe.Param(), "."); idx != -1 {
808 digits = uint64(len(fe.Param()[idx+1:]))
809 }
810
811 f64, err = strconv.ParseFloat(fe.Param(), 64)
812
813 return
814 }
815
816 kind = fe.Kind()
817 if kind == reflect.Ptr {
818 kind = fe.Type().Elem().Kind()
819 }
820
821 switch kind {
822 case reflect.String:
823
824 var c string
825
826 err = fn()
827 if err != nil {
828 goto END
829 }
830
831 c, err = ut.C("gte-string-character", f64, digits, ut.FmtNumber(f64, digits))
832 if err != nil {
833 goto END
834 }
835
836 t, err = ut.T("gte-string", fe.Field(), c)
837
838 case reflect.Slice, reflect.Map, reflect.Array:
839 var c string
840
841 err = fn()
842 if err != nil {
843 goto END
844 }
845
846 c, err = ut.C("gte-items-item", f64, digits, ut.FmtNumber(f64, digits))
847 if err != nil {
848 goto END
849 }
850
851 t, err = ut.T("gte-items", fe.Field(), c)
852
853 case reflect.Struct:
854 if fe.Type() != reflect.TypeOf(time.Time{}) {
855 err = fmt.Errorf("tag '%s'不能用于struct类型.", fe.Tag())
856 } else {
857 t, err = ut.T("gte-datetime", fe.Field())
858 }
859
860 default:
861 err = fn()
862 if err != nil {
863 goto END
864 }
865
866 t, err = ut.T("gte-number", fe.Field(), ut.FmtNumber(f64, digits))
867 }
868
869 END:
870 if err != nil {
871 fmt.Printf("警告: 翻译字段错误: %s", err)
872 return fe.(error).Error()
873 }
874
875 return t
876 },
877 },
878 {
879 tag: "eqfield",
880 translation: "{0}必须等于{1}",
881 override: false,
882 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
883
884 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
885 if err != nil {
886 log.Printf("警告: 翻译字段错误: %#v", fe)
887 return fe.(error).Error()
888 }
889
890 return t
891 },
892 },
893 {
894 tag: "eqcsfield",
895 translation: "{0}必须等于{1}",
896 override: false,
897 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
898
899 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
900 if err != nil {
901 log.Printf("警告: 翻译字段错误: %#v", fe)
902 return fe.(error).Error()
903 }
904
905 return t
906 },
907 },
908 {
909 tag: "necsfield",
910 translation: "{0}不能等于{1}",
911 override: false,
912 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
913
914 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
915 if err != nil {
916 log.Printf("警告: 翻译字段错误: %#v", fe)
917 return fe.(error).Error()
918 }
919
920 return t
921 },
922 },
923 {
924 tag: "gtcsfield",
925 translation: "{0}必须大于{1}",
926 override: false,
927 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
928
929 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
930 if err != nil {
931 log.Printf("警告: 翻译字段错误: %#v", fe)
932 return fe.(error).Error()
933 }
934
935 return t
936 },
937 },
938 {
939 tag: "gtecsfield",
940 translation: "{0}必须大于或等于{1}",
941 override: false,
942 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
943
944 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
945 if err != nil {
946 log.Printf("警告: 翻译字段错误: %#v", fe)
947 return fe.(error).Error()
948 }
949
950 return t
951 },
952 },
953 {
954 tag: "ltcsfield",
955 translation: "{0}必须小于{1}",
956 override: false,
957 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
958
959 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
960 if err != nil {
961 log.Printf("警告: 翻译字段错误: %#v", fe)
962 return fe.(error).Error()
963 }
964
965 return t
966 },
967 },
968 {
969 tag: "ltecsfield",
970 translation: "{0}必须小于或等于{1}",
971 override: false,
972 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
973
974 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
975 if err != nil {
976 log.Printf("警告: 翻译字段错误: %#v", fe)
977 return fe.(error).Error()
978 }
979
980 return t
981 },
982 },
983 {
984 tag: "nefield",
985 translation: "{0}不能等于{1}",
986 override: false,
987 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
988
989 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
990 if err != nil {
991 log.Printf("警告: 翻译字段错误: %#v", fe)
992 return fe.(error).Error()
993 }
994
995 return t
996 },
997 },
998 {
999 tag: "gtfield",
1000 translation: "{0}必须大于{1}",
1001 override: false,
1002 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1003
1004 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1005 if err != nil {
1006 log.Printf("警告: 翻译字段错误: %#v", fe)
1007 return fe.(error).Error()
1008 }
1009
1010 return t
1011 },
1012 },
1013 {
1014 tag: "gtefield",
1015 translation: "{0}必须大于或等于{1}",
1016 override: false,
1017 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1018
1019 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1020 if err != nil {
1021 log.Printf("警告: 翻译字段错误: %#v", fe)
1022 return fe.(error).Error()
1023 }
1024
1025 return t
1026 },
1027 },
1028 {
1029 tag: "ltfield",
1030 translation: "{0}必须小于{1}",
1031 override: false,
1032 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1033
1034 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1035 if err != nil {
1036 log.Printf("警告: 翻译字段错误: %#v", fe)
1037 return fe.(error).Error()
1038 }
1039
1040 return t
1041 },
1042 },
1043 {
1044 tag: "ltefield",
1045 translation: "{0}必须小于或等于{1}",
1046 override: false,
1047 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1048
1049 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1050 if err != nil {
1051 log.Printf("警告: 翻译字段错误: %#v", fe)
1052 return fe.(error).Error()
1053 }
1054
1055 return t
1056 },
1057 },
1058 {
1059 tag: "alpha",
1060 translation: "{0}只能包含字母",
1061 override: false,
1062 },
1063 {
1064 tag: "alphanum",
1065 translation: "{0}只能包含字母和数字",
1066 override: false,
1067 },
1068 {
1069 tag: "alphanumunicode",
1070 translation: "{0}只能包含字母数字和Unicode字符",
1071 override: false,
1072 },
1073 {
1074 tag: "alphaunicode",
1075 translation: "{0}只能包含字母和Unicode字符",
1076 override: false,
1077 },
1078 {
1079 tag: "numeric",
1080 translation: "{0}必须是一个有效的数值",
1081 override: false,
1082 },
1083 {
1084 tag: "number",
1085 translation: "{0}必须是一个有效的数字",
1086 override: false,
1087 },
1088 {
1089 tag: "hexadecimal",
1090 translation: "{0}必须是一个有效的十六进制",
1091 override: false,
1092 },
1093 {
1094 tag: "hexcolor",
1095 translation: "{0}必须是一个有效的十六进制颜色",
1096 override: false,
1097 },
1098 {
1099 tag: "rgb",
1100 translation: "{0}必须是一个有效的RGB颜色",
1101 override: false,
1102 },
1103 {
1104 tag: "rgba",
1105 translation: "{0}必须是一个有效的RGBA颜色",
1106 override: false,
1107 },
1108 {
1109 tag: "hsl",
1110 translation: "{0}必须是一个有效的HSL颜色",
1111 override: false,
1112 },
1113 {
1114 tag: "hsla",
1115 translation: "{0}必须是一个有效的HSLA颜色",
1116 override: false,
1117 },
1118 {
1119 tag: "email",
1120 translation: "{0}必须是一个有效的邮箱",
1121 override: false,
1122 },
1123 {
1124 tag: "url",
1125 translation: "{0}必须是一个有效的URL",
1126 override: false,
1127 },
1128 {
1129 tag: "uri",
1130 translation: "{0}必须是一个有效的URI",
1131 override: false,
1132 },
1133 {
1134 tag: "base64",
1135 translation: "{0}必须是一个有效的Base64字符串",
1136 override: false,
1137 },
1138 {
1139 tag: "contains",
1140 translation: "{0}必须包含文本'{1}'",
1141 override: false,
1142 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1143
1144 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1145 if err != nil {
1146 log.Printf("警告: 翻译字段错误: %#v", fe)
1147 return fe.(error).Error()
1148 }
1149
1150 return t
1151 },
1152 },
1153 {
1154 tag: "containsany",
1155 translation: "{0}必须包含至少一个以下字符'{1}'",
1156 override: false,
1157 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1158
1159 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1160 if err != nil {
1161 log.Printf("警告: 翻译字段错误: %#v", fe)
1162 return fe.(error).Error()
1163 }
1164
1165 return t
1166 },
1167 },
1168 {
1169 tag: "containsrune",
1170 translation: "{0}必须包含字符'{1}'",
1171 override: false,
1172 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1173
1174 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1175 if err != nil {
1176 log.Printf("警告: 翻译字段错误: %#v", fe)
1177 return fe.(error).Error()
1178 }
1179
1180 return t
1181 },
1182 },
1183 {
1184 tag: "excludes",
1185 translation: "{0}不能包含文本'{1}'",
1186 override: false,
1187 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1188
1189 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1190 if err != nil {
1191 log.Printf("警告: 翻译字段错误: %#v", fe)
1192 return fe.(error).Error()
1193 }
1194
1195 return t
1196 },
1197 },
1198 {
1199 tag: "excludesall",
1200 translation: "{0}不能包含以下任何字符'{1}'",
1201 override: false,
1202 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1203
1204 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1205 if err != nil {
1206 log.Printf("警告: 翻译字段错误: %#v", fe)
1207 return fe.(error).Error()
1208 }
1209
1210 return t
1211 },
1212 },
1213 {
1214 tag: "excludesrune",
1215 translation: "{0}不能包含'{1}'",
1216 override: false,
1217 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1218
1219 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1220 if err != nil {
1221 log.Printf("警告: 翻译字段错误: %#v", fe)
1222 return fe.(error).Error()
1223 }
1224
1225 return t
1226 },
1227 },
1228 {
1229 tag: "endswith",
1230 translation: "{0}必须以文本'{1}'结尾",
1231 override: false,
1232 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1233
1234 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1235 if err != nil {
1236 log.Printf("警告: 翻译字段错误: %#v", fe)
1237 return fe.(error).Error()
1238 }
1239
1240 return t
1241 },
1242 },
1243 {
1244 tag: "startswith",
1245 translation: "{0}必须以文本'{1}'开头",
1246 override: false,
1247 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1248
1249 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1250 if err != nil {
1251 log.Printf("警告: 翻译字段错误: %#v", fe)
1252 return fe.(error).Error()
1253 }
1254
1255 return t
1256 },
1257 },
1258 {
1259 tag: "isbn",
1260 translation: "{0}必须是一个有效的ISBN编号",
1261 override: false,
1262 },
1263 {
1264 tag: "isbn10",
1265 translation: "{0}必须是一个有效的ISBN-10编号",
1266 override: false,
1267 },
1268 {
1269 tag: "isbn13",
1270 translation: "{0}必须是一个有效的ISBN-13编号",
1271 override: false,
1272 },
1273 {
1274 tag: "issn",
1275 translation: "{0}必须是一个有效的ISSN编号",
1276 override: false,
1277 },
1278 {
1279 tag: "uuid",
1280 translation: "{0}必须是一个有效的UUID",
1281 override: false,
1282 },
1283 {
1284 tag: "uuid3",
1285 translation: "{0}必须是一个有效的V3 UUID",
1286 override: false,
1287 },
1288 {
1289 tag: "uuid4",
1290 translation: "{0}必须是一个有效的V4 UUID",
1291 override: false,
1292 },
1293 {
1294 tag: "uuid5",
1295 translation: "{0}必须是一个有效的V5 UUID",
1296 override: false,
1297 },
1298 {
1299 tag: "ulid",
1300 translation: "{0}必须是一个有效的ULID",
1301 override: false,
1302 },
1303 {
1304 tag: "ascii",
1305 translation: "{0}必须只包含ascii字符",
1306 override: false,
1307 },
1308 {
1309 tag: "printascii",
1310 translation: "{0}必须只包含可打印的ascii字符",
1311 override: false,
1312 },
1313 {
1314 tag: "multibyte",
1315 translation: "{0}必须包含多字节字符",
1316 override: false,
1317 },
1318 {
1319 tag: "datauri",
1320 translation: "{0}必须包含有效的数据URI",
1321 override: false,
1322 },
1323 {
1324 tag: "latitude",
1325 translation: "{0}必须包含有效的纬度坐标",
1326 override: false,
1327 },
1328 {
1329 tag: "longitude",
1330 translation: "{0}必须包含有效的经度坐标",
1331 override: false,
1332 },
1333 {
1334 tag: "ssn",
1335 translation: "{0}必须是一个有效的社会安全号码(SSN)",
1336 override: false,
1337 },
1338 {
1339 tag: "ipv4",
1340 translation: "{0}必须是一个有效的IPv4地址",
1341 override: false,
1342 },
1343 {
1344 tag: "ipv6",
1345 translation: "{0}必须是一个有效的IPv6地址",
1346 override: false,
1347 },
1348 {
1349 tag: "ip",
1350 translation: "{0}必须是一个有效的IP地址",
1351 override: false,
1352 },
1353 {
1354 tag: "cidr",
1355 translation: "{0}必须是一个有效的无类别域间路由(CIDR)",
1356 override: false,
1357 },
1358 {
1359 tag: "cidrv4",
1360 translation: "{0}必须是一个包含IPv4地址的有效无类别域间路由(CIDR)",
1361 override: false,
1362 },
1363 {
1364 tag: "cidrv6",
1365 translation: "{0}必须是一个包含IPv6地址的有效无类别域间路由(CIDR)",
1366 override: false,
1367 },
1368 {
1369 tag: "tcp_addr",
1370 translation: "{0}必须是一个有效的TCP地址",
1371 override: false,
1372 },
1373 {
1374 tag: "tcp4_addr",
1375 translation: "{0}必须是一个有效的IPv4 TCP地址",
1376 override: false,
1377 },
1378 {
1379 tag: "tcp6_addr",
1380 translation: "{0}必须是一个有效的IPv6 TCP地址",
1381 override: false,
1382 },
1383 {
1384 tag: "udp_addr",
1385 translation: "{0}必须是一个有效的UDP地址",
1386 override: false,
1387 },
1388 {
1389 tag: "udp4_addr",
1390 translation: "{0}必须是一个有效的IPv4 UDP地址",
1391 override: false,
1392 },
1393 {
1394 tag: "udp6_addr",
1395 translation: "{0}必须是一个有效的IPv6 UDP地址",
1396 override: false,
1397 },
1398 {
1399 tag: "ip_addr",
1400 translation: "{0}必须是一个有效的IP地址",
1401 override: false,
1402 },
1403 {
1404 tag: "ip4_addr",
1405 translation: "{0}必须是一个有效的IPv4地址",
1406 override: false,
1407 },
1408 {
1409 tag: "ip6_addr",
1410 translation: "{0}必须是一个有效的IPv6地址",
1411 override: false,
1412 },
1413 {
1414 tag: "unix_addr",
1415 translation: "{0}必须是一个有效的UNIX地址",
1416 override: false,
1417 },
1418 {
1419 tag: "mac",
1420 translation: "{0}必须是一个有效的MAC地址",
1421 override: false,
1422 },
1423 {
1424 tag: "iscolor",
1425 translation: "{0}必须是一个有效的颜色",
1426 override: false,
1427 },
1428 {
1429 tag: "oneof",
1430 translation: "{0}必须是[{1}]中的一个",
1431 override: false,
1432 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1433 s, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1434 if err != nil {
1435 log.Printf("警告: 翻译字段错误: %#v", fe)
1436 return fe.(error).Error()
1437 }
1438 return s
1439 },
1440 },
1441 {
1442 tag: "json",
1443 translation: "{0}必须是一个JSON字符串",
1444 override: false,
1445 },
1446 {
1447 tag: "lowercase",
1448 translation: "{0}必须是小写字母",
1449 override: false,
1450 },
1451 {
1452 tag: "uppercase",
1453 translation: "{0}必须是大写字母",
1454 override: false,
1455 },
1456 {
1457 tag: "datetime",
1458 translation: "{0}的格式必须是{1}",
1459 override: false,
1460 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1461
1462 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1463 if err != nil {
1464 log.Printf("警告: 翻译字段错误: %#v", fe)
1465 return fe.(error).Error()
1466 }
1467
1468 return t
1469 },
1470 },
1471 {
1472 tag: "image",
1473 translation: "{0} 必须是有效图像",
1474 override: false,
1475 },
1476 }
1477
1478 for _, t := range translations {
1479
1480 if t.customTransFunc != nil && t.customRegisFunc != nil {
1481
1482 err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc)
1483
1484 } else if t.customTransFunc != nil && t.customRegisFunc == nil {
1485
1486 err = v.RegisterTranslation(t.tag, trans, registrationFunc(t.tag, t.translation, t.override), t.customTransFunc)
1487
1488 } else if t.customTransFunc == nil && t.customRegisFunc != nil {
1489
1490 err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, translateFunc)
1491
1492 } else {
1493 err = v.RegisterTranslation(t.tag, trans, registrationFunc(t.tag, t.translation, t.override), translateFunc)
1494 }
1495
1496 if err != nil {
1497 return
1498 }
1499 }
1500
1501 return
1502 }
1503
1504 func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc {
1505
1506 return func(ut ut.Translator) (err error) {
1507
1508 if err = ut.Add(tag, translation, override); err != nil {
1509 return
1510 }
1511
1512 return
1513
1514 }
1515
1516 }
1517
1518 func translateFunc(ut ut.Translator, fe validator.FieldError) string {
1519
1520 t, err := ut.T(fe.Tag(), fe.Field())
1521 if err != nil {
1522 log.Printf("警告: 翻译字段错误: %#v", fe)
1523 return fe.(error).Error()
1524 }
1525
1526 return t
1527 }
1528
View as plain text