en.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. package en
  2. import (
  3. "fmt"
  4. "log"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/go-playground/locales"
  10. ut "github.com/go-playground/universal-translator"
  11. "gopkg.in/go-playground/validator.v9"
  12. )
  13. // RegisterDefaultTranslations registers a set of default translations
  14. // for all built in tag's in validator; you may add your own as desired.
  15. func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) {
  16. translations := []struct {
  17. tag string
  18. translation string
  19. override bool
  20. customRegisFunc validator.RegisterTranslationsFunc
  21. customTransFunc validator.TranslationFunc
  22. }{
  23. {
  24. tag: "required",
  25. translation: "{0} is a required field",
  26. override: false,
  27. },
  28. {
  29. tag: "len",
  30. customRegisFunc: func(ut ut.Translator) (err error) {
  31. if err = ut.Add("len-string", "{0} must be {1} in length", false); err != nil {
  32. return
  33. }
  34. if err = ut.AddCardinal("len-string-character", "{0} character", locales.PluralRuleOne, false); err != nil {
  35. return
  36. }
  37. if err = ut.AddCardinal("len-string-character", "{0} characters", locales.PluralRuleOther, false); err != nil {
  38. return
  39. }
  40. if err = ut.Add("len-number", "{0} must be equal to {1}", false); err != nil {
  41. return
  42. }
  43. if err = ut.Add("len-items", "{0} must contain {1}", false); err != nil {
  44. return
  45. }
  46. if err = ut.AddCardinal("len-items-item", "{0} item", locales.PluralRuleOne, false); err != nil {
  47. return
  48. }
  49. if err = ut.AddCardinal("len-items-item", "{0} items", locales.PluralRuleOther, false); err != nil {
  50. return
  51. }
  52. return
  53. },
  54. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  55. var err error
  56. var t string
  57. var digits uint64
  58. var kind reflect.Kind
  59. if idx := strings.Index(fe.Param(), "."); idx != -1 {
  60. digits = uint64(len(fe.Param()[idx+1:]))
  61. }
  62. f64, err := strconv.ParseFloat(fe.Param(), 64)
  63. if err != nil {
  64. goto END
  65. }
  66. kind = fe.Kind()
  67. if kind == reflect.Ptr {
  68. kind = fe.Type().Elem().Kind()
  69. }
  70. switch kind {
  71. case reflect.String:
  72. var c string
  73. c, err = ut.C("len-string-character", f64, digits, ut.FmtNumber(f64, digits))
  74. if err != nil {
  75. goto END
  76. }
  77. t, err = ut.T("len-string", fe.Field(), c)
  78. case reflect.Slice, reflect.Map, reflect.Array:
  79. var c string
  80. c, err = ut.C("len-items-item", f64, digits, ut.FmtNumber(f64, digits))
  81. if err != nil {
  82. goto END
  83. }
  84. t, err = ut.T("len-items", fe.Field(), c)
  85. default:
  86. t, err = ut.T("len-number", fe.Field(), ut.FmtNumber(f64, digits))
  87. }
  88. END:
  89. if err != nil {
  90. fmt.Printf("warning: error translating FieldError: %s", err)
  91. return fe.(error).Error()
  92. }
  93. return t
  94. },
  95. },
  96. {
  97. tag: "min",
  98. customRegisFunc: func(ut ut.Translator) (err error) {
  99. if err = ut.Add("min-string", "{0} must be at least {1} in length", false); err != nil {
  100. return
  101. }
  102. if err = ut.AddCardinal("min-string-character", "{0} character", locales.PluralRuleOne, false); err != nil {
  103. return
  104. }
  105. if err = ut.AddCardinal("min-string-character", "{0} characters", locales.PluralRuleOther, false); err != nil {
  106. return
  107. }
  108. if err = ut.Add("min-number", "{0} must be {1} or greater", false); err != nil {
  109. return
  110. }
  111. if err = ut.Add("min-items", "{0} must contain at least {1}", false); err != nil {
  112. return
  113. }
  114. if err = ut.AddCardinal("min-items-item", "{0} item", locales.PluralRuleOne, false); err != nil {
  115. return
  116. }
  117. if err = ut.AddCardinal("min-items-item", "{0} items", locales.PluralRuleOther, false); err != nil {
  118. return
  119. }
  120. return
  121. },
  122. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  123. var err error
  124. var t string
  125. var digits uint64
  126. var kind reflect.Kind
  127. if idx := strings.Index(fe.Param(), "."); idx != -1 {
  128. digits = uint64(len(fe.Param()[idx+1:]))
  129. }
  130. f64, err := strconv.ParseFloat(fe.Param(), 64)
  131. if err != nil {
  132. goto END
  133. }
  134. kind = fe.Kind()
  135. if kind == reflect.Ptr {
  136. kind = fe.Type().Elem().Kind()
  137. }
  138. switch kind {
  139. case reflect.String:
  140. var c string
  141. c, err = ut.C("min-string-character", f64, digits, ut.FmtNumber(f64, digits))
  142. if err != nil {
  143. goto END
  144. }
  145. t, err = ut.T("min-string", fe.Field(), c)
  146. case reflect.Slice, reflect.Map, reflect.Array:
  147. var c string
  148. c, err = ut.C("min-items-item", f64, digits, ut.FmtNumber(f64, digits))
  149. if err != nil {
  150. goto END
  151. }
  152. t, err = ut.T("min-items", fe.Field(), c)
  153. default:
  154. t, err = ut.T("min-number", fe.Field(), ut.FmtNumber(f64, digits))
  155. }
  156. END:
  157. if err != nil {
  158. fmt.Printf("warning: error translating FieldError: %s", err)
  159. return fe.(error).Error()
  160. }
  161. return t
  162. },
  163. },
  164. {
  165. tag: "max",
  166. customRegisFunc: func(ut ut.Translator) (err error) {
  167. if err = ut.Add("max-string", "{0} must be a maximum of {1} in length", false); err != nil {
  168. return
  169. }
  170. if err = ut.AddCardinal("max-string-character", "{0} character", locales.PluralRuleOne, false); err != nil {
  171. return
  172. }
  173. if err = ut.AddCardinal("max-string-character", "{0} characters", locales.PluralRuleOther, false); err != nil {
  174. return
  175. }
  176. if err = ut.Add("max-number", "{0} must be {1} or less", false); err != nil {
  177. return
  178. }
  179. if err = ut.Add("max-items", "{0} must contain at maximum {1}", false); err != nil {
  180. return
  181. }
  182. if err = ut.AddCardinal("max-items-item", "{0} item", locales.PluralRuleOne, false); err != nil {
  183. return
  184. }
  185. if err = ut.AddCardinal("max-items-item", "{0} items", locales.PluralRuleOther, false); err != nil {
  186. return
  187. }
  188. return
  189. },
  190. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  191. var err error
  192. var t string
  193. var digits uint64
  194. var kind reflect.Kind
  195. if idx := strings.Index(fe.Param(), "."); idx != -1 {
  196. digits = uint64(len(fe.Param()[idx+1:]))
  197. }
  198. f64, err := strconv.ParseFloat(fe.Param(), 64)
  199. if err != nil {
  200. goto END
  201. }
  202. kind = fe.Kind()
  203. if kind == reflect.Ptr {
  204. kind = fe.Type().Elem().Kind()
  205. }
  206. switch kind {
  207. case reflect.String:
  208. var c string
  209. c, err = ut.C("max-string-character", f64, digits, ut.FmtNumber(f64, digits))
  210. if err != nil {
  211. goto END
  212. }
  213. t, err = ut.T("max-string", fe.Field(), c)
  214. case reflect.Slice, reflect.Map, reflect.Array:
  215. var c string
  216. c, err = ut.C("max-items-item", f64, digits, ut.FmtNumber(f64, digits))
  217. if err != nil {
  218. goto END
  219. }
  220. t, err = ut.T("max-items", fe.Field(), c)
  221. default:
  222. t, err = ut.T("max-number", fe.Field(), ut.FmtNumber(f64, digits))
  223. }
  224. END:
  225. if err != nil {
  226. fmt.Printf("warning: error translating FieldError: %s", err)
  227. return fe.(error).Error()
  228. }
  229. return t
  230. },
  231. },
  232. {
  233. tag: "eq",
  234. translation: "{0} is not equal to {1}",
  235. override: false,
  236. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  237. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  238. if err != nil {
  239. fmt.Printf("warning: error translating FieldError: %#v", fe)
  240. return fe.(error).Error()
  241. }
  242. return t
  243. },
  244. },
  245. {
  246. tag: "ne",
  247. translation: "{0} should not be equal to {1}",
  248. override: false,
  249. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  250. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  251. if err != nil {
  252. fmt.Printf("warning: error translating FieldError: %#v", fe)
  253. return fe.(error).Error()
  254. }
  255. return t
  256. },
  257. },
  258. {
  259. tag: "lt",
  260. customRegisFunc: func(ut ut.Translator) (err error) {
  261. if err = ut.Add("lt-string", "{0} must be less than {1} in length", false); err != nil {
  262. return
  263. }
  264. if err = ut.AddCardinal("lt-string-character", "{0} character", locales.PluralRuleOne, false); err != nil {
  265. return
  266. }
  267. if err = ut.AddCardinal("lt-string-character", "{0} characters", locales.PluralRuleOther, false); err != nil {
  268. return
  269. }
  270. if err = ut.Add("lt-number", "{0} must be less than {1}", false); err != nil {
  271. return
  272. }
  273. if err = ut.Add("lt-items", "{0} must contain less than {1}", false); err != nil {
  274. return
  275. }
  276. if err = ut.AddCardinal("lt-items-item", "{0} item", locales.PluralRuleOne, false); err != nil {
  277. return
  278. }
  279. if err = ut.AddCardinal("lt-items-item", "{0} items", locales.PluralRuleOther, false); err != nil {
  280. return
  281. }
  282. if err = ut.Add("lt-datetime", "{0} must be less than the current Date & Time", false); err != nil {
  283. return
  284. }
  285. return
  286. },
  287. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  288. var err error
  289. var t string
  290. var f64 float64
  291. var digits uint64
  292. var kind reflect.Kind
  293. fn := func() (err error) {
  294. if idx := strings.Index(fe.Param(), "."); idx != -1 {
  295. digits = uint64(len(fe.Param()[idx+1:]))
  296. }
  297. f64, err = strconv.ParseFloat(fe.Param(), 64)
  298. return
  299. }
  300. kind = fe.Kind()
  301. if kind == reflect.Ptr {
  302. kind = fe.Type().Elem().Kind()
  303. }
  304. switch kind {
  305. case reflect.String:
  306. var c string
  307. err = fn()
  308. if err != nil {
  309. goto END
  310. }
  311. c, err = ut.C("lt-string-character", f64, digits, ut.FmtNumber(f64, digits))
  312. if err != nil {
  313. goto END
  314. }
  315. t, err = ut.T("lt-string", fe.Field(), c)
  316. case reflect.Slice, reflect.Map, reflect.Array:
  317. var c string
  318. err = fn()
  319. if err != nil {
  320. goto END
  321. }
  322. c, err = ut.C("lt-items-item", f64, digits, ut.FmtNumber(f64, digits))
  323. if err != nil {
  324. goto END
  325. }
  326. t, err = ut.T("lt-items", fe.Field(), c)
  327. case reflect.Struct:
  328. if fe.Type() != reflect.TypeOf(time.Time{}) {
  329. err = fmt.Errorf("tag '%s' cannot be used on a struct type", fe.Tag())
  330. goto END
  331. }
  332. t, err = ut.T("lt-datetime", fe.Field())
  333. default:
  334. err = fn()
  335. if err != nil {
  336. goto END
  337. }
  338. t, err = ut.T("lt-number", fe.Field(), ut.FmtNumber(f64, digits))
  339. }
  340. END:
  341. if err != nil {
  342. fmt.Printf("warning: error translating FieldError: %s", err)
  343. return fe.(error).Error()
  344. }
  345. return t
  346. },
  347. },
  348. {
  349. tag: "lte",
  350. customRegisFunc: func(ut ut.Translator) (err error) {
  351. if err = ut.Add("lte-string", "{0} must be at maximum {1} in length", false); err != nil {
  352. return
  353. }
  354. if err = ut.AddCardinal("lte-string-character", "{0} character", locales.PluralRuleOne, false); err != nil {
  355. return
  356. }
  357. if err = ut.AddCardinal("lte-string-character", "{0} characters", locales.PluralRuleOther, false); err != nil {
  358. return
  359. }
  360. if err = ut.Add("lte-number", "{0} must be {1} or less", false); err != nil {
  361. return
  362. }
  363. if err = ut.Add("lte-items", "{0} must contain at maximum {1}", false); err != nil {
  364. return
  365. }
  366. if err = ut.AddCardinal("lte-items-item", "{0} item", locales.PluralRuleOne, false); err != nil {
  367. return
  368. }
  369. if err = ut.AddCardinal("lte-items-item", "{0} items", locales.PluralRuleOther, false); err != nil {
  370. return
  371. }
  372. if err = ut.Add("lte-datetime", "{0} must be less than or equal to the current Date & Time", false); err != nil {
  373. return
  374. }
  375. return
  376. },
  377. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  378. var err error
  379. var t string
  380. var f64 float64
  381. var digits uint64
  382. var kind reflect.Kind
  383. fn := func() (err error) {
  384. if idx := strings.Index(fe.Param(), "."); idx != -1 {
  385. digits = uint64(len(fe.Param()[idx+1:]))
  386. }
  387. f64, err = strconv.ParseFloat(fe.Param(), 64)
  388. return
  389. }
  390. kind = fe.Kind()
  391. if kind == reflect.Ptr {
  392. kind = fe.Type().Elem().Kind()
  393. }
  394. switch kind {
  395. case reflect.String:
  396. var c string
  397. err = fn()
  398. if err != nil {
  399. goto END
  400. }
  401. c, err = ut.C("lte-string-character", f64, digits, ut.FmtNumber(f64, digits))
  402. if err != nil {
  403. goto END
  404. }
  405. t, err = ut.T("lte-string", fe.Field(), c)
  406. case reflect.Slice, reflect.Map, reflect.Array:
  407. var c string
  408. err = fn()
  409. if err != nil {
  410. goto END
  411. }
  412. c, err = ut.C("lte-items-item", f64, digits, ut.FmtNumber(f64, digits))
  413. if err != nil {
  414. goto END
  415. }
  416. t, err = ut.T("lte-items", fe.Field(), c)
  417. case reflect.Struct:
  418. if fe.Type() != reflect.TypeOf(time.Time{}) {
  419. err = fmt.Errorf("tag '%s' cannot be used on a struct type", fe.Tag())
  420. goto END
  421. }
  422. t, err = ut.T("lte-datetime", fe.Field())
  423. default:
  424. err = fn()
  425. if err != nil {
  426. goto END
  427. }
  428. t, err = ut.T("lte-number", fe.Field(), ut.FmtNumber(f64, digits))
  429. }
  430. END:
  431. if err != nil {
  432. fmt.Printf("warning: error translating FieldError: %s", err)
  433. return fe.(error).Error()
  434. }
  435. return t
  436. },
  437. },
  438. {
  439. tag: "gt",
  440. customRegisFunc: func(ut ut.Translator) (err error) {
  441. if err = ut.Add("gt-string", "{0} must be greater than {1} in length", false); err != nil {
  442. return
  443. }
  444. if err = ut.AddCardinal("gt-string-character", "{0} character", locales.PluralRuleOne, false); err != nil {
  445. return
  446. }
  447. if err = ut.AddCardinal("gt-string-character", "{0} characters", locales.PluralRuleOther, false); err != nil {
  448. return
  449. }
  450. if err = ut.Add("gt-number", "{0} must be greater than {1}", false); err != nil {
  451. return
  452. }
  453. if err = ut.Add("gt-items", "{0} must contain more than {1}", false); err != nil {
  454. return
  455. }
  456. if err = ut.AddCardinal("gt-items-item", "{0} item", locales.PluralRuleOne, false); err != nil {
  457. return
  458. }
  459. if err = ut.AddCardinal("gt-items-item", "{0} items", locales.PluralRuleOther, false); err != nil {
  460. return
  461. }
  462. if err = ut.Add("gt-datetime", "{0} must be greater than the current Date & Time", false); err != nil {
  463. return
  464. }
  465. return
  466. },
  467. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  468. var err error
  469. var t string
  470. var f64 float64
  471. var digits uint64
  472. var kind reflect.Kind
  473. fn := func() (err error) {
  474. if idx := strings.Index(fe.Param(), "."); idx != -1 {
  475. digits = uint64(len(fe.Param()[idx+1:]))
  476. }
  477. f64, err = strconv.ParseFloat(fe.Param(), 64)
  478. return
  479. }
  480. kind = fe.Kind()
  481. if kind == reflect.Ptr {
  482. kind = fe.Type().Elem().Kind()
  483. }
  484. switch kind {
  485. case reflect.String:
  486. var c string
  487. err = fn()
  488. if err != nil {
  489. goto END
  490. }
  491. c, err = ut.C("gt-string-character", f64, digits, ut.FmtNumber(f64, digits))
  492. if err != nil {
  493. goto END
  494. }
  495. t, err = ut.T("gt-string", fe.Field(), c)
  496. case reflect.Slice, reflect.Map, reflect.Array:
  497. var c string
  498. err = fn()
  499. if err != nil {
  500. goto END
  501. }
  502. c, err = ut.C("gt-items-item", f64, digits, ut.FmtNumber(f64, digits))
  503. if err != nil {
  504. goto END
  505. }
  506. t, err = ut.T("gt-items", fe.Field(), c)
  507. case reflect.Struct:
  508. if fe.Type() != reflect.TypeOf(time.Time{}) {
  509. err = fmt.Errorf("tag '%s' cannot be used on a struct type", fe.Tag())
  510. goto END
  511. }
  512. t, err = ut.T("gt-datetime", fe.Field())
  513. default:
  514. err = fn()
  515. if err != nil {
  516. goto END
  517. }
  518. t, err = ut.T("gt-number", fe.Field(), ut.FmtNumber(f64, digits))
  519. }
  520. END:
  521. if err != nil {
  522. fmt.Printf("warning: error translating FieldError: %s", err)
  523. return fe.(error).Error()
  524. }
  525. return t
  526. },
  527. },
  528. {
  529. tag: "gte",
  530. customRegisFunc: func(ut ut.Translator) (err error) {
  531. if err = ut.Add("gte-string", "{0} must be at least {1} in length", false); err != nil {
  532. return
  533. }
  534. if err = ut.AddCardinal("gte-string-character", "{0} character", locales.PluralRuleOne, false); err != nil {
  535. return
  536. }
  537. if err = ut.AddCardinal("gte-string-character", "{0} characters", locales.PluralRuleOther, false); err != nil {
  538. return
  539. }
  540. if err = ut.Add("gte-number", "{0} must be {1} or greater", false); err != nil {
  541. return
  542. }
  543. if err = ut.Add("gte-items", "{0} must contain at least {1}", false); err != nil {
  544. return
  545. }
  546. if err = ut.AddCardinal("gte-items-item", "{0} item", locales.PluralRuleOne, false); err != nil {
  547. return
  548. }
  549. if err = ut.AddCardinal("gte-items-item", "{0} items", locales.PluralRuleOther, false); err != nil {
  550. return
  551. }
  552. if err = ut.Add("gte-datetime", "{0} must be greater than or equal to the current Date & Time", false); err != nil {
  553. return
  554. }
  555. return
  556. },
  557. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  558. var err error
  559. var t string
  560. var f64 float64
  561. var digits uint64
  562. var kind reflect.Kind
  563. fn := func() (err error) {
  564. if idx := strings.Index(fe.Param(), "."); idx != -1 {
  565. digits = uint64(len(fe.Param()[idx+1:]))
  566. }
  567. f64, err = strconv.ParseFloat(fe.Param(), 64)
  568. return
  569. }
  570. kind = fe.Kind()
  571. if kind == reflect.Ptr {
  572. kind = fe.Type().Elem().Kind()
  573. }
  574. switch kind {
  575. case reflect.String:
  576. var c string
  577. err = fn()
  578. if err != nil {
  579. goto END
  580. }
  581. c, err = ut.C("gte-string-character", f64, digits, ut.FmtNumber(f64, digits))
  582. if err != nil {
  583. goto END
  584. }
  585. t, err = ut.T("gte-string", fe.Field(), c)
  586. case reflect.Slice, reflect.Map, reflect.Array:
  587. var c string
  588. err = fn()
  589. if err != nil {
  590. goto END
  591. }
  592. c, err = ut.C("gte-items-item", f64, digits, ut.FmtNumber(f64, digits))
  593. if err != nil {
  594. goto END
  595. }
  596. t, err = ut.T("gte-items", fe.Field(), c)
  597. case reflect.Struct:
  598. if fe.Type() != reflect.TypeOf(time.Time{}) {
  599. err = fmt.Errorf("tag '%s' cannot be used on a struct type", fe.Tag())
  600. goto END
  601. }
  602. t, err = ut.T("gte-datetime", fe.Field())
  603. default:
  604. err = fn()
  605. if err != nil {
  606. goto END
  607. }
  608. t, err = ut.T("gte-number", fe.Field(), ut.FmtNumber(f64, digits))
  609. }
  610. END:
  611. if err != nil {
  612. fmt.Printf("warning: error translating FieldError: %s", err)
  613. return fe.(error).Error()
  614. }
  615. return t
  616. },
  617. },
  618. {
  619. tag: "eqfield",
  620. translation: "{0} must be equal to {1}",
  621. override: false,
  622. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  623. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  624. if err != nil {
  625. log.Printf("warning: error translating FieldError: %#v", fe)
  626. return fe.(error).Error()
  627. }
  628. return t
  629. },
  630. },
  631. {
  632. tag: "eqcsfield",
  633. translation: "{0} must be equal to {1}",
  634. override: false,
  635. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  636. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  637. if err != nil {
  638. log.Printf("warning: error translating FieldError: %#v", fe)
  639. return fe.(error).Error()
  640. }
  641. return t
  642. },
  643. },
  644. {
  645. tag: "necsfield",
  646. translation: "{0} cannot be equal to {1}",
  647. override: false,
  648. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  649. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  650. if err != nil {
  651. log.Printf("warning: error translating FieldError: %#v", fe)
  652. return fe.(error).Error()
  653. }
  654. return t
  655. },
  656. },
  657. {
  658. tag: "gtcsfield",
  659. translation: "{0} must be greater than {1}",
  660. override: false,
  661. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  662. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  663. if err != nil {
  664. log.Printf("warning: error translating FieldError: %#v", fe)
  665. return fe.(error).Error()
  666. }
  667. return t
  668. },
  669. },
  670. {
  671. tag: "gtecsfield",
  672. translation: "{0} must be greater than or equal to {1}",
  673. override: false,
  674. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  675. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  676. if err != nil {
  677. log.Printf("warning: error translating FieldError: %#v", fe)
  678. return fe.(error).Error()
  679. }
  680. return t
  681. },
  682. },
  683. {
  684. tag: "ltcsfield",
  685. translation: "{0} must be less than {1}",
  686. override: false,
  687. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  688. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  689. if err != nil {
  690. log.Printf("warning: error translating FieldError: %#v", fe)
  691. return fe.(error).Error()
  692. }
  693. return t
  694. },
  695. },
  696. {
  697. tag: "ltecsfield",
  698. translation: "{0} must be less than or equal to {1}",
  699. override: false,
  700. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  701. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  702. if err != nil {
  703. log.Printf("warning: error translating FieldError: %#v", fe)
  704. return fe.(error).Error()
  705. }
  706. return t
  707. },
  708. },
  709. {
  710. tag: "nefield",
  711. translation: "{0} cannot be equal to {1}",
  712. override: false,
  713. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  714. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  715. if err != nil {
  716. log.Printf("warning: error translating FieldError: %#v", fe)
  717. return fe.(error).Error()
  718. }
  719. return t
  720. },
  721. },
  722. {
  723. tag: "gtfield",
  724. translation: "{0} must be greater than {1}",
  725. override: false,
  726. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  727. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  728. if err != nil {
  729. log.Printf("warning: error translating FieldError: %#v", fe)
  730. return fe.(error).Error()
  731. }
  732. return t
  733. },
  734. },
  735. {
  736. tag: "gtefield",
  737. translation: "{0} must be greater than or equal to {1}",
  738. override: false,
  739. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  740. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  741. if err != nil {
  742. log.Printf("warning: error translating FieldError: %#v", fe)
  743. return fe.(error).Error()
  744. }
  745. return t
  746. },
  747. },
  748. {
  749. tag: "ltfield",
  750. translation: "{0} must be less than {1}",
  751. override: false,
  752. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  753. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  754. if err != nil {
  755. log.Printf("warning: error translating FieldError: %#v", fe)
  756. return fe.(error).Error()
  757. }
  758. return t
  759. },
  760. },
  761. {
  762. tag: "ltefield",
  763. translation: "{0} must be less than or equal to {1}",
  764. override: false,
  765. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  766. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  767. if err != nil {
  768. log.Printf("warning: error translating FieldError: %#v", fe)
  769. return fe.(error).Error()
  770. }
  771. return t
  772. },
  773. },
  774. {
  775. tag: "alpha",
  776. translation: "{0} can only contain alphabetic characters",
  777. override: false,
  778. },
  779. {
  780. tag: "alphanum",
  781. translation: "{0} can only contain alphanumeric characters",
  782. override: false,
  783. },
  784. {
  785. tag: "numeric",
  786. translation: "{0} must be a valid numeric value",
  787. override: false,
  788. },
  789. {
  790. tag: "number",
  791. translation: "{0} must be a valid number",
  792. override: false,
  793. },
  794. {
  795. tag: "hexadecimal",
  796. translation: "{0} must be a valid hexadecimal",
  797. override: false,
  798. },
  799. {
  800. tag: "hexcolor",
  801. translation: "{0} must be a valid HEX color",
  802. override: false,
  803. },
  804. {
  805. tag: "rgb",
  806. translation: "{0} must be a valid RGB color",
  807. override: false,
  808. },
  809. {
  810. tag: "rgba",
  811. translation: "{0} must be a valid RGBA color",
  812. override: false,
  813. },
  814. {
  815. tag: "hsl",
  816. translation: "{0} must be a valid HSL color",
  817. override: false,
  818. },
  819. {
  820. tag: "hsla",
  821. translation: "{0} must be a valid HSLA color",
  822. override: false,
  823. },
  824. {
  825. tag: "email",
  826. translation: "{0} must be a valid email address",
  827. override: false,
  828. },
  829. {
  830. tag: "url",
  831. translation: "{0} must be a valid URL",
  832. override: false,
  833. },
  834. {
  835. tag: "uri",
  836. translation: "{0} must be a valid URI",
  837. override: false,
  838. },
  839. {
  840. tag: "base64",
  841. translation: "{0} must be a valid Base64 string",
  842. override: false,
  843. },
  844. {
  845. tag: "contains",
  846. translation: "{0} must contain the text '{1}'",
  847. override: false,
  848. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  849. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  850. if err != nil {
  851. log.Printf("warning: error translating FieldError: %#v", fe)
  852. return fe.(error).Error()
  853. }
  854. return t
  855. },
  856. },
  857. {
  858. tag: "containsany",
  859. translation: "{0} must contain at least one of the following characters '{1}'",
  860. override: false,
  861. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  862. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  863. if err != nil {
  864. log.Printf("warning: error translating FieldError: %#v", fe)
  865. return fe.(error).Error()
  866. }
  867. return t
  868. },
  869. },
  870. {
  871. tag: "excludes",
  872. translation: "{0} cannot contain the text '{1}'",
  873. override: false,
  874. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  875. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  876. if err != nil {
  877. log.Printf("warning: error translating FieldError: %#v", fe)
  878. return fe.(error).Error()
  879. }
  880. return t
  881. },
  882. },
  883. {
  884. tag: "excludesall",
  885. translation: "{0} cannot contain any of the following characters '{1}'",
  886. override: false,
  887. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  888. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  889. if err != nil {
  890. log.Printf("warning: error translating FieldError: %#v", fe)
  891. return fe.(error).Error()
  892. }
  893. return t
  894. },
  895. },
  896. {
  897. tag: "excludesrune",
  898. translation: "{0} cannot contain the following '{1}'",
  899. override: false,
  900. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  901. t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  902. if err != nil {
  903. log.Printf("warning: error translating FieldError: %#v", fe)
  904. return fe.(error).Error()
  905. }
  906. return t
  907. },
  908. },
  909. {
  910. tag: "isbn",
  911. translation: "{0} must be a valid ISBN number",
  912. override: false,
  913. },
  914. {
  915. tag: "isbn10",
  916. translation: "{0} must be a valid ISBN-10 number",
  917. override: false,
  918. },
  919. {
  920. tag: "isbn13",
  921. translation: "{0} must be a valid ISBN-13 number",
  922. override: false,
  923. },
  924. {
  925. tag: "uuid",
  926. translation: "{0} must be a valid UUID",
  927. override: false,
  928. },
  929. {
  930. tag: "uuid3",
  931. translation: "{0} must be a valid version 3 UUID",
  932. override: false,
  933. },
  934. {
  935. tag: "uuid4",
  936. translation: "{0} must be a valid version 4 UUID",
  937. override: false,
  938. },
  939. {
  940. tag: "uuid5",
  941. translation: "{0} must be a valid version 5 UUID",
  942. override: false,
  943. },
  944. {
  945. tag: "ascii",
  946. translation: "{0} must contain only ascii characters",
  947. override: false,
  948. },
  949. {
  950. tag: "printascii",
  951. translation: "{0} must contain only printable ascii characters",
  952. override: false,
  953. },
  954. {
  955. tag: "multibyte",
  956. translation: "{0} must contain multibyte characters",
  957. override: false,
  958. },
  959. {
  960. tag: "datauri",
  961. translation: "{0} must contain a valid Data URI",
  962. override: false,
  963. },
  964. {
  965. tag: "latitude",
  966. translation: "{0} must contain valid latitude coordinates",
  967. override: false,
  968. },
  969. {
  970. tag: "longitude",
  971. translation: "{0} must contain a valid longitude coordinates",
  972. override: false,
  973. },
  974. {
  975. tag: "ssn",
  976. translation: "{0} must be a valid SSN number",
  977. override: false,
  978. },
  979. {
  980. tag: "ipv4",
  981. translation: "{0} must be a valid IPv4 address",
  982. override: false,
  983. },
  984. {
  985. tag: "ipv6",
  986. translation: "{0} must be a valid IPv6 address",
  987. override: false,
  988. },
  989. {
  990. tag: "ip",
  991. translation: "{0} must be a valid IP address",
  992. override: false,
  993. },
  994. {
  995. tag: "cidr",
  996. translation: "{0} must contain a valid CIDR notation",
  997. override: false,
  998. },
  999. {
  1000. tag: "cidrv4",
  1001. translation: "{0} must contain a valid CIDR notation for an IPv4 address",
  1002. override: false,
  1003. },
  1004. {
  1005. tag: "cidrv6",
  1006. translation: "{0} must contain a valid CIDR notation for an IPv6 address",
  1007. override: false,
  1008. },
  1009. {
  1010. tag: "tcp_addr",
  1011. translation: "{0} must be a valid TCP address",
  1012. override: false,
  1013. },
  1014. {
  1015. tag: "tcp4_addr",
  1016. translation: "{0} must be a valid IPv4 TCP address",
  1017. override: false,
  1018. },
  1019. {
  1020. tag: "tcp6_addr",
  1021. translation: "{0} must be a valid IPv6 TCP address",
  1022. override: false,
  1023. },
  1024. {
  1025. tag: "udp_addr",
  1026. translation: "{0} must be a valid UDP address",
  1027. override: false,
  1028. },
  1029. {
  1030. tag: "udp4_addr",
  1031. translation: "{0} must be a valid IPv4 UDP address",
  1032. override: false,
  1033. },
  1034. {
  1035. tag: "udp6_addr",
  1036. translation: "{0} must be a valid IPv6 UDP address",
  1037. override: false,
  1038. },
  1039. {
  1040. tag: "ip_addr",
  1041. translation: "{0} must be a resolvable IP address",
  1042. override: false,
  1043. },
  1044. {
  1045. tag: "ip4_addr",
  1046. translation: "{0} must be a resolvable IPv4 address",
  1047. override: false,
  1048. },
  1049. {
  1050. tag: "ip6_addr",
  1051. translation: "{0} must be a resolvable IPv6 address",
  1052. override: false,
  1053. },
  1054. {
  1055. tag: "unix_addr",
  1056. translation: "{0} must be a resolvable UNIX address",
  1057. override: false,
  1058. },
  1059. {
  1060. tag: "mac",
  1061. translation: "{0} must contain a valid MAC address",
  1062. override: false,
  1063. },
  1064. {
  1065. tag: "unique",
  1066. translation: "{0} must contain unique values",
  1067. override: false,
  1068. },
  1069. {
  1070. tag: "iscolor",
  1071. translation: "{0} must be a valid color",
  1072. override: false,
  1073. },
  1074. {
  1075. tag: "oneof",
  1076. translation: "{0} must be one of [{1}]",
  1077. override: false,
  1078. customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
  1079. s, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
  1080. if err != nil {
  1081. log.Printf("warning: error translating FieldError: %#v", fe)
  1082. return fe.(error).Error()
  1083. }
  1084. return s
  1085. },
  1086. },
  1087. }
  1088. for _, t := range translations {
  1089. if t.customTransFunc != nil && t.customRegisFunc != nil {
  1090. err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc)
  1091. } else if t.customTransFunc != nil && t.customRegisFunc == nil {
  1092. err = v.RegisterTranslation(t.tag, trans, registrationFunc(t.tag, t.translation, t.override), t.customTransFunc)
  1093. } else if t.customTransFunc == nil && t.customRegisFunc != nil {
  1094. err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, translateFunc)
  1095. } else {
  1096. err = v.RegisterTranslation(t.tag, trans, registrationFunc(t.tag, t.translation, t.override), translateFunc)
  1097. }
  1098. if err != nil {
  1099. return
  1100. }
  1101. }
  1102. return
  1103. }
  1104. func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc {
  1105. return func(ut ut.Translator) (err error) {
  1106. if err = ut.Add(tag, translation, override); err != nil {
  1107. return
  1108. }
  1109. return
  1110. }
  1111. }
  1112. func translateFunc(ut ut.Translator, fe validator.FieldError) string {
  1113. t, err := ut.T(fe.Tag(), fe.Field())
  1114. if err != nil {
  1115. log.Printf("warning: error translating FieldError: %#v", fe)
  1116. return fe.(error).Error()
  1117. }
  1118. return t
  1119. }