br.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package br
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type br struct {
  10. locale string
  11. pluralsCardinal []locales.PluralRule
  12. pluralsOrdinal []locales.PluralRule
  13. pluralsRange []locales.PluralRule
  14. decimal string
  15. group string
  16. minus string
  17. percent string
  18. percentSuffix string
  19. perMille string
  20. timeSeparator string
  21. inifinity string
  22. currencies []string // idx = enum of currency code
  23. currencyPositiveSuffix string
  24. currencyNegativeSuffix string
  25. monthsAbbreviated []string
  26. monthsNarrow []string
  27. monthsWide []string
  28. daysAbbreviated []string
  29. daysNarrow []string
  30. daysShort []string
  31. daysWide []string
  32. periodsAbbreviated []string
  33. periodsNarrow []string
  34. periodsShort []string
  35. periodsWide []string
  36. erasAbbreviated []string
  37. erasNarrow []string
  38. erasWide []string
  39. timezones map[string]string
  40. }
  41. // New returns a new instance of translator for the 'br' locale
  42. func New() locales.Translator {
  43. return &br{
  44. locale: "br",
  45. pluralsCardinal: []locales.PluralRule{2, 3, 4, 5, 6},
  46. pluralsOrdinal: nil,
  47. pluralsRange: nil,
  48. timeSeparator: ":",
  49. currencies: []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "AOA", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "ARS", "ATS", "$A", "AWG", "AZM", "AZN", "BAD", "BAM", "BAN", "BBD", "BDT", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "BMD", "BND", "BOB", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "BRL", "BRN", "BRR", "BRZ", "BSD", "BTN", "BUK", "BWP", "BYB", "BYR", "BZD", "$CA", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNX", "CNY", "COP", "COU", "CRC", "CSD", "CSK", "CUC", "CUP", "CVE", "CYP", "CZK", "DDM", "DEM", "DJF", "DKK", "DOP", "DZD", "ECS", "ECV", "EEK", "EGP", "ERN", "ESA", "ESB", "ESP", "ETB", "€", "FIM", "FJD", "FKP", "FRF", "£ RU", "GEK", "GEL", "GHC", "GHS", "GIP", "GMD", "GNF", "GNS", "GQE", "GRD", "GTQ", "GWE", "GWP", "GYD", "$ HK", "HNL", "HRD", "HRK", "HTG", "HUF", "IDR", "IEP", "ILP", "ILR", "ILS", "₹", "IQD", "IRR", "ISJ", "ISK", "ITL", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRH", "KRO", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LTT", "LUC", "LUF", "LUL", "LVL", "LVR", "LYD", "MAD", "MAF", "MCF", "MDC", "MDL", "MGA", "MGF", "MKD", "MKN", "MLF", "MMK", "MNT", "MOP", "MRO", "MTL", "MTP", "MUR", "MVP ", "MVR", "MWK", "MX$", "MXP", "MXV", "MYR", "MZE", "MZM", "MZN", "NAD", "NGN", "NIC", "NIO", "NLG", "NOK", "NPR", "$ ZN", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "PHP", "PKR", "PLN", "PLZ", "PTE", "PYG", "QAR", "RHD", "ROL", "RON", "RSD", "RUB", "RUR", "RWF", "SAR", "SBD", "SCR", "SDD", "SDG", "SDP", "SEK", "SGD", "SHP", "SIT", "SKK", "SLL", "SOS", "SRD", "SRG", "SSP", "STD", "SUR", "SVC", "SYP", "SZL", "THB", "TJR", "TJS", "TMM", "TMT", "TND", "TOP", "TPE", "TRL", "TRY", "TTD", "TWD", "TZS", "UAH", "UAK", "UGS", "UGX", "$ SU", "USN", "USS", "UYI", "UYP", "UYU", "UZS", "VEB", "VEF", "VND", "VNN", "VUV", "WST", "FCFA", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "XCD", "XDR", "XEU", "XFO", "XFU", "CFA", "XPD", "CFPF", "XPT", "XRE", "XSU", "XTS", "XUA ", "XXX", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "ZAR", "ZMK", "ZMW", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
  50. percentSuffix: " ",
  51. currencyPositiveSuffix: " ",
  52. currencyNegativeSuffix: " ",
  53. monthsAbbreviated: []string{"", "Gen.", "Cʼhwe.", "Meur.", "Ebr.", "Mae", "Mezh.", "Goue.", "Eost", "Gwen.", "Here", "Du", "Kzu."},
  54. monthsNarrow: []string{"", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"},
  55. monthsWide: []string{"", "Genver", "Cʼhwevrer", "Meurzh", "Ebrel", "Mae", "Mezheven", "Gouere", "Eost", "Gwengolo", "Here", "Du", "Kerzu"},
  56. daysAbbreviated: []string{"Sul", "Lun", "Meu.", "Mer.", "Yaou", "Gwe.", "Sad."},
  57. daysNarrow: []string{"Su", "L", "Mz", "Mc", "Y", "G", "Sa"},
  58. daysWide: []string{"Sul", "Lun", "Meurzh", "Mercʼher", "Yaou", "Gwener", "Sadorn"},
  59. periodsAbbreviated: []string{"A.M.", "G.M."},
  60. periodsNarrow: []string{"am", "gm"},
  61. periodsWide: []string{"A.M.", "G.M."},
  62. erasAbbreviated: []string{"a-raok J.K.", "goude J.K."},
  63. erasNarrow: []string{"a-raok J.K.", "goude J.K."},
  64. erasWide: []string{"a-raok Jezuz-Krist", "goude Jezuz-Krist"},
  65. timezones: map[string]string{"CHAST": "eur cʼhoañv Chatham", "EDT": "eur hañv ar Reter", "HNT": "eur cʼhoañv Newfoundland", "AWST": "eur cʼhoañv Aostralia ar Cʼhornôg", "AKDT": "eur hañv Alaska", "NZST": "eur cʼhoañv Zeland-Nevez", "HKST": "eur hañv Hong Kong", "OEZ": "eur cʼhoañv Europa ar Reter", "TMST": "eur hañv Turkmenistan", "PST": "PST", "AWDT": "eur hañv Aostralia ar Cʼhornôg", "MESZ": "eur hañv Kreizeuropa", "WESZ": "eur hañv Europa ar Cʼhornôg", "CAT": "eur Kreizafrika", "ACDT": "eur hañv Kreizaostralia", "AEST": "eur cʼhoañv Aostralia ar Reter", "AEDT": "eur hañv Aostralia ar Reter", "WEZ": "eur cʼhoañv Europa ar Cʼhornôg", "HKT": "eur cʼhoañv Hong Kong", "WART": "eur cʼhoañv Arcʼhantina ar Cʼhornôg", "CST": "CST", "MST": "eur cʼhoañv ar Menezioù", "∅∅∅": "eur hañv Brasília", "GMT": "Amzer keitat Greenwich (AKG)", "JST": "eur cʼhoañv Japan", "WITA": "WITA", "ART": "eur cʼhoañv Arcʼhantina", "ARST": "eur hañv Arcʼhantina", "COST": "eur hañv Kolombia", "HADT": "HADT", "PDT": "PDT", "UYT": "eur cʼhoañv Uruguay", "ACWST": "eur cʼhoañv Kreizaostralia ar Cʼhornôg", "IST": "eur cʼhoañv India", "EAT": "eur Afrika ar Reter", "ACWDT": "eur hañv Kreizaostralia ar Cʼhornôg", "VET": "eur Venezuela", "AST": "AST", "SAST": "eur cʼhoañv Suafrika", "WIT": "eur Indonezia ar Reter", "ACST": "eur cʼhoañv Kreizaostralia", "CLST": "eur hañv Chile", "HAST": "HAST", "WAT": "eur cʼhoañv Afrika ar Cʼhornôg", "WAST": "eur hañv Afrika ar Cʼhornôg", "WARST": "eur hañv Arcʼhantina ar Cʼhornôg", "GFT": "eur Gwiana cʼhall", "GYT": "eur Guyana", "CDT": "CDT", "ChST": "ChST", "CLT": "eur cʼhoañv Chile", "EST": "eur cʼhoañv ar Reter", "OESZ": "eur hañv Europa ar Reter", "LHST": "LHST", "LHDT": "LHDT", "JDT": "eur hañv Japan", "WIB": "eur Indonezia ar Cʼhornôg", "TMT": "eur cʼhoañv Turkmenistan", "MYT": "eur Malaysia", "MEZ": "eur cʼhoañv Kreizeuropa", "NZDT": "eur hañv Zeland-Nevez", "ADT": "ADT", "HAT": "eur hañv Newfoundland", "UYST": "eur hañv Uruguay", "MDT": "eur hañv ar Menezioù", "COT": "eur cʼhoañv Kolombia", "SGT": "eur cʼhoañv Singapour", "BT": "eur Bhoutan", "AKST": "eur cʼhoañv Alaska", "BOT": "eur Bolivia", "CHADT": "eur hañv Chatham", "ECT": "eur Ecuador", "SRT": "eur Surinam"},
  66. }
  67. }
  68. // Locale returns the current translators string locale
  69. func (br *br) Locale() string {
  70. return br.locale
  71. }
  72. // PluralsCardinal returns the list of cardinal plural rules associated with 'br'
  73. func (br *br) PluralsCardinal() []locales.PluralRule {
  74. return br.pluralsCardinal
  75. }
  76. // PluralsOrdinal returns the list of ordinal plural rules associated with 'br'
  77. func (br *br) PluralsOrdinal() []locales.PluralRule {
  78. return br.pluralsOrdinal
  79. }
  80. // PluralsRange returns the list of range plural rules associated with 'br'
  81. func (br *br) PluralsRange() []locales.PluralRule {
  82. return br.pluralsRange
  83. }
  84. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'br'
  85. func (br *br) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  86. n := math.Abs(num)
  87. nMod10 := math.Mod(n, 10)
  88. nMod100 := math.Mod(n, 100)
  89. nMod1000000 := math.Mod(n, 1000000)
  90. if nMod10 == 1 && (nMod100 != 11 && nMod100 != 71 && nMod100 != 91) {
  91. return locales.PluralRuleOne
  92. } else if nMod10 == 2 && (nMod100 != 12 && nMod100 != 72 && nMod100 != 92) {
  93. return locales.PluralRuleTwo
  94. } else if nMod10 >= 3 && nMod10 <= 4 && (nMod10 == 9) && (nMod100 < 10 && nMod100 > 19) || (nMod100 < 70 && nMod100 > 79) || (nMod100 < 90 && nMod100 > 99) {
  95. return locales.PluralRuleFew
  96. } else if n != 0 && nMod1000000 == 0 {
  97. return locales.PluralRuleMany
  98. }
  99. return locales.PluralRuleOther
  100. }
  101. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'br'
  102. func (br *br) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  103. return locales.PluralRuleUnknown
  104. }
  105. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'br'
  106. func (br *br) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  107. return locales.PluralRuleUnknown
  108. }
  109. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  110. func (br *br) MonthAbbreviated(month time.Month) string {
  111. return br.monthsAbbreviated[month]
  112. }
  113. // MonthsAbbreviated returns the locales abbreviated months
  114. func (br *br) MonthsAbbreviated() []string {
  115. return br.monthsAbbreviated[1:]
  116. }
  117. // MonthNarrow returns the locales narrow month given the 'month' provided
  118. func (br *br) MonthNarrow(month time.Month) string {
  119. return br.monthsNarrow[month]
  120. }
  121. // MonthsNarrow returns the locales narrow months
  122. func (br *br) MonthsNarrow() []string {
  123. return br.monthsNarrow[1:]
  124. }
  125. // MonthWide returns the locales wide month given the 'month' provided
  126. func (br *br) MonthWide(month time.Month) string {
  127. return br.monthsWide[month]
  128. }
  129. // MonthsWide returns the locales wide months
  130. func (br *br) MonthsWide() []string {
  131. return br.monthsWide[1:]
  132. }
  133. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  134. func (br *br) WeekdayAbbreviated(weekday time.Weekday) string {
  135. return br.daysAbbreviated[weekday]
  136. }
  137. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  138. func (br *br) WeekdaysAbbreviated() []string {
  139. return br.daysAbbreviated
  140. }
  141. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  142. func (br *br) WeekdayNarrow(weekday time.Weekday) string {
  143. return br.daysNarrow[weekday]
  144. }
  145. // WeekdaysNarrow returns the locales narrow weekdays
  146. func (br *br) WeekdaysNarrow() []string {
  147. return br.daysNarrow
  148. }
  149. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  150. func (br *br) WeekdayShort(weekday time.Weekday) string {
  151. return br.daysShort[weekday]
  152. }
  153. // WeekdaysShort returns the locales short weekdays
  154. func (br *br) WeekdaysShort() []string {
  155. return br.daysShort
  156. }
  157. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  158. func (br *br) WeekdayWide(weekday time.Weekday) string {
  159. return br.daysWide[weekday]
  160. }
  161. // WeekdaysWide returns the locales wide weekdays
  162. func (br *br) WeekdaysWide() []string {
  163. return br.daysWide
  164. }
  165. // FmtNumber returns 'num' with digits/precision of 'v' for 'br' and handles both Whole and Real numbers based on 'v'
  166. func (br *br) FmtNumber(num float64, v uint64) string {
  167. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  168. l := len(s) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  169. count := 0
  170. inWhole := v == 0
  171. b := make([]byte, 0, l)
  172. for i := len(s) - 1; i >= 0; i-- {
  173. if s[i] == '.' {
  174. b = append(b, br.decimal[0])
  175. inWhole = true
  176. continue
  177. }
  178. if inWhole {
  179. if count == 3 {
  180. b = append(b, br.group[0])
  181. count = 1
  182. } else {
  183. count++
  184. }
  185. }
  186. b = append(b, s[i])
  187. }
  188. if num < 0 {
  189. b = append(b, br.minus[0])
  190. }
  191. // reverse
  192. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  193. b[i], b[j] = b[j], b[i]
  194. }
  195. return string(b)
  196. }
  197. // FmtPercent returns 'num' with digits/precision of 'v' for 'br' and handles both Whole and Real numbers based on 'v'
  198. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  199. func (br *br) FmtPercent(num float64, v uint64) string {
  200. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  201. l := len(s) + 2
  202. b := make([]byte, 0, l)
  203. for i := len(s) - 1; i >= 0; i-- {
  204. if s[i] == '.' {
  205. b = append(b, br.decimal[0])
  206. continue
  207. }
  208. b = append(b, s[i])
  209. }
  210. if num < 0 {
  211. b = append(b, br.minus[0])
  212. }
  213. // reverse
  214. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  215. b[i], b[j] = b[j], b[i]
  216. }
  217. b = append(b, br.percentSuffix...)
  218. b = append(b, br.percent...)
  219. return string(b)
  220. }
  221. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'br'
  222. func (br *br) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  223. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  224. symbol := br.currencies[currency]
  225. l := len(s) + len(symbol) + 2 + 0*len(s[:len(s)-int(v)-1])/3
  226. count := 0
  227. inWhole := v == 0
  228. b := make([]byte, 0, l)
  229. for i := len(s) - 1; i >= 0; i-- {
  230. if s[i] == '.' {
  231. b = append(b, br.decimal[0])
  232. inWhole = true
  233. continue
  234. }
  235. if inWhole {
  236. if count == 3 {
  237. b = append(b, br.group[0])
  238. count = 1
  239. } else {
  240. count++
  241. }
  242. }
  243. b = append(b, s[i])
  244. }
  245. if num < 0 {
  246. b = append(b, br.minus[0])
  247. }
  248. // reverse
  249. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  250. b[i], b[j] = b[j], b[i]
  251. }
  252. if int(v) < 2 {
  253. if v == 0 {
  254. b = append(b, br.decimal...)
  255. }
  256. for i := 0; i < 2-int(v); i++ {
  257. b = append(b, '0')
  258. }
  259. }
  260. b = append(b, br.currencyPositiveSuffix...)
  261. b = append(b, symbol...)
  262. return string(b)
  263. }
  264. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'br'
  265. // in accounting notation.
  266. func (br *br) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  267. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  268. symbol := br.currencies[currency]
  269. l := len(s) + len(symbol) + 2 + 0*len(s[:len(s)-int(v)-1])/3
  270. count := 0
  271. inWhole := v == 0
  272. b := make([]byte, 0, l)
  273. for i := len(s) - 1; i >= 0; i-- {
  274. if s[i] == '.' {
  275. b = append(b, br.decimal[0])
  276. inWhole = true
  277. continue
  278. }
  279. if inWhole {
  280. if count == 3 {
  281. b = append(b, br.group[0])
  282. count = 1
  283. } else {
  284. count++
  285. }
  286. }
  287. b = append(b, s[i])
  288. }
  289. if num < 0 {
  290. b = append(b, br.minus[0])
  291. }
  292. // reverse
  293. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  294. b[i], b[j] = b[j], b[i]
  295. }
  296. if int(v) < 2 {
  297. if v == 0 {
  298. b = append(b, br.decimal...)
  299. }
  300. for i := 0; i < 2-int(v); i++ {
  301. b = append(b, '0')
  302. }
  303. }
  304. if num < 0 {
  305. b = append(b, br.currencyNegativeSuffix...)
  306. b = append(b, symbol...)
  307. } else {
  308. b = append(b, br.currencyPositiveSuffix...)
  309. b = append(b, symbol...)
  310. }
  311. return string(b)
  312. }
  313. // FmtDateShort returns the short date representation of 't' for 'br'
  314. func (br *br) FmtDateShort(t time.Time) string {
  315. b := make([]byte, 0, 32)
  316. return string(b)
  317. }
  318. // FmtDateMedium returns the medium date representation of 't' for 'br'
  319. func (br *br) FmtDateMedium(t time.Time) string {
  320. b := make([]byte, 0, 32)
  321. return string(b)
  322. }
  323. // FmtDateLong returns the long date representation of 't' for 'br'
  324. func (br *br) FmtDateLong(t time.Time) string {
  325. b := make([]byte, 0, 32)
  326. return string(b)
  327. }
  328. // FmtDateFull returns the full date representation of 't' for 'br'
  329. func (br *br) FmtDateFull(t time.Time) string {
  330. b := make([]byte, 0, 32)
  331. return string(b)
  332. }
  333. // FmtTimeShort returns the short time representation of 't' for 'br'
  334. func (br *br) FmtTimeShort(t time.Time) string {
  335. b := make([]byte, 0, 32)
  336. return string(b)
  337. }
  338. // FmtTimeMedium returns the medium time representation of 't' for 'br'
  339. func (br *br) FmtTimeMedium(t time.Time) string {
  340. b := make([]byte, 0, 32)
  341. return string(b)
  342. }
  343. // FmtTimeLong returns the long time representation of 't' for 'br'
  344. func (br *br) FmtTimeLong(t time.Time) string {
  345. b := make([]byte, 0, 32)
  346. return string(b)
  347. }
  348. // FmtTimeFull returns the full time representation of 't' for 'br'
  349. func (br *br) FmtTimeFull(t time.Time) string {
  350. b := make([]byte, 0, 32)
  351. return string(b)
  352. }