translator.tmpl 26 KB

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