translator.tmpl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. return {{ .BaseLocale }}.monthsAbbreviated[1:]
  184. }
  185. // MonthNarrow returns the locales narrow month given the 'month' provided
  186. func({{ .BaseLocale }} *{{ .Locale }}) MonthNarrow(month time.Month) string {
  187. return {{ .BaseLocale }}.monthsNarrow[month]
  188. }
  189. // MonthsNarrow returns the locales narrow months
  190. func({{ .BaseLocale }} *{{ .Locale }}) MonthsNarrow() []string {
  191. return {{ .BaseLocale }}.monthsNarrow[1:]
  192. }
  193. // MonthWide returns the locales wide month given the 'month' provided
  194. func({{ .BaseLocale }} *{{ .Locale }}) MonthWide(month time.Month) string {
  195. return {{ .BaseLocale }}.monthsWide[month]
  196. }
  197. // MonthsWide returns the locales wide months
  198. func({{ .BaseLocale }} *{{ .Locale }}) MonthsWide() []string {
  199. return {{ .BaseLocale }}.monthsWide[1:]
  200. }
  201. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  202. func({{ .BaseLocale }} *{{ .Locale }}) WeekdayAbbreviated(weekday time.Weekday) string {
  203. return {{ .BaseLocale }}.daysAbbreviated[weekday]
  204. }
  205. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  206. func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysAbbreviated() []string {
  207. return {{ .BaseLocale }}.daysAbbreviated
  208. }
  209. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  210. func({{ .BaseLocale }} *{{ .Locale }}) WeekdayNarrow(weekday time.Weekday) string {
  211. return {{ .BaseLocale }}.daysNarrow[weekday]
  212. }
  213. // WeekdaysNarrow returns the locales narrow weekdays
  214. func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysNarrow() []string {
  215. return {{ .BaseLocale }}.daysNarrow
  216. }
  217. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  218. func({{ .BaseLocale }} *{{ .Locale }}) WeekdayShort(weekday time.Weekday) string {
  219. return {{ .BaseLocale }}.daysShort[weekday]
  220. }
  221. // WeekdaysShort returns the locales short weekdays
  222. func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysShort() []string {
  223. return {{ .BaseLocale }}.daysShort
  224. }
  225. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  226. func({{ .BaseLocale }} *{{ .Locale }}) WeekdayWide(weekday time.Weekday) string {
  227. return {{ .BaseLocale }}.daysWide[weekday]
  228. }
  229. // WeekdaysWide returns the locales wide weekdays
  230. func({{ .BaseLocale }} *{{ .Locale }}) WeekdaysWide() []string {
  231. return {{ .BaseLocale }}.daysWide
  232. }
  233. // FmtNumber returns 'num' with digits/precision of 'v' for '{{ .Locale }}' and handles both Whole and Real numbers based on 'v'
  234. func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) string {
  235. {{ if eq .FmtNumberExists true }}
  236. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  237. {{- if gt .FmtNumberGroupLen 0 }}
  238. l := len(s) + {{ byte_count .Decimal .Minus }} + {{ byte_count .Group }} * len(s[:len(s)-int(v)-1]) / {{ .FmtNumberGroupLen }}
  239. count := 0
  240. inWhole := v == 0
  241. {{- if gt .FmtNumberSecondaryGroupLen 0}}
  242. inSecondary := false
  243. groupThreshold := {{ .FmtNumberGroupLen }}
  244. {{ end -}}
  245. {{ else }}
  246. l := len(s) + {{ byte_count .Decimal .Minus }}
  247. {{ end }}
  248. b := make([]byte, 0, l)
  249. for i := len(s) - 1; i >= 0; i-- {
  250. if s[i] == '.' {
  251. {{- if is_multibyte .Decimal }}
  252. for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
  253. b = append(b, {{ .BaseLocale }}.decimal[j])
  254. }
  255. {{- else }}
  256. b = append(b, {{ .BaseLocale }}.decimal[0])
  257. {{- end -}}
  258. {{- if gt .FmtNumberGroupLen 0 }}
  259. inWhole = true
  260. {{- end }}
  261. continue
  262. }
  263. {{ if gt .FmtNumberGroupLen 0 }}
  264. if inWhole {
  265. {{- if gt .FmtNumberSecondaryGroupLen 0}}
  266. if count == groupThreshold {
  267. {{- if is_multibyte .Group }}
  268. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  269. b = append(b, {{ .BaseLocale }}.group[j])
  270. }
  271. {{- else }}
  272. b = append(b, {{ .BaseLocale }}.group[0])
  273. {{- end }}
  274. count = 1
  275. if !inSecondary {
  276. inSecondary = true
  277. groupThreshold = {{ .FmtNumberSecondaryGroupLen }}
  278. }
  279. {{ else }}
  280. if count == {{ .FmtNumberGroupLen }} {
  281. {{- if is_multibyte .Group }}
  282. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  283. b = append(b, {{ .BaseLocale }}.group[j])
  284. }
  285. {{- else }}
  286. b = append(b, {{ .BaseLocale }}.group[0])
  287. {{- end }}
  288. count = 1
  289. {{ end -}}
  290. } else {
  291. count++
  292. }
  293. }
  294. {{ end }}
  295. b = append(b, s[i])
  296. }
  297. if num < 0 {
  298. {{- if is_multibyte .Minus }}
  299. for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
  300. b = append(b, {{ .BaseLocale }}.minus[j])
  301. }
  302. {{ else }}
  303. b = append(b, {{ .BaseLocale }}.minus[0])
  304. {{ end -}}
  305. }
  306. // reverse
  307. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  308. b[i], b[j] = b[j], b[i]
  309. }
  310. {{ if gt .FmtNumberMinDecimalLen 0 }}
  311. if int(v) < {{ .FmtNumberMinDecimalLen }} {
  312. if v == 0 {
  313. b = append(b, {{ .BaseLocale }}.decimal...)
  314. }
  315. for i := 0; i < {{ .FmtNumberMinDecimalLen }}-int(v); i++ {
  316. b = append(b, '0')
  317. }
  318. }
  319. {{ end }}
  320. return string(b)
  321. {{ else }}
  322. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  323. {{ end -}}
  324. }
  325. // FmtPercent returns 'num' with digits/precision of 'v' for '{{ .Locale }}' and handles both Whole and Real numbers based on 'v'
  326. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  327. func({{ .BaseLocale }} *{{ .Locale }}) FmtPercent(num float64, v uint64) string {
  328. {{- if eq .FmtPercentExists true }}
  329. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  330. {{- if gt .FmtPercentGroupLen 0 }}
  331. l := len(s) + {{ byte_count .Decimal .Minus .Percent .FmtPercentPrefix .FmtPercentSuffix }} + {{ byte_count .Group }} * len(s[:len(s)-int(v)-1]) / {{ .FmtPercentGroupLen }}
  332. count := 0
  333. inWhole := v == 0
  334. {{- if gt .FmtPercentSecondaryGroupLen 0}}
  335. inSecondary := false
  336. groupThreshold := {{ .FmtPercentGroupLen }}
  337. {{ end -}}
  338. {{ else }}
  339. l := len(s) + {{ byte_count .Decimal .Minus .Percent .FmtPercentPrefix .FmtPercentSuffix }}
  340. {{- end }}
  341. b := make([]byte, 0, l)
  342. for i := len(s) - 1; i >= 0; i-- {
  343. if s[i] == '.' {
  344. {{- if is_multibyte .Decimal }}
  345. for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
  346. b = append(b, {{ .BaseLocale }}.decimal[j])
  347. }
  348. {{- else }}
  349. b = append(b, {{ .BaseLocale }}.decimal[0])
  350. {{- end -}}
  351. {{- if gt .FmtPercentGroupLen 0 }}
  352. inWhole = true
  353. {{ end }}
  354. continue
  355. }
  356. {{ if gt .FmtPercentGroupLen 0 }}
  357. if inWhole {
  358. {{- if gt .FmtPercentSecondaryGroupLen 0}}
  359. if count == groupThreshold {
  360. {{- if is_multibyte .Group }}
  361. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  362. b = append(b, {{ .BaseLocale }}.group[j])
  363. }
  364. {{- else }}
  365. b = append(b, {{ .BaseLocale }}.group[0])
  366. {{- end }}
  367. count = 1
  368. if !inSecondary {
  369. inSecondary = true
  370. groupThreshold = {{ .FmtPercentSecondaryGroupLen }}
  371. }
  372. {{ else }}
  373. if count == {{ .FmtPercentGroupLen }} {
  374. {{- if is_multibyte .Group }}
  375. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  376. b = append(b, {{ .BaseLocale }}.group[j])
  377. }
  378. {{- else }}
  379. b = append(b, {{ .BaseLocale }}.group[0])
  380. {{- end }}
  381. count = 1
  382. {{ end -}}
  383. } else {
  384. count++
  385. }
  386. }
  387. {{ end }}
  388. b = append(b, s[i])
  389. }
  390. if num < 0 {
  391. {{- if is_multibyte .Minus }}
  392. for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
  393. b = append(b, {{ .BaseLocale }}.minus[j])
  394. }
  395. {{ else }}
  396. b = append(b, {{ .BaseLocale }}.minus[0])
  397. {{ end -}}
  398. }
  399. {{ if and .FmtPercentInPrefix (not .FmtPercentLeft) }}
  400. {{- if is_multibyte .Percent }}
  401. for j := len({{ .BaseLocale }}.percent) - 1; j >= 0; j-- {
  402. b = append(b, {{ .BaseLocale }}.percent[j])
  403. }
  404. {{ else }}
  405. b = append(b, {{ .BaseLocale }}.percent[0])
  406. {{ end }}
  407. {{ end }}
  408. {{ if gt (len .FmtPercentPrefix) 0}}
  409. {{- if is_multibyte .FmtPercentPrefix }}
  410. for j := len({{ .BaseLocale }}.percentPrefix) - 1; j >= 0; j-- {
  411. b = append(b, {{ .BaseLocale }}.percentPrefix[j])
  412. }
  413. {{ else }}
  414. b = append(b, {{ .BaseLocale }}.percentPrefix[0])
  415. {{ end }}
  416. {{ end }}
  417. {{ if and .FmtPercentInPrefix .FmtPercentLeft }}
  418. {{- if is_multibyte .Percent }}
  419. for j := len({{ .BaseLocale }}.percent) - 1; j >= 0; j-- {
  420. b = append(b, {{ .BaseLocale }}.percent[j])
  421. }
  422. {{ else }}
  423. b = append(b, {{ .BaseLocale }}.percent[0])
  424. {{ end }}
  425. {{ end }}
  426. // reverse
  427. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  428. b[i], b[j] = b[j], b[i]
  429. }
  430. {{ if gt .FmtPercentMinDecimalLen 0 }}
  431. if int(v) < {{ .FmtPercentMinDecimalLen }} {
  432. if v == 0 {
  433. b = append(b, {{ .BaseLocale }}.decimal...)
  434. }
  435. for i := 0; i < {{ .FmtPercentMinDecimalLen }}-int(v); i++ {
  436. b = append(b, '0')
  437. }
  438. }
  439. {{ end }}
  440. {{ if and (not .FmtPercentInPrefix) .FmtPercentLeft }}
  441. b = append(b, {{ .BaseLocale }}.percent...)
  442. {{ end }}
  443. {{ if gt (len .FmtPercentSuffix) 0}}
  444. b = append(b, {{ .BaseLocale }}.percentSuffix...)
  445. {{ end }}
  446. {{ if and (not .FmtPercentInPrefix) (not .FmtPercentLeft) }}
  447. b = append(b, {{ .BaseLocale }}.percent...)
  448. {{ end }}
  449. return string(b)
  450. {{ else }}
  451. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  452. {{ end -}}
  453. }
  454. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for '{{ .Locale }}'
  455. func({{ .BaseLocale }} *{{ .Locale }}) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  456. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  457. symbol := {{ .BaseLocale }}.currencies[currency]
  458. {{- if eq .FmtCurrencyExists true }}
  459. {{- if gt .FmtCurrencyGroupLen 0 }}
  460. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyPrefix .FmtCurrencySuffix }} + {{ byte_count .Group }} * len(s[:len(s)-int(v)-1]) / {{ .FmtCurrencyGroupLen }}
  461. count := 0
  462. inWhole := v == 0
  463. {{- if gt .FmtCurrencySecondaryGroupLen 0}}
  464. inSecondary := false
  465. groupThreshold := {{ .FmtCurrencyGroupLen }}
  466. {{ end -}}
  467. {{ else }}
  468. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyPrefix .FmtCurrencySuffix }}
  469. {{ end }}
  470. b := make([]byte, 0, l)
  471. for i := len(s) - 1; i >= 0; i-- {
  472. if s[i] == '.' {
  473. {{- if is_multibyte .Decimal }}
  474. for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
  475. b = append(b, {{ .BaseLocale }}.decimal[j])
  476. }
  477. {{- else }}
  478. b = append(b, {{ .BaseLocale }}.decimal[0])
  479. {{- end -}}
  480. {{- if gt .FmtCurrencyGroupLen 0 }}
  481. inWhole = true
  482. {{- end }}
  483. continue
  484. }
  485. {{ if gt .FmtCurrencyGroupLen 0 }}
  486. if inWhole {
  487. {{- if gt .FmtCurrencySecondaryGroupLen 0}}
  488. if count == groupThreshold {
  489. {{- if is_multibyte .Group }}
  490. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  491. b = append(b, {{ .BaseLocale }}.group[j])
  492. }
  493. {{- else }}
  494. b = append(b, {{ .BaseLocale }}.group[0])
  495. {{- end }}
  496. count = 1
  497. if !inSecondary {
  498. inSecondary = true
  499. groupThreshold = {{ .FmtCurrencySecondaryGroupLen }}
  500. }
  501. {{ else }}
  502. if count == {{ .FmtCurrencyGroupLen }} {
  503. {{- if is_multibyte .Group }}
  504. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  505. b = append(b, {{ .BaseLocale }}.group[j])
  506. }
  507. {{- else }}
  508. b = append(b, {{ .BaseLocale }}.group[0])
  509. {{- end }}
  510. count = 1
  511. {{ end -}}
  512. } else {
  513. count++
  514. }
  515. }
  516. {{ end }}
  517. b = append(b, s[i])
  518. }
  519. {{ if and .FmtCurrencyInPrefix (not .FmtCurrencyLeft) }}
  520. for j := len(symbol) - 1; j >= 0; j-- {
  521. b = append(b, symbol[j])
  522. }
  523. {{ end }}
  524. {{ if gt (len .FmtCurrencyPrefix) 0}}
  525. {{- if is_multibyte .FmtCurrencyPrefix }}
  526. for j := len({{ .BaseLocale }}.currencyPositivePrefix) - 1; j >= 0; j-- {
  527. b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[j])
  528. }
  529. {{ else }}
  530. b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[0])
  531. {{ end }}
  532. {{ end }}
  533. {{ if and .FmtCurrencyInPrefix .FmtCurrencyLeft }}
  534. for j := len(symbol) - 1; j >= 0; j-- {
  535. b = append(b, symbol[j])
  536. }
  537. {{ end }}
  538. if num < 0 {
  539. {{- if is_multibyte .Minus }}
  540. for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
  541. b = append(b, {{ .BaseLocale }}.minus[j])
  542. }
  543. {{ else -}}
  544. b = append(b, {{ .BaseLocale }}.minus[0])
  545. {{ end -}}
  546. }
  547. // reverse
  548. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  549. b[i], b[j] = b[j], b[i]
  550. }
  551. {{ if gt .FmtCurrencyMinDecimalLen 0 }}
  552. if int(v) < {{ .FmtCurrencyMinDecimalLen }} {
  553. if v == 0 {
  554. b = append(b, {{ .BaseLocale }}.decimal...)
  555. }
  556. for i := 0; i < {{ .FmtCurrencyMinDecimalLen }}-int(v); i++ {
  557. b = append(b, '0')
  558. }
  559. }
  560. {{ end }}
  561. {{ if and (not .FmtCurrencyInPrefix) .FmtCurrencyLeft }}
  562. b = append(b, symbol...)
  563. {{ end }}
  564. {{ if gt (len .FmtCurrencySuffix) 0}}
  565. b = append(b, {{ .BaseLocale }}.currencyPositiveSuffix...)
  566. {{ end }}
  567. {{ if and (not .FmtCurrencyInPrefix) (not .FmtCurrencyLeft) }}
  568. b = append(b, symbol...)
  569. {{ end }}
  570. return string(b)
  571. {{ else }}
  572. return string(append(append([]byte{}, symbol...), s...))
  573. {{ end -}}
  574. }
  575. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for '{{ .Locale }}'
  576. // in accounting notation.
  577. func({{ .BaseLocale }} *{{ .Locale }}) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  578. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  579. symbol := {{ .BaseLocale }}.currencies[currency]
  580. {{- if eq .FmtCurrencyExists true }}
  581. {{- if gt .FmtCurrencyGroupLen 0 }}
  582. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyNegativePrefix .FmtCurrencyNegativeSuffix }} + {{ byte_count .Group }} * len(s[:len(s)-int(v)-1]) / {{ .FmtCurrencyGroupLen }}
  583. count := 0
  584. inWhole := v == 0
  585. {{- if gt .FmtCurrencySecondaryGroupLen 0}}
  586. inSecondary := false
  587. groupThreshold := {{ .FmtCurrencyGroupLen }}
  588. {{ end -}}
  589. {{ else }}
  590. l := len(s) + len(symbol) + {{ byte_count .Decimal .Minus .FmtCurrencyNegativePrefix .FmtCurrencyNegativeSuffix }}
  591. {{ end }}
  592. b := make([]byte, 0, l)
  593. for i := len(s) - 1; i >= 0; i-- {
  594. if s[i] == '.' {
  595. {{- if is_multibyte .Decimal }}
  596. for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- {
  597. b = append(b, {{ .BaseLocale }}.decimal[j])
  598. }
  599. {{- else }}
  600. b = append(b, {{ .BaseLocale }}.decimal[0])
  601. {{- end -}}
  602. {{- if gt .FmtCurrencyGroupLen 0 }}
  603. inWhole = true
  604. {{- end }}
  605. continue
  606. }
  607. {{ if gt .FmtCurrencyGroupLen 0 }}
  608. if inWhole {
  609. {{- if gt .FmtCurrencySecondaryGroupLen 0}}
  610. if count == groupThreshold {
  611. {{- if is_multibyte .Group }}
  612. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  613. b = append(b, {{ .BaseLocale }}.group[j])
  614. }
  615. {{- else }}
  616. b = append(b, {{ .BaseLocale }}.group[0])
  617. {{- end }}
  618. count = 1
  619. if !inSecondary {
  620. inSecondary = true
  621. groupThreshold = {{ .FmtCurrencySecondaryGroupLen }}
  622. }
  623. {{ else }}
  624. if count == {{ .FmtCurrencyGroupLen }} {
  625. {{- if is_multibyte .Group }}
  626. for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- {
  627. b = append(b, {{ .BaseLocale }}.group[j])
  628. }
  629. {{- else }}
  630. b = append(b, {{ .BaseLocale }}.group[0])
  631. {{- end }}
  632. count = 1
  633. {{ end -}}
  634. } else {
  635. count++
  636. }
  637. }
  638. {{ end }}
  639. b = append(b, s[i])
  640. }
  641. if num < 0 {
  642. {{ if and .FmtCurrencyNegativeInPrefix (not .FmtCurrencyNegativeLeft) }}
  643. for j := len(symbol) - 1; j >= 0; j-- {
  644. b = append(b, symbol[j])
  645. }
  646. {{ end }}
  647. {{ if gt (len .FmtCurrencyNegativePrefix) 0}}
  648. {{- if is_multibyte .FmtCurrencyNegativePrefix }}
  649. for j := len({{ .BaseLocale }}.currencyNegativePrefix) - 1; j >= 0; j-- {
  650. b = append(b, {{ .BaseLocale }}.currencyNegativePrefix[j])
  651. }
  652. {{ else }}
  653. b = append(b, {{ .BaseLocale }}.currencyNegativePrefix[0])
  654. {{ end }}
  655. {{ end }}
  656. {{ if and .FmtCurrencyNegativeInPrefix .FmtCurrencyNegativeLeft }}
  657. for j := len(symbol) - 1; j >= 0; j-- {
  658. b = append(b, symbol[j])
  659. }
  660. {{ end }}
  661. {{ if eq (not .FmtCurrencyNegativeExists) true}}
  662. {{- if is_multibyte .Minus }}
  663. for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- {
  664. b = append(b, {{ .BaseLocale }}.minus[j])
  665. }
  666. {{ else -}}
  667. b = append(b, {{ .BaseLocale }}.minus[0])
  668. {{ end -}}
  669. {{ end }}
  670. {{ if or .FmtCurrencyInPrefix (gt (len .FmtCurrencyPrefix) 0) }}
  671. } else {
  672. {{ end }}
  673. {{ if and .FmtCurrencyInPrefix (not .FmtCurrencyLeft) }}
  674. for j := len(symbol) - 1; j >= 0; j-- {
  675. b = append(b, symbol[j])
  676. }
  677. {{ end }}
  678. {{ if gt (len .FmtCurrencyPrefix) 0}}
  679. {{- if is_multibyte .FmtCurrencyPrefix }}
  680. for j := len({{ .BaseLocale }}.currencyPositivePrefix) - 1; j >= 0; j-- {
  681. b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[j])
  682. }
  683. {{ else }}
  684. b = append(b, {{ .BaseLocale }}.currencyPositivePrefix[0])
  685. {{ end }}
  686. {{ end }}
  687. {{ if and .FmtCurrencyInPrefix .FmtCurrencyLeft }}
  688. for j := len(symbol) - 1; j >= 0; j-- {
  689. b = append(b, symbol[j])
  690. }
  691. {{- end }}
  692. }
  693. // reverse
  694. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  695. b[i], b[j] = b[j], b[i]
  696. }
  697. {{ if gt .FmtCurrencyMinDecimalLen 0 }}
  698. if int(v) < {{ .FmtCurrencyMinDecimalLen }} {
  699. if v == 0 {
  700. b = append(b, {{ .BaseLocale }}.decimal...)
  701. }
  702. for i := 0; i < {{ .FmtCurrencyMinDecimalLen }}-int(v); i++ {
  703. b = append(b, '0')
  704. }
  705. }
  706. {{- end }}
  707. {{ if or (not .FmtCurrencyNegativeInPrefix) (gt (len .FmtCurrencyNegativeSuffix) 0)}}
  708. if num < 0 {
  709. {{- end }}
  710. {{- if and (not .FmtCurrencyNegativeInPrefix) .FmtCurrencyNegativeLeft }}
  711. b = append(b, symbol...)
  712. {{- end -}}
  713. {{- if gt (len .FmtCurrencyNegativeSuffix) 0}}
  714. b = append(b, {{ .BaseLocale }}.currencyNegativeSuffix...)
  715. {{- end -}}
  716. {{- if and (not .FmtCurrencyNegativeInPrefix) (not .FmtCurrencyNegativeLeft) }}
  717. b = append(b, symbol...)
  718. {{- end -}}
  719. {{ if or (not .FmtCurrencyInPrefix) (gt (len .FmtCurrencySuffix) 0)}}
  720. } else {
  721. {{ end }}
  722. {{- if and (not .FmtCurrencyInPrefix) .FmtCurrencyLeft }}
  723. b = append(b, symbol...)
  724. {{- end -}}
  725. {{- if gt (len .FmtCurrencySuffix) 0}}
  726. b = append(b, {{ .BaseLocale }}.currencyPositiveSuffix...)
  727. {{- end -}}
  728. {{- if and (not .FmtCurrencyInPrefix) (not .FmtCurrencyLeft) }}
  729. b = append(b, symbol...)
  730. {{- end -}}
  731. {{- if or (not .FmtCurrencyNegativeInPrefix) (gt (len .FmtCurrencyNegativeSuffix) 0)}}
  732. }
  733. {{- end }}
  734. return string(b)
  735. {{ else }}
  736. return string(append(append([]byte{}, symbol...), s...))
  737. {{ end -}}
  738. }
  739. // FmtDateShort returns the short date representation of 't' for '{{ .Locale }}'
  740. func({{ .BaseLocale }} *{{ .Locale }}) FmtDateShort(t time.Time) string {
  741. b := make([]byte, 0, 32)
  742. {{ .FmtDateShort }}
  743. return string(b)
  744. }
  745. // FmtDateMedium returns the medium date representation of 't' for '{{ .Locale }}'
  746. func({{ .BaseLocale }} *{{ .Locale }}) FmtDateMedium(t time.Time) string {
  747. b := make([]byte, 0, 32)
  748. {{ .FmtDateMedium }}
  749. return string(b)
  750. }
  751. // FmtDateLong returns the long date representation of 't' for '{{ .Locale }}'
  752. func({{ .BaseLocale }} *{{ .Locale }}) FmtDateLong(t time.Time) string {
  753. b := make([]byte, 0, 32)
  754. {{ .FmtDateLong }}
  755. return string(b)
  756. }
  757. // FmtDateFull returns the full date representation of 't' for '{{ .Locale }}'
  758. func({{ .BaseLocale }} *{{ .Locale }}) FmtDateFull(t time.Time) string {
  759. b := make([]byte, 0, 32)
  760. {{ .FmtDateFull }}
  761. return string(b)
  762. }
  763. // FmtTimeShort returns the short time representation of 't' for '{{ .Locale }}'
  764. func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeShort(t time.Time) string {
  765. b := make([]byte, 0, 32)
  766. {{ .FmtTimeShort }}
  767. return string(b)
  768. }
  769. // FmtTimeMedium returns the medium time representation of 't' for '{{ .Locale }}'
  770. func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeMedium(t time.Time) string {
  771. b := make([]byte, 0, 32)
  772. {{ .FmtTimeMedium }}
  773. return string(b)
  774. }
  775. // FmtTimeLong returns the long time representation of 't' for '{{ .Locale }}'
  776. func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeLong(t time.Time) string {
  777. b := make([]byte, 0, 32)
  778. {{ .FmtTimeLong }}
  779. return string(b)
  780. }
  781. // FmtTimeFull returns the full time representation of 't' for '{{ .Locale }}'
  782. func({{ .BaseLocale }} *{{ .Locale }}) FmtTimeFull(t time.Time) string {
  783. b := make([]byte, 0, 32)
  784. {{ .FmtTimeFull }}
  785. return string(b)
  786. }
  787. {{ end }}