translator.tmpl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. {{ define "translator" }}
  2. package {{ .Locale }}
  3. import (
  4. "math"
  5. "strconv"
  6. "time"
  7. "github.com/go-playground/locales"
  8. "github.com/go-playground/locales/currency"
  9. )
  10. type {{ .Locale }} struct {
  11. locale string
  12. pluralsCardinal []locales.PluralRule
  13. pluralsOrdinal []locales.PluralRule
  14. pluralsRange []locales.PluralRule
  15. decimal string
  16. group string
  17. minus string
  18. percent string
  19. {{- if gt (len .FmtPercentPrefix) 0}}
  20. percentPrefix string
  21. {{- end }}
  22. {{- if gt (len .FmtPercentSuffix) 0}}
  23. percentSuffix string
  24. {{- end }}
  25. perMille string
  26. timeSeparator string
  27. inifinity string
  28. currencies []string // idx = enum of currency code
  29. {{- if gt (len .FmtCurrencyPrefix) 0}}
  30. currencyPositivePrefix string
  31. {{- end }}
  32. {{- if gt (len .FmtCurrencySuffix) 0}}
  33. currencyPositiveSuffix string
  34. {{- end }}
  35. {{- if gt (len .FmtCurrencyNegativePrefix) 0}}
  36. currencyNegativePrefix string
  37. {{- end }}
  38. {{- if gt (len .FmtCurrencyNegativeSuffix) 0}}
  39. currencyNegativeSuffix string
  40. {{- end }}
  41. monthsAbbreviated []string
  42. monthsNarrow []string
  43. monthsWide []string
  44. daysAbbreviated []string
  45. daysNarrow []string
  46. daysShort []string
  47. daysWide []string
  48. periodsAbbreviated []string
  49. periodsNarrow []string
  50. periodsShort []string
  51. periodsWide []string
  52. erasAbbreviated []string
  53. erasNarrow []string
  54. erasWide []string
  55. timezones map[string]string
  56. }
  57. // New returns a new instance of translator for the '{{ .Locale }}' locale
  58. func New() locales.Translator {
  59. return &{{ .Locale }}{
  60. locale: "{{ .Locale }}",
  61. pluralsCardinal: {{ .Plurals }},
  62. pluralsOrdinal: {{ .PluralsOrdinal }},
  63. pluralsRange: {{ .PluralsRange }},
  64. {{- if gt (len .Decimal) 0}}
  65. decimal: "{{ .Decimal }}",
  66. {{- end}}
  67. {{- if gt (len .Group) 0}}
  68. group: "{{ .Group }}",
  69. {{- end}}
  70. {{- if gt (len .Minus) 0}}
  71. minus: "{{ .Minus }}",
  72. {{- end}}
  73. {{- if gt (len .Percent) 0}}
  74. percent: "{{ .Percent }}",
  75. {{- end}}
  76. {{- if gt (len .PerMille) 0}}
  77. perMille: "{{ .PerMille }}",
  78. {{- end}}
  79. {{- if gt (len .TimeSeparator) 0}}
  80. timeSeparator: "{{ .TimeSeparator }}",
  81. {{- end}}
  82. {{- if gt (len .Infinity) 0}}
  83. inifinity: "{{ .Infinity }}",
  84. {{- end}}
  85. currencies: {{ .Currencies }},
  86. {{- if gt (len .FmtPercentPrefix) 0}}
  87. percentPrefix: "{{ .FmtPercentPrefix }}",
  88. {{- end -}}
  89. {{- if gt (len .FmtPercentSuffix) 0}}
  90. percentSuffix: "{{ .FmtPercentSuffix }}",
  91. {{- end -}}
  92. {{- if gt (len .FmtCurrencyPrefix) 0}}
  93. currencyPositivePrefix: "{{ .FmtCurrencyPrefix }}",
  94. {{- end -}}
  95. {{- if gt (len .FmtCurrencySuffix) 0}}
  96. currencyPositiveSuffix: "{{ .FmtCurrencySuffix }}",
  97. {{- end -}}
  98. {{- if gt (len .FmtCurrencyNegativePrefix) 0}}
  99. currencyNegativePrefix: "{{ .FmtCurrencyNegativePrefix }}",
  100. {{- end -}}
  101. {{- if gt (len .FmtCurrencyNegativeSuffix) 0}}
  102. currencyNegativeSuffix: "{{ .FmtCurrencyNegativeSuffix }}",
  103. {{- end -}}
  104. {{- if gt (len .FmtMonthsAbbreviated) 0 }}
  105. monthsAbbreviated: {{ .FmtMonthsAbbreviated }},
  106. {{- end -}}
  107. {{- if gt (len .FmtMonthsNarrow) 0 }}
  108. monthsNarrow: {{ .FmtMonthsNarrow }},
  109. {{- end -}}
  110. {{- if gt (len .FmtMonthsWide) 0 }}
  111. monthsWide: {{ .FmtMonthsWide }},
  112. {{- end -}}
  113. {{- if gt (len .FmtDaysAbbreviated) 0 }}
  114. daysAbbreviated: {{ .FmtDaysAbbreviated }},
  115. {{- end -}}
  116. {{- if gt (len .FmtDaysNarrow) 0 }}
  117. daysNarrow: {{ .FmtDaysNarrow }},
  118. {{- end -}}
  119. {{- if gt (len .FmtDaysShort) 0 }}
  120. daysShort: {{ .FmtDaysShort }},
  121. {{- end -}}
  122. {{- if gt (len .FmtDaysWide) 0 }}
  123. daysWide: {{ .FmtDaysWide }},
  124. {{- end -}}
  125. {{- if gt (len .FmtPeriodsAbbreviated) 0 }}
  126. periodsAbbreviated: {{ .FmtPeriodsAbbreviated }},
  127. {{- end -}}
  128. {{- if gt (len .FmtPeriodsNarrow) 0 }}
  129. periodsNarrow: {{ .FmtPeriodsNarrow }},
  130. {{- end -}}
  131. {{- if gt (len .FmtPeriodsShort) 0 }}
  132. periodsShort: {{ .FmtPeriodsShort }},
  133. {{- end -}}
  134. {{- if gt (len .FmtPeriodsWide) 0 }}
  135. periodsWide: {{ .FmtPeriodsWide }},
  136. {{- end -}}
  137. {{- if gt (len .FmtErasAbbreviated) 0 }}
  138. erasAbbreviated: {{ .FmtErasAbbreviated }},
  139. {{- end -}}
  140. {{- if gt (len .FmtErasNarrow) 0 }}
  141. erasNarrow: {{ .FmtErasNarrow }},
  142. {{- end -}}
  143. {{- if gt (len .FmtErasWide) 0 }}
  144. erasWide: {{ .FmtErasWide }},
  145. {{- end }}
  146. timezones: {{ .FmtTimezones }},
  147. }
  148. }
  149. // Locale returns the current translators string locale
  150. func({{ .BaseLocale }} *{{ .Locale }}) Locale() string {
  151. return {{ .BaseLocale }}.locale
  152. }
  153. // PluralsCardinal returns the list of cardinal plural rules associated with '{{ .Locale }}'
  154. func({{ .BaseLocale }} *{{ .Locale }}) PluralsCardinal() []locales.PluralRule {
  155. return {{ .BaseLocale }}.pluralsCardinal
  156. }
  157. // PluralsOrdinal returns the list of ordinal plural rules associated with '{{ .Locale }}'
  158. func({{ .BaseLocale }} *{{ .Locale }}) PluralsOrdinal() []locales.PluralRule {
  159. return {{ .BaseLocale }}.pluralsOrdinal
  160. }
  161. // PluralsRange returns the list of range plural rules associated with '{{ .Locale }}'
  162. func({{ .BaseLocale }} *{{ .Locale }}) PluralsRange() []locales.PluralRule {
  163. return {{ .BaseLocale }}.pluralsRange
  164. }
  165. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for '{{ .Locale }}'
  166. func({{ .BaseLocale }} *{{ .Locale }}) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  167. {{ .CardinalFunc }}
  168. }
  169. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for '{{ .Locale }}'
  170. func({{ .BaseLocale }} *{{ .Locale }}) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  171. {{ .OrdinalFunc }}
  172. }
  173. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for '{{ .Locale }}'
  174. func({{ .BaseLocale }} *{{ .Locale }}) RangePluralRule(num1 float64, v1 uint64,num2 float64, v2 uint64) locales.PluralRule {
  175. {{ .RangeFunc }}
  176. }
  177. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  178. func({{ .BaseLocale }} *{{ .Locale }}) MonthAbbreviated(month time.Month) string {
  179. return {{ .BaseLocale }}.monthsAbbreviated[month]
  180. }
  181. // MonthsAbbreviated returns the locales abbreviated months
  182. func({{ .BaseLocale }} *{{ .Locale }}) MonthsAbbreviated() []string {
  183. {{- if gt (len .FmtMonthsAbbreviated) 0 }}
  184. return {{ .BaseLocale }}.monthsAbbreviated[1:]
  185. {{ else }}
  186. return nil
  187. {{- end -}}
  188. }
  189. // MonthNarrow returns the locales narrow month given the 'month' provided
  190. func({{ .BaseLocale }} *{{ .Locale }}) MonthNarrow(month time.Month) string {
  191. return {{ .BaseLocale }}.monthsNarrow[month]
  192. }
  193. // MonthsNarrow returns the locales narrow months
  194. func({{ .BaseLocale }} *{{ .Locale }}) MonthsNarrow() []string {
  195. {{- if gt (len .FmtMonthsNarrow) 0 }}
  196. return {{ .BaseLocale }}.monthsNarrow[1:]
  197. {{ else }}
  198. return nil
  199. {{- end -}}
  200. }
  201. // MonthWide returns the locales wide month given the 'month' provided
  202. func({{ .BaseLocale }} *{{ .Locale }}) MonthWide(month time.Month) string {
  203. return {{ .BaseLocale }}.monthsWide[month]
  204. }
  205. // MonthsWide returns the locales wide months
  206. func({{ .BaseLocale }} *{{ .Locale }}) MonthsWide() []string {
  207. {{- if gt (len .FmtMonthsWide) 0 }}
  208. return {{ .BaseLocale }}.monthsWide[1:]
  209. {{ else }}
  210. return nil
  211. {{- end -}}
  212. }
  213. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  214. func({{ .BaseLocale }} *{{ .Locale }}) WeekdayAbbreviated(weekday time.Weekday) string {
  215. return {{ .BaseLocale }}.daysAbbreviated[weekday]
  216. }
  217. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  218. func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysAbbreviated() []string {
  219. return {{ .BaseLocale }}.daysAbbreviated
  220. }
  221. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  222. func({{ .BaseLocale }} *{{ .Locale }}) WeekdayNarrow(weekday time.Weekday) string {
  223. return {{ .BaseLocale }}.daysNarrow[weekday]
  224. }
  225. // WeekdaysNarrow returns the locales narrow weekdays
  226. func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysNarrow() []string {
  227. return {{ .BaseLocale }}.daysNarrow
  228. }
  229. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  230. func({{ .BaseLocale }} *{{ .Locale }}) WeekdayShort(weekday time.Weekday) string {
  231. return {{ .BaseLocale }}.daysShort[weekday]
  232. }
  233. // WeekdaysShort returns the locales short weekdays
  234. func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysShort() []string {
  235. return {{ .BaseLocale }}.daysShort
  236. }
  237. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  238. func({{ .BaseLocale }} *{{ .Locale }}) WeekdayWide(weekday time.Weekday) string {
  239. return {{ .BaseLocale }}.daysWide[weekday]
  240. }
  241. // WeekdaysWide returns the locales wide weekdays
  242. func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysWide() []string {
  243. return {{ .BaseLocale }}.daysWide
  244. }
  245. // Decimal returns the decimal point of number
  246. func({{ .BaseLocale }} *{{ .Locale }}) Decimal() string {
  247. return {{ .BaseLocale }}.decimal
  248. }
  249. // Group returns the group of number
  250. func({{ .BaseLocale }} *{{ .Locale }}) Group() string {
  251. return {{ .BaseLocale }}.group
  252. }
  253. // Group returns the minus sign of number
  254. func({{ .BaseLocale }} *{{ .Locale }}) Minus() string {
  255. return {{ .BaseLocale }}.minus
  256. }
  257. // FmtNumber returns 'num' with digits/precision of 'v' for '{{ .Locale }}' and handles both Whole and Real numbers based on 'v'
  258. func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) string {
  259. {{ if eq .FmtNumberExists true }}
  260. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  261. {{- if gt .FmtNumberGroupLen 0 }}
  262. {{- $byteCountGroup := byte_count .Group -}}
  263. {{ if ne $byteCountGroup "0" }}
  264. l := len(s) + {{ byte_count .Decimal .Minus }} + {{ $byteCountGroup }} * len(s[:len(s)-int(v)-1]) / {{ .FmtNumberGroupLen }}
  265. {{ else }}
  266. l := len(s) + {{ byte_count .Decimal .Minus }}
  267. {{ end -}}
  268. count := 0
  269. inWhole := v == 0
  270. {{- if gt .FmtNumberSecondaryGroupLen 0}}
  271. inSecondary := false
  272. groupThreshold := {{ .FmtNumberGroupLen }}
  273. {{ end -}}
  274. {{ else }}
  275. l := len(s) + {{ byte_count .Decimal .Minus }}
  276. {{ end }}
  277. b := make([]byte, 0, l)
  278. for i := len(s) - 1; i >= 0; i-- {
  279. if s[i] == '.' {
  280. {{- if is_multibyte .Decimal }}
  281. for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
  282. b = append(b, {{ .BaseLocale }}.decimal[j])
  283. }
  284. {{- else }}
  285. b = append(b, {{ .BaseLocale }}.decimal[0])
  286. {{- end -}}
  287. {{- if gt .FmtNumberGroupLen 0 }}
  288. inWhole = true
  289. {{- end }}
  290. continue
  291. }
  292. {{ if gt .FmtNumberGroupLen 0 }}
  293. if inWhole {
  294. {{- if gt .FmtNumberSecondaryGroupLen 0}}
  295. if count == groupThreshold {
  296. {{- if is_multibyte .Group }}
  297. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  298. b = append(b, {{ .BaseLocale }}.group[j])
  299. }
  300. {{- else }}
  301. b = append(b, {{ .BaseLocale }}.group[0])
  302. {{- end }}
  303. count = 1
  304. if !inSecondary {
  305. inSecondary = true
  306. groupThreshold = {{ .FmtNumberSecondaryGroupLen }}
  307. }
  308. {{ else }}
  309. if count == {{ .FmtNumberGroupLen }} {
  310. {{- if is_multibyte .Group }}
  311. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  312. b = append(b, {{ .BaseLocale }}.group[j])
  313. }
  314. {{- else }}
  315. b = append(b, {{ .BaseLocale }}.group[0])
  316. {{- end }}
  317. count = 1
  318. {{ end -}}
  319. } else {
  320. count++
  321. }
  322. }
  323. {{ end }}
  324. b = append(b, s[i])
  325. }
  326. if num < 0 {
  327. {{- if is_multibyte .Minus }}
  328. for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
  329. b = append(b, {{ .BaseLocale }}.minus[j])
  330. }
  331. {{ else }}
  332. b = append(b, {{ .BaseLocale }}.minus[0])
  333. {{ end -}}
  334. }
  335. // reverse
  336. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  337. b[i], b[j] = b[j], b[i]
  338. }
  339. {{ if gt .FmtNumberMinDecimalLen 0 }}
  340. if int(v) < {{ .FmtNumberMinDecimalLen }} {
  341. if v == 0 {
  342. b = append(b, {{ .BaseLocale }}.decimal...)
  343. }
  344. for i := 0; i < {{ .FmtNumberMinDecimalLen }}-int(v); i++ {
  345. b = append(b, '0')
  346. }
  347. }
  348. {{ end }}
  349. return string(b)
  350. {{ else }}
  351. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  352. {{ end -}}
  353. }
  354. // FmtPercent returns 'num' with digits/precision of 'v' for '{{ .Locale }}' and handles both Whole and Real numbers based on 'v'
  355. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  356. func({{ .BaseLocale }} *{{ .Locale }}) FmtPercent(num float64, v uint64) string {
  357. {{- if eq .FmtPercentExists true }}
  358. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  359. {{- if gt .FmtPercentGroupLen 0 }}
  360. {{- $byteCountGroup := byte_count .Group -}}
  361. {{ if ne $byteCountGroup "0" }}
  362. l := len(s) + {{ byte_count .Decimal .Minus .Percent .FmtPercentPrefix .FmtPercentSuffix }} + {{ $byteCountGroup }} * len(s[:len(s)-int(v)-1]) / {{ .FmtPercentGroupLen }}
  363. {{ else }}
  364. l := len(s) + {{ byte_count .Decimal .Minus .Percent .FmtPercentPrefix .FmtPercentSuffix }}
  365. {{ end -}}
  366. count := 0
  367. inWhole := v == 0
  368. {{- if gt .FmtPercentSecondaryGroupLen 0}}
  369. inSecondary := false
  370. groupThreshold := {{ .FmtPercentGroupLen }}
  371. {{ end -}}
  372. {{ else }}
  373. l := len(s) + {{ byte_count .Decimal .Minus .Percent .FmtPercentPrefix .FmtPercentSuffix }}
  374. {{- end }}
  375. b := make([]byte, 0, l)
  376. for i := len(s) - 1; i >= 0; i-- {
  377. if s[i] == '.' {
  378. {{- if is_multibyte .Decimal }}
  379. for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
  380. b = append(b, {{ .BaseLocale }}.decimal[j])
  381. }
  382. {{- else }}
  383. b = append(b, {{ .BaseLocale }}.decimal[0])
  384. {{- end -}}
  385. {{- if gt .FmtPercentGroupLen 0 }}
  386. inWhole = true
  387. {{ end }}
  388. continue
  389. }
  390. {{ if gt .FmtPercentGroupLen 0 }}
  391. if inWhole {
  392. {{- if gt .FmtPercentSecondaryGroupLen 0}}
  393. if count == groupThreshold {
  394. {{- if is_multibyte .Group }}
  395. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  396. b = append(b, {{ .BaseLocale }}.group[j])
  397. }
  398. {{- else }}
  399. b = append(b, {{ .BaseLocale }}.group[0])
  400. {{- end }}
  401. count = 1
  402. if !inSecondary {
  403. inSecondary = true
  404. groupThreshold = {{ .FmtPercentSecondaryGroupLen }}
  405. }
  406. {{ else }}
  407. if count == {{ .FmtPercentGroupLen }} {
  408. {{- if is_multibyte .Group }}
  409. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  410. b = append(b, {{ .BaseLocale }}.group[j])
  411. }
  412. {{- else }}
  413. b = append(b, {{ .BaseLocale }}.group[0])
  414. {{- end }}
  415. count = 1
  416. {{ end -}}
  417. } else {
  418. count++
  419. }
  420. }
  421. {{ end }}
  422. b = append(b, s[i])
  423. }
  424. if num < 0 {
  425. {{- if is_multibyte .Minus }}
  426. for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
  427. b = append(b, {{ .BaseLocale }}.minus[j])
  428. }
  429. {{ else }}
  430. b = append(b, {{ .BaseLocale }}.minus[0])
  431. {{ end -}}
  432. }
  433. {{ if and .FmtPercentInPrefix (not .FmtPercentLeft) }}
  434. {{- if is_multibyte .Percent }}
  435. for j := len({{ .BaseLocale }}.percent) - 1; j >= 0; j-- {
  436. b = append(b, {{ .BaseLocale }}.percent[j])
  437. }
  438. {{ else }}
  439. b = append(b, {{ .BaseLocale }}.percent[0])
  440. {{ end }}
  441. {{ end }}
  442. {{ if gt (len .FmtPercentPrefix) 0}}
  443. {{- if is_multibyte .FmtPercentPrefix }}
  444. for j := len({{ .BaseLocale }}.percentPrefix) - 1; j >= 0; j-- {
  445. b = append(b, {{ .BaseLocale }}.percentPrefix[j])
  446. }
  447. {{ else }}
  448. b = append(b, {{ .BaseLocale }}.percentPrefix[0])
  449. {{ end }}
  450. {{ end }}
  451. {{ if and .FmtPercentInPrefix .FmtPercentLeft }}
  452. {{- if is_multibyte .Percent }}
  453. for j := len({{ .BaseLocale }}.percent) - 1; j >= 0; j-- {
  454. b = append(b, {{ .BaseLocale }}.percent[j])
  455. }
  456. {{ else }}
  457. b = append(b, {{ .BaseLocale }}.percent[0])
  458. {{ end }}
  459. {{ end }}
  460. // reverse
  461. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  462. b[i], b[j] = b[j], b[i]
  463. }
  464. {{ if gt .FmtPercentMinDecimalLen 0 }}
  465. if int(v) < {{ .FmtPercentMinDecimalLen }} {
  466. if v == 0 {
  467. b = append(b, {{ .BaseLocale }}.decimal...)
  468. }
  469. for i := 0; i < {{ .FmtPercentMinDecimalLen }}-int(v); i++ {
  470. b = append(b, '0')
  471. }
  472. }
  473. {{ end }}
  474. {{ if and (not .FmtPercentInPrefix) .FmtPercentLeft }}
  475. b = append(b, {{ .BaseLocale }}.percent...)
  476. {{ end }}
  477. {{ if gt (len .FmtPercentSuffix) 0}}
  478. b = append(b, {{ .BaseLocale }}.percentSuffix...)
  479. {{ end }}
  480. {{ if and (not .FmtPercentInPrefix) (not .FmtPercentLeft) }}
  481. b = append(b, {{ .BaseLocale }}.percent...)
  482. {{ end }}
  483. return string(b)
  484. {{ else }}
  485. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  486. {{ end -}}
  487. }
  488. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for '{{ .Locale }}'
  489. func({{ .BaseLocale }} *{{ .Locale }}) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  490. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  491. symbol := {{ .BaseLocale }}.currencies[currency]
  492. {{- if eq .FmtCurrencyExists true }}
  493. {{- if gt .FmtCurrencyGroupLen 0 }}
  494. {{- $byteCountGroup := byte_count .Group -}}
  495. {{ if ne $byteCountGroup "0" }}
  496. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyPrefix .FmtCurrencySuffix }} + {{ $byteCountGroup }} * len(s[:len(s)-int(v)-1]) / {{ .FmtCurrencyGroupLen }}
  497. {{ else }}
  498. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyPrefix .FmtCurrencySuffix }}
  499. {{ end -}}
  500. count := 0
  501. inWhole := v == 0
  502. {{- if gt .FmtCurrencySecondaryGroupLen 0}}
  503. inSecondary := false
  504. groupThreshold := {{ .FmtCurrencyGroupLen }}
  505. {{ end -}}
  506. {{ else }}
  507. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyPrefix .FmtCurrencySuffix }}
  508. {{ end }}
  509. b := make([]byte, 0, l)
  510. for i := len(s) - 1; i >= 0; i-- {
  511. if s[i] == '.' {
  512. {{- if is_multibyte .Decimal }}
  513. for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
  514. b = append(b, {{ .BaseLocale }}.decimal[j])
  515. }
  516. {{- else }}
  517. b = append(b, {{ .BaseLocale }}.decimal[0])
  518. {{- end -}}
  519. {{- if gt .FmtCurrencyGroupLen 0 }}
  520. inWhole = true
  521. {{- end }}
  522. continue
  523. }
  524. {{ if gt .FmtCurrencyGroupLen 0 }}
  525. if inWhole {
  526. {{- if gt .FmtCurrencySecondaryGroupLen 0}}
  527. if count == groupThreshold {
  528. {{- if is_multibyte .Group }}
  529. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  530. b = append(b, {{ .BaseLocale }}.group[j])
  531. }
  532. {{- else }}
  533. b = append(b, {{ .BaseLocale }}.group[0])
  534. {{- end }}
  535. count = 1
  536. if !inSecondary {
  537. inSecondary = true
  538. groupThreshold = {{ .FmtCurrencySecondaryGroupLen }}
  539. }
  540. {{ else }}
  541. if count == {{ .FmtCurrencyGroupLen }} {
  542. {{- if is_multibyte .Group }}
  543. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  544. b = append(b, {{ .BaseLocale }}.group[j])
  545. }
  546. {{- else }}
  547. b = append(b, {{ .BaseLocale }}.group[0])
  548. {{- end }}
  549. count = 1
  550. {{ end -}}
  551. } else {
  552. count++
  553. }
  554. }
  555. {{ end }}
  556. b = append(b, s[i])
  557. }
  558. {{ if and .FmtCurrencyInPrefix (not .FmtCurrencyLeft) }}
  559. for j := len(symbol) - 1; j >= 0; j-- {
  560. b = append(b, symbol[j])
  561. }
  562. {{ end }}
  563. {{ if gt (len .FmtCurrencyPrefix) 0}}
  564. {{- if is_multibyte .FmtCurrencyPrefix }}
  565. for j := len({{ .BaseLocale }}.currencyPositivePrefix) - 1; j >= 0; j-- {
  566. b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[j])
  567. }
  568. {{ else }}
  569. b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[0])
  570. {{ end }}
  571. {{ end }}
  572. {{ if and .FmtCurrencyInPrefix .FmtCurrencyLeft }}
  573. for j := len(symbol) - 1; j >= 0; j-- {
  574. b = append(b, symbol[j])
  575. }
  576. {{ end }}
  577. if num < 0 {
  578. {{- if is_multibyte .Minus }}
  579. for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
  580. b = append(b, {{ .BaseLocale }}.minus[j])
  581. }
  582. {{ else -}}
  583. b = append(b, {{ .BaseLocale }}.minus[0])
  584. {{ end -}}
  585. }
  586. // reverse
  587. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  588. b[i], b[j] = b[j], b[i]
  589. }
  590. {{ if gt .FmtCurrencyMinDecimalLen 0 }}
  591. if int(v) < {{ .FmtCurrencyMinDecimalLen }} {
  592. if v == 0 {
  593. b = append(b, {{ .BaseLocale }}.decimal...)
  594. }
  595. for i := 0; i < {{ .FmtCurrencyMinDecimalLen }}-int(v); i++ {
  596. b = append(b, '0')
  597. }
  598. }
  599. {{ end }}
  600. {{ if and (not .FmtCurrencyInPrefix) .FmtCurrencyLeft }}
  601. b = append(b, symbol...)
  602. {{ end }}
  603. {{ if gt (len .FmtCurrencySuffix) 0}}
  604. b = append(b, {{ .BaseLocale }}.currencyPositiveSuffix...)
  605. {{ end }}
  606. {{ if and (not .FmtCurrencyInPrefix) (not .FmtCurrencyLeft) }}
  607. b = append(b, symbol...)
  608. {{ end }}
  609. return string(b)
  610. {{ else }}
  611. return string(append(append([]byte{}, symbol...), s...))
  612. {{ end -}}
  613. }
  614. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for '{{ .Locale }}'
  615. // in accounting notation.
  616. func({{ .BaseLocale }} *{{ .Locale }}) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  617. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  618. symbol := {{ .BaseLocale }}.currencies[currency]
  619. {{- if eq .FmtCurrencyExists true }}
  620. {{- if gt .FmtCurrencyGroupLen 0 }}
  621. {{- $byteCountGroup := byte_count .Group -}}
  622. {{ if ne $byteCountGroup "0" }}
  623. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyNegativePrefix .FmtCurrencyNegativeSuffix }} + {{ $byteCountGroup }} * len(s[:len(s)-int(v)-1]) / {{ .FmtCurrencyGroupLen }}
  624. {{ else }}
  625. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyNegativePrefix .FmtCurrencyNegativeSuffix }}
  626. {{ end -}}
  627. count := 0
  628. inWhole := v == 0
  629. {{- if gt .FmtCurrencySecondaryGroupLen 0}}
  630. inSecondary := false
  631. groupThreshold := {{ .FmtCurrencyGroupLen }}
  632. {{ end -}}
  633. {{ else }}
  634. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyNegativePrefix .FmtCurrencyNegativeSuffix }}
  635. {{ end }}
  636. b := make([]byte, 0, l)
  637. for i := len(s) - 1; i >= 0; i-- {
  638. if s[i] == '.' {
  639. {{- if is_multibyte .Decimal }}
  640. for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
  641. b = append(b, {{ .BaseLocale }}.decimal[j])
  642. }
  643. {{- else }}
  644. b = append(b, {{ .BaseLocale }}.decimal[0])
  645. {{- end -}}
  646. {{- if gt .FmtCurrencyGroupLen 0 }}
  647. inWhole = true
  648. {{- end }}
  649. continue
  650. }
  651. {{ if gt .FmtCurrencyGroupLen 0 }}
  652. if inWhole {
  653. {{- if gt .FmtCurrencySecondaryGroupLen 0}}
  654. if count == groupThreshold {
  655. {{- if is_multibyte .Group }}
  656. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  657. b = append(b, {{ .BaseLocale }}.group[j])
  658. }
  659. {{- else }}
  660. b = append(b, {{ .BaseLocale }}.group[0])
  661. {{- end }}
  662. count = 1
  663. if !inSecondary {
  664. inSecondary = true
  665. groupThreshold = {{ .FmtCurrencySecondaryGroupLen }}
  666. }
  667. {{ else }}
  668. if count == {{ .FmtCurrencyGroupLen }} {
  669. {{- if is_multibyte .Group }}
  670. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  671. b = append(b, {{ .BaseLocale }}.group[j])
  672. }
  673. {{- else }}
  674. b = append(b, {{ .BaseLocale }}.group[0])
  675. {{- end }}
  676. count = 1
  677. {{ end -}}
  678. } else {
  679. count++
  680. }
  681. }
  682. {{ end }}
  683. b = append(b, s[i])
  684. }
  685. if num < 0 {
  686. {{ if and .FmtCurrencyNegativeInPrefix (not .FmtCurrencyNegativeLeft) }}
  687. for j := len(symbol) - 1; j >= 0; j-- {
  688. b = append(b, symbol[j])
  689. }
  690. {{ end }}
  691. {{ if gt (len .FmtCurrencyNegativePrefix) 0}}
  692. {{- if is_multibyte .FmtCurrencyNegativePrefix }}
  693. for j := len({{ .BaseLocale }}.currencyNegativePrefix) - 1; j >= 0; j-- {
  694. b = append(b, {{ .BaseLocale }}.currencyNegativePrefix[j])
  695. }
  696. {{ else }}
  697. b = append(b, {{ .BaseLocale }}.currencyNegativePrefix[0])
  698. {{ end }}
  699. {{ end }}
  700. {{ if and .FmtCurrencyNegativeInPrefix .FmtCurrencyNegativeLeft }}
  701. for j := len(symbol) - 1; j >= 0; j-- {
  702. b = append(b, symbol[j])
  703. }
  704. {{ end }}
  705. {{ if eq (not .FmtCurrencyNegativeExists) true}}
  706. {{- if is_multibyte .Minus }}
  707. for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
  708. b = append(b, {{ .BaseLocale }}.minus[j])
  709. }
  710. {{ else -}}
  711. b = append(b, {{ .BaseLocale }}.minus[0])
  712. {{ end -}}
  713. {{ end }}
  714. {{ if or .FmtCurrencyInPrefix (gt (len .FmtCurrencyPrefix) 0) }}
  715. } else {
  716. {{ end }}
  717. {{ if and .FmtCurrencyInPrefix (not .FmtCurrencyLeft) }}
  718. for j := len(symbol) - 1; j >= 0; j-- {
  719. b = append(b, symbol[j])
  720. }
  721. {{ end }}
  722. {{ if gt (len .FmtCurrencyPrefix) 0}}
  723. {{- if is_multibyte .FmtCurrencyPrefix }}
  724. for j := len({{ .BaseLocale }}.currencyPositivePrefix) - 1; j >= 0; j-- {
  725. b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[j])
  726. }
  727. {{ else }}
  728. b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[0])
  729. {{ end }}
  730. {{ end }}
  731. {{ if and .FmtCurrencyInPrefix .FmtCurrencyLeft }}
  732. for j := len(symbol) - 1; j >= 0; j-- {
  733. b = append(b, symbol[j])
  734. }
  735. {{- end }}
  736. }
  737. // reverse
  738. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  739. b[i], b[j] = b[j], b[i]
  740. }
  741. {{ if gt .FmtCurrencyMinDecimalLen 0 }}
  742. if int(v) < {{ .FmtCurrencyMinDecimalLen }} {
  743. if v == 0 {
  744. b = append(b, {{ .BaseLocale }}.decimal...)
  745. }
  746. for i := 0; i < {{ .FmtCurrencyMinDecimalLen }}-int(v); i++ {
  747. b = append(b, '0')
  748. }
  749. }
  750. {{- end }}
  751. {{ if or (not .FmtCurrencyNegativeInPrefix) (gt (len .FmtCurrencyNegativeSuffix) 0)}}
  752. if num < 0 {
  753. {{- end }}
  754. {{- if and (not .FmtCurrencyNegativeInPrefix) .FmtCurrencyNegativeLeft }}
  755. b = append(b, symbol...)
  756. {{- end -}}
  757. {{- if gt (len .FmtCurrencyNegativeSuffix) 0}}
  758. b = append(b, {{ .BaseLocale }}.currencyNegativeSuffix...)
  759. {{- end -}}
  760. {{- if and (not .FmtCurrencyNegativeInPrefix) (not .FmtCurrencyNegativeLeft) }}
  761. b = append(b, symbol...)
  762. {{- end -}}
  763. {{ if or (not .FmtCurrencyInPrefix) (gt (len .FmtCurrencySuffix) 0)}}
  764. } else {
  765. {{ end }}
  766. {{- if and (not .FmtCurrencyInPrefix) .FmtCurrencyLeft }}
  767. b = append(b, symbol...)
  768. {{- end -}}
  769. {{- if gt (len .FmtCurrencySuffix) 0}}
  770. b = append(b, {{ .BaseLocale }}.currencyPositiveSuffix...)
  771. {{- end -}}
  772. {{- if and (not .FmtCurrencyInPrefix) (not .FmtCurrencyLeft) }}
  773. b = append(b, symbol...)
  774. {{- end -}}
  775. {{- if or (not .FmtCurrencyNegativeInPrefix) (gt (len .FmtCurrencyNegativeSuffix) 0)}}
  776. }
  777. {{- end }}
  778. return string(b)
  779. {{ else }}
  780. return string(append(append([]byte{}, symbol...), s...))
  781. {{ end -}}
  782. }
  783. // FmtDateShort returns the short date representation of 't' for '{{ .Locale }}'
  784. func({{ .BaseLocale }} *{{ .Locale }}) FmtDateShort(t time.Time) string {
  785. b := make([]byte, 0, 32)
  786. {{ .FmtDateShort }}
  787. return string(b)
  788. }
  789. // FmtDateMedium returns the medium date representation of 't' for '{{ .Locale }}'
  790. func({{ .BaseLocale }} *{{ .Locale }}) FmtDateMedium(t time.Time) string {
  791. b := make([]byte, 0, 32)
  792. {{ .FmtDateMedium }}
  793. return string(b)
  794. }
  795. // FmtDateLong returns the long date representation of 't' for '{{ .Locale }}'
  796. func({{ .BaseLocale }} *{{ .Locale }}) FmtDateLong(t time.Time) string {
  797. b := make([]byte, 0, 32)
  798. {{ .FmtDateLong }}
  799. return string(b)
  800. }
  801. // FmtDateFull returns the full date representation of 't' for '{{ .Locale }}'
  802. func({{ .BaseLocale }} *{{ .Locale }}) FmtDateFull(t time.Time) string {
  803. b := make([]byte, 0, 32)
  804. {{ .FmtDateFull }}
  805. return string(b)
  806. }
  807. // FmtTimeShort returns the short time representation of 't' for '{{ .Locale }}'
  808. func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeShort(t time.Time) string {
  809. b := make([]byte, 0, 32)
  810. {{ .FmtTimeShort }}
  811. return string(b)
  812. }
  813. // FmtTimeMedium returns the medium time representation of 't' for '{{ .Locale }}'
  814. func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeMedium(t time.Time) string {
  815. b := make([]byte, 0, 32)
  816. {{ .FmtTimeMedium }}
  817. return string(b)
  818. }
  819. // FmtTimeLong returns the long time representation of 't' for '{{ .Locale }}'
  820. func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeLong(t time.Time) string {
  821. b := make([]byte, 0, 32)
  822. {{ .FmtTimeLong }}
  823. return string(b)
  824. }
  825. // FmtTimeFull returns the full time representation of 't' for '{{ .Locale }}'
  826. func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeFull(t time.Time) string {
  827. b := make([]byte, 0, 32)
  828. {{ .FmtTimeFull }}
  829. return string(b)
  830. }
  831. {{ end }}