1 package lv
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 translations := []struct {
20 tag string
21 translation string
22 override bool
23 customRegisFunc validator.RegisterTranslationsFunc
24 customTransFunc validator.TranslationFunc
25 }{
26 {
27 tag: "required",
28 translation: "{0} ir obligāts lauks",
29 override: false,
30 },
31 {
32 tag: "required_if",
33 translation: "{0} ir obligāts lauks",
34 override: false,
35 },
36 {
37 tag: "len",
38 customRegisFunc: func(ut ut.Translator) (err error) {
39 if err = ut.Add("len-string", "{0} garumam jābūt {1}", false); err != nil {
40 return
41 }
42
43 if err = ut.AddCardinal("len-string-character", "{0} rakstu zīme", locales.PluralRuleOne, false); err != nil {
44 return
45 }
46
47 if err = ut.AddCardinal("len-string-character", "{0} rakstu zīmes", locales.PluralRuleOther, false); err != nil {
48 return
49 }
50
51 if err = ut.Add("len-number", "{0} vērtībai jābūt {1}", false); err != nil {
52 return
53 }
54
55 if err = ut.Add("len-items", "{0} vērtībai jāsatur {1}", false); err != nil {
56 return
57 }
58 if err = ut.AddCardinal("len-items-item", "{0} elements", locales.PluralRuleOne, false); err != nil {
59 return
60 }
61
62 if err = ut.AddCardinal("len-items-item", "{0} elementi", locales.PluralRuleOther, false); err != nil {
63 return
64 }
65
66 return
67 },
68 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
69 var err error
70 var t string
71
72 var digits uint64
73 var kind reflect.Kind
74
75 if idx := strings.Index(fe.Param(), "."); idx != -1 {
76 digits = uint64(len(fe.Param()[idx+1:]))
77 }
78
79 f64, err := strconv.ParseFloat(fe.Param(), 64)
80 if err != nil {
81 goto END
82 }
83
84 kind = fe.Kind()
85 if kind == reflect.Ptr {
86 kind = fe.Type().Elem().Kind()
87 }
88
89 switch kind {
90 case reflect.String:
91
92 var c string
93
94 c, err = ut.C("len-string-character", f64, digits, ut.FmtNumber(f64, digits))
95 if err != nil {
96 goto END
97 }
98
99 t, err = ut.T("len-string", fe.Field(), c)
100
101 case reflect.Slice, reflect.Map, reflect.Array:
102 var c string
103
104 c, err = ut.C("len-items-item", f64, digits, ut.FmtNumber(f64, digits))
105 if err != nil {
106 goto END
107 }
108
109 t, err = ut.T("len-items", fe.Field(), c)
110
111 default:
112 t, err = ut.T("len-number", fe.Field(), ut.FmtNumber(f64, digits))
113 }
114
115 END:
116 if err != nil {
117 fmt.Printf("warning: error translating FieldError: %s", err)
118 return fe.(error).Error()
119 }
120
121 return t
122 },
123 },
124 {
125 tag: "min",
126 customRegisFunc: func(ut ut.Translator) (err error) {
127 if err = ut.Add("min-string", "{0} garumam jābūt minimums {1}", false); err != nil {
128 return
129 }
130
131 if err = ut.AddCardinal("min-string-character", "{0} rakstu zīme", locales.PluralRuleOne, false); err != nil {
132 return
133 }
134
135 if err = ut.AddCardinal("min-string-character", "{0} rakstu zīmes", locales.PluralRuleOther, false); err != nil {
136 return
137 }
138
139 if err = ut.Add("min-number", "{0} vērtībai jābūt {1} vai lielākai", false); err != nil {
140 return
141 }
142
143 if err = ut.Add("min-items", "{0} jāsatur minimums {1}", false); err != nil {
144 return
145 }
146 if err = ut.AddCardinal("min-items-item", "{0} elements", locales.PluralRuleOne, false); err != nil {
147 return
148 }
149
150 if err = ut.AddCardinal("min-items-item", "{0} elementi", locales.PluralRuleOther, false); err != nil {
151 return
152 }
153
154 return
155 },
156 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
157 var err error
158 var t string
159
160 var digits uint64
161 var kind reflect.Kind
162
163 if idx := strings.Index(fe.Param(), "."); idx != -1 {
164 digits = uint64(len(fe.Param()[idx+1:]))
165 }
166
167 f64, err := strconv.ParseFloat(fe.Param(), 64)
168 if err != nil {
169 goto END
170 }
171
172 kind = fe.Kind()
173 if kind == reflect.Ptr {
174 kind = fe.Type().Elem().Kind()
175 }
176
177 switch kind {
178 case reflect.String:
179
180 var c string
181
182 c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits))
183 if err != nil {
184 goto END
185 }
186
187 t, err = ut.T("min-string", fe.Field(), c)
188
189 case reflect.Slice, reflect.Map, reflect.Array:
190 var c string
191
192 c, err = ut.C("min-items-item", f64, digits, ut.FmtNumber(f64, digits))
193 if err != nil {
194 goto END
195 }
196
197 t, err = ut.T("min-items", fe.Field(), c)
198
199 default:
200 t, err = ut.T("min-number", fe.Field(), ut.FmtNumber(f64, digits))
201 }
202
203 END:
204 if err != nil {
205 fmt.Printf("warning: error translating FieldError: %s", err)
206 return fe.(error).Error()
207 }
208
209 return t
210 },
211 },
212 {
213 tag: "max",
214 customRegisFunc: func(ut ut.Translator) (err error) {
215 if err = ut.Add("max-string", "{0} vērtība pārsniedz maksimālo garumu {1}", false); err != nil {
216 return
217 }
218
219 if err = ut.AddCardinal("max-string-character", "{0} rakstu zīme", locales.PluralRuleOne, false); err != nil {
220 return
221 }
222
223 if err = ut.AddCardinal("max-string-character", "{0} rakstu zīmes", locales.PluralRuleOther, false); err != nil {
224 return
225 }
226
227 if err = ut.Add("max-number", "{0} vērtībai jābūt {1} vai mazākai", false); err != nil {
228 return
229 }
230
231 if err = ut.Add("max-items", "{0} jāsatur maksimums {1}", false); err != nil {
232 return
233 }
234 if err = ut.AddCardinal("max-items-item", "{0} elements", locales.PluralRuleOne, false); err != nil {
235 return
236 }
237
238 if err = ut.AddCardinal("max-items-item", "{0} elementi", locales.PluralRuleOther, false); err != nil {
239 return
240 }
241
242 return
243 },
244 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
245 var err error
246 var t string
247
248 var digits uint64
249 var kind reflect.Kind
250
251 if idx := strings.Index(fe.Param(), "."); idx != -1 {
252 digits = uint64(len(fe.Param()[idx+1:]))
253 }
254
255 f64, err := strconv.ParseFloat(fe.Param(), 64)
256 if err != nil {
257 goto END
258 }
259
260 kind = fe.Kind()
261 if kind == reflect.Ptr {
262 kind = fe.Type().Elem().Kind()
263 }
264
265 switch kind {
266 case reflect.String:
267
268 var c string
269
270 c, err = ut.C("max-string-character", f64, digits, ut.FmtNumber(f64, digits))
271 if err != nil {
272 goto END
273 }
274
275 t, err = ut.T("max-string", fe.Field(), c)
276
277 case reflect.Slice, reflect.Map, reflect.Array:
278 var c string
279
280 c, err = ut.C("max-items-item", f64, digits, ut.FmtNumber(f64, digits))
281 if err != nil {
282 goto END
283 }
284
285 t, err = ut.T("max-items", fe.Field(), c)
286
287 default:
288 t, err = ut.T("max-number", fe.Field(), ut.FmtNumber(f64, digits))
289 }
290
291 END:
292 if err != nil {
293 fmt.Printf("warning: error translating FieldError: %s", err)
294 return fe.(error).Error()
295 }
296
297 return t
298 },
299 },
300 {
301 tag: "eq",
302 translation: "{0} nav vienāds ar {1}",
303 override: false,
304 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
305 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
306 if err != nil {
307 fmt.Printf("warning: error translating FieldError: %#v", fe)
308 return fe.(error).Error()
309 }
310
311 return t
312 },
313 },
314 {
315 tag: "ne",
316 translation: "{0} nedrīkst būt vienāds ar {1}",
317 override: false,
318 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
319 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
320 if err != nil {
321 fmt.Printf("warning: error translating FieldError: %#v", fe)
322 return fe.(error).Error()
323 }
324
325 return t
326 },
327 },
328 {
329 tag: "lt",
330 customRegisFunc: func(ut ut.Translator) (err error) {
331 if err = ut.Add("lt-string", "{0} garumam jābūt mazākam par {1}", false); err != nil {
332 return
333 }
334
335 if err = ut.AddCardinal("lt-string-character", "{0} rakstu zīmi", locales.PluralRuleOne, false); err != nil {
336 return
337 }
338
339 if err = ut.AddCardinal("lt-string-character", "{0} rakstu zīmēm", locales.PluralRuleOther, false); err != nil {
340 return
341 }
342
343 if err = ut.Add("lt-number", "{0} jābūt mazākam par {1}", false); err != nil {
344 return
345 }
346
347 if err = ut.Add("lt-items", "{0} jāsatur mazāk par {1}", false); err != nil {
348 return
349 }
350
351 if err = ut.AddCardinal("lt-items-item", "{0} elements", locales.PluralRuleOne, false); err != nil {
352 return
353 }
354
355 if err = ut.AddCardinal("lt-items-item", "{0} elementiem", locales.PluralRuleOther, false); err != nil {
356 return
357 }
358
359 if err = ut.Add("lt-datetime", "{0} jābūt mazākam par šī brīža Datumu un laiku", false); err != nil {
360 return
361 }
362
363 return
364 },
365 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
366 var err error
367 var t string
368 var f64 float64
369 var digits uint64
370 var kind reflect.Kind
371
372 fn := func() (err error) {
373 if idx := strings.Index(fe.Param(), "."); idx != -1 {
374 digits = uint64(len(fe.Param()[idx+1:]))
375 }
376
377 f64, err = strconv.ParseFloat(fe.Param(), 64)
378
379 return
380 }
381
382 kind = fe.Kind()
383 if kind == reflect.Ptr {
384 kind = fe.Type().Elem().Kind()
385 }
386
387 switch kind {
388 case reflect.String:
389
390 var c string
391
392 err = fn()
393 if err != nil {
394 goto END
395 }
396
397 c, err = ut.C("lt-string-character", f64, digits, ut.FmtNumber(f64, digits))
398 if err != nil {
399 goto END
400 }
401
402 t, err = ut.T("lt-string", fe.Field(), c)
403
404 case reflect.Slice, reflect.Map, reflect.Array:
405 var c string
406
407 err = fn()
408 if err != nil {
409 goto END
410 }
411
412 c, err = ut.C("lt-items-item", f64, digits, ut.FmtNumber(f64, digits))
413 if err != nil {
414 goto END
415 }
416
417 t, err = ut.T("lt-items", fe.Field(), c)
418
419 case reflect.Struct:
420 if fe.Type() != reflect.TypeOf(time.Time{}) {
421 err = fmt.Errorf("tag '%s' cannot be used on a struct type", fe.Tag())
422 goto END
423 }
424
425 t, err = ut.T("lt-datetime", fe.Field())
426
427 default:
428 err = fn()
429 if err != nil {
430 goto END
431 }
432
433 t, err = ut.T("lt-number", fe.Field(), ut.FmtNumber(f64, digits))
434 }
435
436 END:
437 if err != nil {
438 fmt.Printf("warning: error translating FieldError: %s", err)
439 return fe.(error).Error()
440 }
441
442 return t
443 },
444 },
445 {
446 tag: "lte",
447 customRegisFunc: func(ut ut.Translator) (err error) {
448 if err = ut.Add("lte-string", "{0} garumam jābūt maksimums {1}", false); err != nil {
449 return
450 }
451
452 if err = ut.AddCardinal("lte-string-character", "{0} rakstu zīme", locales.PluralRuleOne, false); err != nil {
453 return
454 }
455
456 if err = ut.AddCardinal("lte-string-character", "{0} rakstu zīmes", locales.PluralRuleOther, false); err != nil {
457 return
458 }
459
460 if err = ut.Add("lte-number", "{0} jābūt {1} vai mazākam", false); err != nil {
461 return
462 }
463
464 if err = ut.Add("lte-items", "{0} jāsatur maksimums {1}", false); err != nil {
465 return
466 }
467
468 if err = ut.AddCardinal("lte-items-item", "{0} elements", locales.PluralRuleOne, false); err != nil {
469 return
470 }
471
472 if err = ut.AddCardinal("lte-items-item", "{0} elementi", locales.PluralRuleOther, false); err != nil {
473 return
474 }
475
476 if err = ut.Add("lte-datetime", "{0} jābūt mazākam par šī brīža Datumu un laiku vai vienādam", false); err != nil {
477 return
478 }
479
480 return
481 },
482 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
483 var err error
484 var t string
485 var f64 float64
486 var digits uint64
487 var kind reflect.Kind
488
489 fn := func() (err error) {
490 if idx := strings.Index(fe.Param(), "."); idx != -1 {
491 digits = uint64(len(fe.Param()[idx+1:]))
492 }
493
494 f64, err = strconv.ParseFloat(fe.Param(), 64)
495
496 return
497 }
498
499 kind = fe.Kind()
500 if kind == reflect.Ptr {
501 kind = fe.Type().Elem().Kind()
502 }
503
504 switch kind {
505 case reflect.String:
506
507 var c string
508
509 err = fn()
510 if err != nil {
511 goto END
512 }
513
514 c, err = ut.C("lte-string-character", f64, digits, ut.FmtNumber(f64, digits))
515 if err != nil {
516 goto END
517 }
518
519 t, err = ut.T("lte-string", fe.Field(), c)
520
521 case reflect.Slice, reflect.Map, reflect.Array:
522 var c string
523
524 err = fn()
525 if err != nil {
526 goto END
527 }
528
529 c, err = ut.C("lte-items-item", f64, digits, ut.FmtNumber(f64, digits))
530 if err != nil {
531 goto END
532 }
533
534 t, err = ut.T("lte-items", fe.Field(), c)
535
536 case reflect.Struct:
537 if fe.Type() != reflect.TypeOf(time.Time{}) {
538 err = fmt.Errorf("tag '%s' cannot be used on a struct type", fe.Tag())
539 goto END
540 }
541
542 t, err = ut.T("lte-datetime", fe.Field())
543
544 default:
545 err = fn()
546 if err != nil {
547 goto END
548 }
549
550 t, err = ut.T("lte-number", fe.Field(), ut.FmtNumber(f64, digits))
551 }
552
553 END:
554 if err != nil {
555 fmt.Printf("warning: error translating FieldError: %s", err)
556 return fe.(error).Error()
557 }
558
559 return t
560 },
561 },
562 {
563 tag: "gt",
564 customRegisFunc: func(ut ut.Translator) (err error) {
565 if err = ut.Add("gt-string", "{0} ir jābūt garākam par {1}", false); err != nil {
566 return
567 }
568
569 if err = ut.AddCardinal("gt-string-character", "{0} rakstu zīme", locales.PluralRuleOne, false); err != nil {
570 return
571 }
572
573 if err = ut.AddCardinal("gt-string-character", "{0} rakstu zīmēm", locales.PluralRuleOther, false); err != nil {
574 return
575 }
576
577 if err = ut.Add("gt-number", "{0} jābūt lielākam par {1}", false); err != nil {
578 return
579 }
580
581 if err = ut.Add("gt-items", "{0} jāsatur vairāk par {1}", false); err != nil {
582 return
583 }
584
585 if err = ut.AddCardinal("gt-items-item", "{0} elements", locales.PluralRuleOne, false); err != nil {
586 return
587 }
588
589 if err = ut.AddCardinal("gt-items-item", "{0} elementiem", locales.PluralRuleOther, false); err != nil {
590 return
591 }
592
593 if err = ut.Add("gt-datetime", "{0} jābūt lielākam par šī brīža Datumu un laiku", false); err != nil {
594 return
595 }
596
597 return
598 },
599 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
600 var err error
601 var t string
602 var f64 float64
603 var digits uint64
604 var kind reflect.Kind
605
606 fn := func() (err error) {
607 if idx := strings.Index(fe.Param(), "."); idx != -1 {
608 digits = uint64(len(fe.Param()[idx+1:]))
609 }
610
611 f64, err = strconv.ParseFloat(fe.Param(), 64)
612
613 return
614 }
615
616 kind = fe.Kind()
617 if kind == reflect.Ptr {
618 kind = fe.Type().Elem().Kind()
619 }
620
621 switch kind {
622 case reflect.String:
623
624 var c string
625
626 err = fn()
627 if err != nil {
628 goto END
629 }
630
631 c, err = ut.C("gt-string-character", f64, digits, ut.FmtNumber(f64, digits))
632 if err != nil {
633 goto END
634 }
635
636 t, err = ut.T("gt-string", fe.Field(), c)
637
638 case reflect.Slice, reflect.Map, reflect.Array:
639 var c string
640
641 err = fn()
642 if err != nil {
643 goto END
644 }
645
646 c, err = ut.C("gt-items-item", f64, digits, ut.FmtNumber(f64, digits))
647 if err != nil {
648 goto END
649 }
650
651 t, err = ut.T("gt-items", fe.Field(), c)
652
653 case reflect.Struct:
654 if fe.Type() != reflect.TypeOf(time.Time{}) {
655 err = fmt.Errorf("tag '%s' cannot be used on a struct type", fe.Tag())
656 goto END
657 }
658
659 t, err = ut.T("gt-datetime", fe.Field())
660
661 default:
662 err = fn()
663 if err != nil {
664 goto END
665 }
666
667 t, err = ut.T("gt-number", fe.Field(), ut.FmtNumber(f64, digits))
668 }
669
670 END:
671 if err != nil {
672 fmt.Printf("warning: error translating FieldError: %s", err)
673 return fe.(error).Error()
674 }
675
676 return t
677 },
678 },
679 {
680 tag: "gte",
681 customRegisFunc: func(ut ut.Translator) (err error) {
682 if err = ut.Add("gte-string", "{0} garumam jābūt minimums {1}", false); err != nil {
683 return
684 }
685
686 if err = ut.AddCardinal("gte-string-character", "{0} rakstu zīme", locales.PluralRuleOne, false); err != nil {
687 return
688 }
689
690 if err = ut.AddCardinal("gte-string-character", "{0} rakstu zīmes", locales.PluralRuleOther, false); err != nil {
691 return
692 }
693
694 if err = ut.Add("gte-number", "{0} jābūt {1} vai lielākam", false); err != nil {
695 return
696 }
697
698 if err = ut.Add("gte-items", "{0} jāsatur minimums {1}", false); err != nil {
699 return
700 }
701
702 if err = ut.AddCardinal("gte-items-item", "{0} elements", locales.PluralRuleOne, false); err != nil {
703 return
704 }
705
706 if err = ut.AddCardinal("gte-items-item", "{0} elementi", locales.PluralRuleOther, false); err != nil {
707 return
708 }
709
710 if err = ut.Add("gte-datetime", "{0} jābūt lielākam par šī brīža Datumu un laiku vai vienādam", false); err != nil {
711 return
712 }
713
714 return
715 },
716 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
717 var err error
718 var t string
719 var f64 float64
720 var digits uint64
721 var kind reflect.Kind
722
723 fn := func() (err error) {
724 if idx := strings.Index(fe.Param(), "."); idx != -1 {
725 digits = uint64(len(fe.Param()[idx+1:]))
726 }
727
728 f64, err = strconv.ParseFloat(fe.Param(), 64)
729
730 return
731 }
732
733 kind = fe.Kind()
734 if kind == reflect.Ptr {
735 kind = fe.Type().Elem().Kind()
736 }
737
738 switch kind {
739 case reflect.String:
740
741 var c string
742
743 err = fn()
744 if err != nil {
745 goto END
746 }
747
748 c, err = ut.C("gte-string-character", f64, digits, ut.FmtNumber(f64, digits))
749 if err != nil {
750 goto END
751 }
752
753 t, err = ut.T("gte-string", fe.Field(), c)
754
755 case reflect.Slice, reflect.Map, reflect.Array:
756 var c string
757
758 err = fn()
759 if err != nil {
760 goto END
761 }
762
763 c, err = ut.C("gte-items-item", f64, digits, ut.FmtNumber(f64, digits))
764 if err != nil {
765 goto END
766 }
767
768 t, err = ut.T("gte-items", fe.Field(), c)
769
770 case reflect.Struct:
771 if fe.Type() != reflect.TypeOf(time.Time{}) {
772 err = fmt.Errorf("tag '%s' cannot be used on a struct type", fe.Tag())
773 goto END
774 }
775
776 t, err = ut.T("gte-datetime", fe.Field())
777
778 default:
779 err = fn()
780 if err != nil {
781 goto END
782 }
783
784 t, err = ut.T("gte-number", fe.Field(), ut.FmtNumber(f64, digits))
785 }
786
787 END:
788 if err != nil {
789 fmt.Printf("warning: error translating FieldError: %s", err)
790 return fe.(error).Error()
791 }
792
793 return t
794 },
795 },
796 {
797 tag: "eqfield",
798 translation: "{0} jābūt vienādam ar {1}",
799 override: false,
800 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
801 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
802 if err != nil {
803 log.Printf("warning: error translating FieldError: %#v", fe)
804 return fe.(error).Error()
805 }
806
807 return t
808 },
809 },
810 {
811 tag: "eqcsfield",
812 translation: "{0} jābūt vienādam ar {1}",
813 override: false,
814 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
815 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
816 if err != nil {
817 log.Printf("warning: error translating FieldError: %#v", fe)
818 return fe.(error).Error()
819 }
820
821 return t
822 },
823 },
824 {
825 tag: "necsfield",
826 translation: "{0} nedrīkst būt vienāds ar {1}",
827 override: false,
828 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
829 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
830 if err != nil {
831 log.Printf("warning: error translating FieldError: %#v", fe)
832 return fe.(error).Error()
833 }
834
835 return t
836 },
837 },
838 {
839 tag: "gtcsfield",
840 translation: "{0} jābūt lielākam par {1}",
841 override: false,
842 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
843 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
844 if err != nil {
845 log.Printf("warning: error translating FieldError: %#v", fe)
846 return fe.(error).Error()
847 }
848
849 return t
850 },
851 },
852 {
853 tag: "gtecsfield",
854 translation: "{0} jābūt lielākam par {1} vai vienādam",
855 override: false,
856 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
857 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
858 if err != nil {
859 log.Printf("warning: error translating FieldError: %#v", fe)
860 return fe.(error).Error()
861 }
862
863 return t
864 },
865 },
866 {
867 tag: "ltcsfield",
868 translation: "{0} jābūt mazākam par {1}",
869 override: false,
870 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
871 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
872 if err != nil {
873 log.Printf("warning: error translating FieldError: %#v", fe)
874 return fe.(error).Error()
875 }
876
877 return t
878 },
879 },
880 {
881 tag: "ltecsfield",
882 translation: "{0} jābūt mazākam par {1} vai vienādam",
883 override: false,
884 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
885 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
886 if err != nil {
887 log.Printf("warning: error translating FieldError: %#v", fe)
888 return fe.(error).Error()
889 }
890
891 return t
892 },
893 },
894 {
895 tag: "nefield",
896 translation: "{0} nedrīkst būt vienāds ar {1}",
897 override: false,
898 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
899 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
900 if err != nil {
901 log.Printf("warning: error translating FieldError: %#v", fe)
902 return fe.(error).Error()
903 }
904
905 return t
906 },
907 },
908 {
909 tag: "gtfield",
910 translation: "{0} jābūt lielākam par {1}",
911 override: false,
912 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
913 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
914 if err != nil {
915 log.Printf("warning: error translating FieldError: %#v", fe)
916 return fe.(error).Error()
917 }
918
919 return t
920 },
921 },
922 {
923 tag: "gtefield",
924 translation: "{0} jābūt lielākam par {1} vai vienādam",
925 override: false,
926 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
927 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
928 if err != nil {
929 log.Printf("warning: error translating FieldError: %#v", fe)
930 return fe.(error).Error()
931 }
932
933 return t
934 },
935 },
936 {
937 tag: "ltfield",
938 translation: "{0} jābūt mazākam par {1}",
939 override: false,
940 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
941 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
942 if err != nil {
943 log.Printf("warning: error translating FieldError: %#v", fe)
944 return fe.(error).Error()
945 }
946
947 return t
948 },
949 },
950 {
951 tag: "ltefield",
952 translation: "{0} jābūt mazākam par {1} vai vienādam",
953 override: false,
954 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
955 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
956 if err != nil {
957 log.Printf("warning: error translating FieldError: %#v", fe)
958 return fe.(error).Error()
959 }
960
961 return t
962 },
963 },
964 {
965 tag: "alpha",
966 translation: "{0} jāsatur tikai simboli no alfabēta",
967 override: false,
968 },
969 {
970 tag: "alphanum",
971 translation: "{0} jāsatur tikai simboli no alfabēta vai cipari (Alphanumeric)",
972 override: false,
973 },
974 {
975 tag: "numeric",
976 translation: "{0} jāsatur tikai cipari",
977 override: false,
978 },
979 {
980 tag: "number",
981 translation: "{0} jāsatur derīgs skaitlis",
982 override: false,
983 },
984 {
985 tag: "hexadecimal",
986 translation: "{0} jābūt heksadecimālam skaitlim",
987 override: false,
988 },
989 {
990 tag: "hexcolor",
991 translation: "{0} jābūt derīgai HEX krāsai",
992 override: false,
993 },
994 {
995 tag: "rgb",
996 translation: "{0} jābūt derīgai RGB krāsai",
997 override: false,
998 },
999 {
1000 tag: "rgba",
1001 translation: "{0} jābūt derīgai RGBA krāsai",
1002 override: false,
1003 },
1004 {
1005 tag: "hsl",
1006 translation: "{0} jābūt derīgai HSL krāsai",
1007 override: false,
1008 },
1009 {
1010 tag: "hsla",
1011 translation: "{0} jābūt derīgai HSLA krāsai",
1012 override: false,
1013 },
1014 {
1015 tag: "e164",
1016 translation: "{0} jābūt derīgam, pēc E.164 formatētam talruņa numuram",
1017 override: false,
1018 },
1019 {
1020 tag: "email",
1021 translation: "{0} jābūt derīgai e-pasta adresei",
1022 override: false,
1023 },
1024 {
1025 tag: "url",
1026 translation: "{0} jābūt derīgam URL",
1027 override: false,
1028 },
1029 {
1030 tag: "uri",
1031 translation: "{0} jābūt derīgam URI",
1032 override: false,
1033 },
1034 {
1035 tag: "base64",
1036 translation: "{0} jābūt derīgai Base64 virknei",
1037 override: false,
1038 },
1039 {
1040 tag: "contains",
1041 translation: "{0} jāsatur teksts '{1}'",
1042 override: false,
1043 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1044 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1045 if err != nil {
1046 log.Printf("warning: error translating FieldError: %#v", fe)
1047 return fe.(error).Error()
1048 }
1049
1050 return t
1051 },
1052 },
1053 {
1054 tag: "containsany",
1055 translation: "{0} jāsatur minimums 1 no rakstu zīmēm '{1}'",
1056 override: false,
1057 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1058 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1059 if err != nil {
1060 log.Printf("warning: error translating FieldError: %#v", fe)
1061 return fe.(error).Error()
1062 }
1063
1064 return t
1065 },
1066 },
1067 {
1068 tag: "excludes",
1069 translation: "{0} nedrīkst saturēt tekstu '{1}'",
1070 override: false,
1071 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1072 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1073 if err != nil {
1074 log.Printf("warning: error translating FieldError: %#v", fe)
1075 return fe.(error).Error()
1076 }
1077
1078 return t
1079 },
1080 },
1081 {
1082 tag: "excludesall",
1083 translation: "{0} nedrīkst saturēt nevienu no sekojošām rakstu zīmēm '{1}'",
1084 override: false,
1085 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1086 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1087 if err != nil {
1088 log.Printf("warning: error translating FieldError: %#v", fe)
1089 return fe.(error).Error()
1090 }
1091
1092 return t
1093 },
1094 },
1095 {
1096 tag: "excludesrune",
1097 translation: "{0} nedrīkst saturēt sekojošo '{1}'",
1098 override: false,
1099 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1100 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1101 if err != nil {
1102 log.Printf("warning: error translating FieldError: %#v", fe)
1103 return fe.(error).Error()
1104 }
1105
1106 return t
1107 },
1108 },
1109 {
1110 tag: "isbn",
1111 translation: "{0} jābūt derīgam ISBN numuram",
1112 override: false,
1113 },
1114 {
1115 tag: "isbn10",
1116 translation: "{0} jābūt derīgam ISBN-10 numuram",
1117 override: false,
1118 },
1119 {
1120 tag: "isbn13",
1121 translation: "{0} jābūt derīgam ISBN-13 numuram",
1122 override: false,
1123 },
1124 {
1125 tag: "issn",
1126 translation: "{0} jābūt derīgam ISSN numuram",
1127 override: false,
1128 },
1129 {
1130 tag: "uuid",
1131 translation: "{0} jābūt derīgam UUID",
1132 override: false,
1133 },
1134 {
1135 tag: "uuid3",
1136 translation: "{0} jābūt derīgam 3. versijas UUID",
1137 override: false,
1138 },
1139 {
1140 tag: "uuid4",
1141 translation: "{0} jābūt derīgam 4. versijas UUID",
1142 override: false,
1143 },
1144 {
1145 tag: "uuid5",
1146 translation: "{0} jābūt derīgam 5. versijas UUID",
1147 override: false,
1148 },
1149 {
1150 tag: "ulid",
1151 translation: "{0} jābūt derīgam ULID",
1152 override: false,
1153 },
1154 {
1155 tag: "ascii",
1156 translation: "{0} jāsatur tikai ascii rakstu zīmes",
1157 override: false,
1158 },
1159 {
1160 tag: "printascii",
1161 translation: "{0} jāsatur tikai drukājamas ascii rakstu zīmes",
1162 override: false,
1163 },
1164 {
1165 tag: "multibyte",
1166 translation: "{0} jāsatur multibyte rakstu zīmes",
1167 override: false,
1168 },
1169 {
1170 tag: "datauri",
1171 translation: "{0} jāsatur derīgs Data URI",
1172 override: false,
1173 },
1174 {
1175 tag: "latitude",
1176 translation: "{0} jāsatur derīgus platuma grādus",
1177 override: false,
1178 },
1179 {
1180 tag: "longitude",
1181 translation: "{0} jāsatur derīgus garuma grādus",
1182 override: false,
1183 },
1184 {
1185 tag: "ssn",
1186 translation: "{0} jābūt derīgam SSN numuram",
1187 override: false,
1188 },
1189 {
1190 tag: "ipv4",
1191 translation: "{0} jābūt derīgai IPv4 adresei",
1192 override: false,
1193 },
1194 {
1195 tag: "ipv6",
1196 translation: "{0} jābūt derīgai IPv6 adresei",
1197 override: false,
1198 },
1199 {
1200 tag: "ip",
1201 translation: "{0} jābūt derīgai IP adresei",
1202 override: false,
1203 },
1204 {
1205 tag: "cidr",
1206 translation: "{0} jāsatur derīgu CIDR notāciju",
1207 override: false,
1208 },
1209 {
1210 tag: "cidrv4",
1211 translation: "{0} jāsatur derīgu CIDR notāciju IPv4 adresei",
1212 override: false,
1213 },
1214 {
1215 tag: "cidrv6",
1216 translation: "{0} jāsatur derīgu CIDR notāciju IPv6 adresei",
1217 override: false,
1218 },
1219 {
1220 tag: "tcp_addr",
1221 translation: "{0} jābūt derīgai TCP adresei",
1222 override: false,
1223 },
1224 {
1225 tag: "tcp4_addr",
1226 translation: "{0} jābūt derīgai IPv4 TCP adresei",
1227 override: false,
1228 },
1229 {
1230 tag: "tcp6_addr",
1231 translation: "{0} jābūt derīgai IPv6 TCP adresei",
1232 override: false,
1233 },
1234 {
1235 tag: "udp_addr",
1236 translation: "{0} jābūt derīgai UDP adresei",
1237 override: false,
1238 },
1239 {
1240 tag: "udp4_addr",
1241 translation: "{0} jābūt derīgai IPv4 UDP adresei",
1242 override: false,
1243 },
1244 {
1245 tag: "udp6_addr",
1246 translation: "{0} jābūt derīgai IPv6 UDP adresei",
1247 override: false,
1248 },
1249 {
1250 tag: "ip_addr",
1251 translation: "{0} jābūt atrisināmai IP adresei",
1252 override: false,
1253 },
1254 {
1255 tag: "ip4_addr",
1256 translation: "{0} jābūt atrisināmai IPv4 adresei",
1257 override: false,
1258 },
1259 {
1260 tag: "ip6_addr",
1261 translation: "{0} jābūt atrisināmai IPv6 adresei",
1262 override: false,
1263 },
1264 {
1265 tag: "unix_addr",
1266 translation: "{0} jābūt atrisināmai UNIX adresei",
1267 override: false,
1268 },
1269 {
1270 tag: "mac",
1271 translation: "{0} jābūt derīgai MAC adresei",
1272 override: false,
1273 },
1274 {
1275 tag: "unique",
1276 translation: "{0} jāsatur unikālas vērtības",
1277 override: false,
1278 },
1279 {
1280 tag: "iscolor",
1281 translation: "{0} jābūt derīgai krāsai",
1282 override: false,
1283 },
1284 {
1285 tag: "oneof",
1286 translation: "{0} jābūt vienam no [{1}]",
1287 override: false,
1288 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1289 s, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1290 if err != nil {
1291 log.Printf("warning: error translating FieldError: %#v", fe)
1292 return fe.(error).Error()
1293 }
1294 return s
1295 },
1296 },
1297 {
1298 tag: "json",
1299 translation: "{0} jābūt derīgai json virknei",
1300 override: false,
1301 },
1302 {
1303 tag: "jwt",
1304 translation: "{0} jābūt derīgai jwt virknei",
1305 override: false,
1306 },
1307 {
1308 tag: "lowercase",
1309 translation: "{0} jābūt mazo burtu virknei",
1310 override: false,
1311 },
1312 {
1313 tag: "uppercase",
1314 translation: "{0} jābūt lielo burtu virknei",
1315 override: false,
1316 },
1317 {
1318 tag: "datetime",
1319 translation: "{0} neatbilst formātam {1}",
1320 override: false,
1321 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1322 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1323 if err != nil {
1324 log.Printf("warning: error translating FieldError: %#v", fe)
1325 return fe.(error).Error()
1326 }
1327
1328 return t
1329 },
1330 },
1331 {
1332 tag: "postcode_iso3166_alpha2",
1333 translation: "{0} neatbilst pasta indeksa formātam valstī {1}",
1334 override: false,
1335 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1336 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1337 if err != nil {
1338 log.Printf("warning: error translating FieldError: %#v", fe)
1339 return fe.(error).Error()
1340 }
1341
1342 return t
1343 },
1344 },
1345 {
1346 tag: "postcode_iso3166_alpha2_field",
1347 translation: "{0} neatbilst pasta indeksa formātam valstī, kura norādīta laukā {1}",
1348 override: false,
1349 customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
1350 t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
1351 if err != nil {
1352 log.Printf("warning: error translating FieldError: %#v", fe)
1353 return fe.(error).Error()
1354 }
1355
1356 return t
1357 },
1358 },
1359 {
1360 tag: "boolean",
1361 translation: "{0} jābūt derīgai boolean vērtībai",
1362 override: false,
1363 },
1364 }
1365
1366 for _, t := range translations {
1367
1368 if t.customTransFunc != nil && t.customRegisFunc != nil {
1369 err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc)
1370 } else if t.customTransFunc != nil && t.customRegisFunc == nil {
1371 err = v.RegisterTranslation(t.tag, trans, registrationFunc(t.tag, t.translation, t.override), t.customTransFunc)
1372 } else if t.customTransFunc == nil && t.customRegisFunc != nil {
1373 err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, translateFunc)
1374 } else {
1375 err = v.RegisterTranslation(t.tag, trans, registrationFunc(t.tag, t.translation, t.override), translateFunc)
1376 }
1377
1378 if err != nil {
1379 return
1380 }
1381 }
1382
1383 return
1384 }
1385
1386 func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc {
1387 return func(ut ut.Translator) (err error) {
1388 if err = ut.Add(tag, translation, override); err != nil {
1389 return
1390 }
1391
1392 return
1393 }
1394 }
1395
1396 func translateFunc(ut ut.Translator, fe validator.FieldError) string {
1397 t, err := ut.T(fe.Tag(), fe.Field())
1398 if err != nil {
1399 log.Printf("warning: error translating FieldError: %#v", fe)
1400 return fe.(error).Error()
1401 }
1402
1403 return t
1404 }
1405
View as plain text