lag.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. package lag
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type lag 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. perMille string
  19. timeSeparator string
  20. inifinity string
  21. currencies []string // idx = enum of currency code
  22. currencyPositivePrefix string
  23. currencyPositiveSuffix string
  24. currencyNegativePrefix string
  25. currencyNegativeSuffix string
  26. monthsAbbreviated []string
  27. monthsNarrow []string
  28. monthsWide []string
  29. daysAbbreviated []string
  30. daysNarrow []string
  31. daysShort []string
  32. daysWide []string
  33. periodsAbbreviated []string
  34. periodsNarrow []string
  35. periodsShort []string
  36. periodsWide []string
  37. erasAbbreviated []string
  38. erasNarrow []string
  39. erasWide []string
  40. timezones map[string]string
  41. }
  42. // New returns a new instance of translator for the 'lag' locale
  43. func New() locales.Translator {
  44. return &lag{
  45. locale: "lag",
  46. pluralsCardinal: []locales.PluralRule{1, 2, 6},
  47. pluralsOrdinal: nil,
  48. pluralsRange: nil,
  49. timeSeparator: ":",
  50. currencies: []string{"ADP ", "AED ", "AFA ", "AFN ", "ALK ", "ALL ", "AMD ", "ANG ", "AOA ", "AOK ", "AON ", "AOR ", "ARA ", "ARL ", "ARM ", "ARP ", "ARS ", "ATS ", "AUD ", "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 ", "CAD ", "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 ", "EUR ", "FIM ", "FJD ", "FKP ", "FRF ", "GBP ", "GEK ", "GEL ", "GHC ", "GHS ", "GIP ", "GMD ", "GNF ", "GNS ", "GQE ", "GRD ", "GTQ ", "GWE ", "GWP ", "GYD ", "HKD ", "HNL ", "HRD ", "HRK ", "HTG ", "HUF ", "IDR ", "IEP ", "ILP ", "ILR ", "ILS ", "INR ", "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 ", "MXN ", "MXP ", "MXV ", "MYR ", "MZE ", "MZM ", "MZN ", "NAD ", "NGN ", "NIC ", "NIO ", "NLG ", "NOK ", "NPR ", "NZD ", "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 ", "TSh", "UAH ", "UAK ", "UGS ", "UGX ", "USD ", "USN ", "USS ", "UYI ", "UYP ", "UYU ", "UZS ", "VEB ", "VEF ", "VND ", "VNN ", "VUV ", "WST ", "XAF ", "XAG ", "XAU ", "XBA ", "XBB ", "XBC ", "XBD ", "XCD ", "XDR ", "XEU ", "XFO ", "XFU ", "XOF ", "XPD ", "XPF ", "XPT ", "XRE ", "XSU ", "XTS ", "XUA ", "XXX ", "YDD ", "YER ", "YUD ", "YUM ", "YUN ", "YUR ", "ZAL ", "ZAR ", "ZMK ", "ZMW ", "ZRN ", "ZRZ ", "ZWD ", "ZWL ", "ZWR "},
  51. currencyPositivePrefix: " ",
  52. currencyPositiveSuffix: "K",
  53. currencyNegativePrefix: " ",
  54. currencyNegativeSuffix: "K",
  55. monthsAbbreviated: []string{"", "Fúngatɨ", "Naanɨ", "Keenda", "Ikúmi", "Inyambala", "Idwaata", "Mʉʉnchɨ", "Vɨɨrɨ", "Saatʉ", "Inyi", "Saano", "Sasatʉ"},
  56. monthsNarrow: []string{"", "F", "N", "K", "I", "I", "I", "M", "V", "S", "I", "S", "S"},
  57. monthsWide: []string{"", "Kʉfúngatɨ", "Kʉnaanɨ", "Kʉkeenda", "Kwiikumi", "Kwiinyambála", "Kwiidwaata", "Kʉmʉʉnchɨ", "Kʉvɨɨrɨ", "Kʉsaatʉ", "Kwiinyi", "Kʉsaano", "Kʉsasatʉ"},
  58. daysAbbreviated: []string{"Píili", "Táatu", "Íne", "Táano", "Alh", "Ijm", "Móosi"},
  59. daysNarrow: []string{"P", "T", "E", "O", "A", "I", "M"},
  60. daysWide: []string{"Jumapíiri", "Jumatátu", "Jumaíne", "Jumatáano", "Alamíisi", "Ijumáa", "Jumamóosi"},
  61. periodsAbbreviated: []string{"TOO", "MUU"},
  62. periodsWide: []string{"TOO", "MUU"},
  63. erasAbbreviated: []string{"KSA", "KA"},
  64. erasNarrow: []string{"", ""},
  65. erasWide: []string{"Kɨrɨsitʉ sɨ anavyaal", "Kɨrɨsitʉ akavyaalwe"},
  66. timezones: map[string]string{"TMT": "TMT", "EAT": "EAT", "MST": "MST", "SAST": "SAST", "HAT": "HAT", "ChST": "ChST", "MEZ": "MEZ", "PDT": "PDT", "BOT": "BOT", "SGT": "SGT", "AKST": "AKST", "AWST": "AWST", "MYT": "MYT", "AEDT": "AEDT", "CAT": "CAT", "HKT": "HKT", "WIB": "WIB", "IST": "IST", "NZST": "NZST", "WAT": "WAT", "WITA": "WITA", "CLT": "CLT", "VET": "VET", "NZDT": "NZDT", "CDT": "CDT", "AWDT": "AWDT", "LHST": "LHST", "ECT": "ECT", "WART": "WART", "JST": "JST", "AKDT": "AKDT", "WARST": "WARST", "MDT": "MDT", "HAST": "HAST", "HADT": "HADT", "BT": "BT", "HNT": "HNT", "TMST": "TMST", "SRT": "SRT", "∅∅∅": "∅∅∅", "ADT": "ADT", "PST": "PST", "MESZ": "MESZ", "CHAST": "CHAST", "EST": "EST", "ARST": "ARST", "WEZ": "WEZ", "ACWDT": "ACWDT", "GMT": "GMT", "CST": "CST", "ART": "ART", "CHADT": "CHADT", "CLST": "CLST", "ACWST": "ACWST", "ACST": "ACST", "ACDT": "ACDT", "WAST": "WAST", "OESZ": "OESZ", "LHDT": "LHDT", "GYT": "GYT", "JDT": "JDT", "AEST": "AEST", "COT": "COT", "COST": "COST", "OEZ": "OEZ", "AST": "AST", "GFT": "GFT", "WIT": "WIT", "UYT": "UYT", "UYST": "UYST", "WESZ": "WESZ", "EDT": "EDT", "HKST": "HKST"},
  67. }
  68. }
  69. // Locale returns the current translators string locale
  70. func (lag *lag) Locale() string {
  71. return lag.locale
  72. }
  73. // PluralsCardinal returns the list of cardinal plural rules associated with 'lag'
  74. func (lag *lag) PluralsCardinal() []locales.PluralRule {
  75. return lag.pluralsCardinal
  76. }
  77. // PluralsOrdinal returns the list of ordinal plural rules associated with 'lag'
  78. func (lag *lag) PluralsOrdinal() []locales.PluralRule {
  79. return lag.pluralsOrdinal
  80. }
  81. // PluralsRange returns the list of range plural rules associated with 'lag'
  82. func (lag *lag) PluralsRange() []locales.PluralRule {
  83. return lag.pluralsRange
  84. }
  85. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'lag'
  86. func (lag *lag) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  87. n := math.Abs(num)
  88. i := int64(n)
  89. if n == 0 {
  90. return locales.PluralRuleZero
  91. } else if (i == 0 || i == 1) && n != 0 {
  92. return locales.PluralRuleOne
  93. }
  94. return locales.PluralRuleOther
  95. }
  96. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'lag'
  97. func (lag *lag) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  98. return locales.PluralRuleUnknown
  99. }
  100. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'lag'
  101. func (lag *lag) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  102. return locales.PluralRuleUnknown
  103. }
  104. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  105. func (lag *lag) MonthAbbreviated(month time.Month) string {
  106. return lag.monthsAbbreviated[month]
  107. }
  108. // MonthsAbbreviated returns the locales abbreviated months
  109. func (lag *lag) MonthsAbbreviated() []string {
  110. return lag.monthsAbbreviated[1:]
  111. }
  112. // MonthNarrow returns the locales narrow month given the 'month' provided
  113. func (lag *lag) MonthNarrow(month time.Month) string {
  114. return lag.monthsNarrow[month]
  115. }
  116. // MonthsNarrow returns the locales narrow months
  117. func (lag *lag) MonthsNarrow() []string {
  118. return lag.monthsNarrow[1:]
  119. }
  120. // MonthWide returns the locales wide month given the 'month' provided
  121. func (lag *lag) MonthWide(month time.Month) string {
  122. return lag.monthsWide[month]
  123. }
  124. // MonthsWide returns the locales wide months
  125. func (lag *lag) MonthsWide() []string {
  126. return lag.monthsWide[1:]
  127. }
  128. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  129. func (lag *lag) WeekdayAbbreviated(weekday time.Weekday) string {
  130. return lag.daysAbbreviated[weekday]
  131. }
  132. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  133. func (lag *lag) WeekdaysAbbreviated() []string {
  134. return lag.daysAbbreviated
  135. }
  136. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  137. func (lag *lag) WeekdayNarrow(weekday time.Weekday) string {
  138. return lag.daysNarrow[weekday]
  139. }
  140. // WeekdaysNarrow returns the locales narrow weekdays
  141. func (lag *lag) WeekdaysNarrow() []string {
  142. return lag.daysNarrow
  143. }
  144. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  145. func (lag *lag) WeekdayShort(weekday time.Weekday) string {
  146. return lag.daysShort[weekday]
  147. }
  148. // WeekdaysShort returns the locales short weekdays
  149. func (lag *lag) WeekdaysShort() []string {
  150. return lag.daysShort
  151. }
  152. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  153. func (lag *lag) WeekdayWide(weekday time.Weekday) string {
  154. return lag.daysWide[weekday]
  155. }
  156. // WeekdaysWide returns the locales wide weekdays
  157. func (lag *lag) WeekdaysWide() []string {
  158. return lag.daysWide
  159. }
  160. // FmtNumber returns 'num' with digits/precision of 'v' for 'lag' and handles both Whole and Real numbers based on 'v'
  161. func (lag *lag) FmtNumber(num float64, v uint64) string {
  162. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  163. }
  164. // FmtPercent returns 'num' with digits/precision of 'v' for 'lag' and handles both Whole and Real numbers based on 'v'
  165. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  166. func (lag *lag) FmtPercent(num float64, v uint64) string {
  167. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  168. }
  169. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'lag'
  170. func (lag *lag) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  171. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  172. symbol := lag.currencies[currency]
  173. l := len(s) + len(symbol) + 3
  174. b := make([]byte, 0, l)
  175. for i := len(s) - 1; i >= 0; i-- {
  176. if s[i] == '.' {
  177. b = append(b, lag.decimal[0])
  178. continue
  179. }
  180. b = append(b, s[i])
  181. }
  182. for j := len(symbol) - 1; j >= 0; j-- {
  183. b = append(b, symbol[j])
  184. }
  185. for j := len(lag.currencyPositivePrefix) - 1; j >= 0; j-- {
  186. b = append(b, lag.currencyPositivePrefix[j])
  187. }
  188. if num < 0 {
  189. b = append(b, lag.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. b = append(b, lag.currencyPositiveSuffix...)
  196. return string(b)
  197. }
  198. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'lag'
  199. // in accounting notation.
  200. func (lag *lag) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  201. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  202. symbol := lag.currencies[currency]
  203. l := len(s) + len(symbol) + 3
  204. b := make([]byte, 0, l)
  205. for i := len(s) - 1; i >= 0; i-- {
  206. if s[i] == '.' {
  207. b = append(b, lag.decimal[0])
  208. continue
  209. }
  210. b = append(b, s[i])
  211. }
  212. if num < 0 {
  213. for j := len(symbol) - 1; j >= 0; j-- {
  214. b = append(b, symbol[j])
  215. }
  216. for j := len(lag.currencyNegativePrefix) - 1; j >= 0; j-- {
  217. b = append(b, lag.currencyNegativePrefix[j])
  218. }
  219. b = append(b, lag.minus[0])
  220. } else {
  221. for j := len(symbol) - 1; j >= 0; j-- {
  222. b = append(b, symbol[j])
  223. }
  224. for j := len(lag.currencyPositivePrefix) - 1; j >= 0; j-- {
  225. b = append(b, lag.currencyPositivePrefix[j])
  226. }
  227. }
  228. // reverse
  229. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  230. b[i], b[j] = b[j], b[i]
  231. }
  232. if num < 0 {
  233. b = append(b, lag.currencyNegativeSuffix...)
  234. } else {
  235. b = append(b, lag.currencyPositiveSuffix...)
  236. }
  237. return string(b)
  238. }
  239. // FmtDateShort returns the short date representation of 't' for 'lag'
  240. func (lag *lag) FmtDateShort(t time.Time) string {
  241. b := make([]byte, 0, 32)
  242. if t.Day() < 10 {
  243. b = append(b, '0')
  244. }
  245. b = strconv.AppendInt(b, int64(t.Day()), 10)
  246. b = append(b, []byte{0x2f}...)
  247. if t.Month() < 10 {
  248. b = append(b, '0')
  249. }
  250. b = strconv.AppendInt(b, int64(t.Month()), 10)
  251. b = append(b, []byte{0x2f}...)
  252. b = strconv.AppendInt(b, int64(t.Year()), 10)
  253. return string(b)
  254. }
  255. // FmtDateMedium returns the medium date representation of 't' for 'lag'
  256. func (lag *lag) FmtDateMedium(t time.Time) string {
  257. b := make([]byte, 0, 32)
  258. b = strconv.AppendInt(b, int64(t.Day()), 10)
  259. b = append(b, []byte{0x20}...)
  260. b = append(b, lag.monthsAbbreviated[t.Month()]...)
  261. b = append(b, []byte{0x20}...)
  262. b = strconv.AppendInt(b, int64(t.Year()), 10)
  263. return string(b)
  264. }
  265. // FmtDateLong returns the long date representation of 't' for 'lag'
  266. func (lag *lag) FmtDateLong(t time.Time) string {
  267. b := make([]byte, 0, 32)
  268. b = strconv.AppendInt(b, int64(t.Day()), 10)
  269. b = append(b, []byte{0x20}...)
  270. b = append(b, lag.monthsWide[t.Month()]...)
  271. b = append(b, []byte{0x20}...)
  272. b = strconv.AppendInt(b, int64(t.Year()), 10)
  273. return string(b)
  274. }
  275. // FmtDateFull returns the full date representation of 't' for 'lag'
  276. func (lag *lag) FmtDateFull(t time.Time) string {
  277. b := make([]byte, 0, 32)
  278. b = append(b, lag.daysWide[t.Weekday()]...)
  279. b = append(b, []byte{0x2c, 0x20}...)
  280. b = strconv.AppendInt(b, int64(t.Day()), 10)
  281. b = append(b, []byte{0x20}...)
  282. b = append(b, lag.monthsWide[t.Month()]...)
  283. b = append(b, []byte{0x20}...)
  284. b = strconv.AppendInt(b, int64(t.Year()), 10)
  285. return string(b)
  286. }
  287. // FmtTimeShort returns the short time representation of 't' for 'lag'
  288. func (lag *lag) FmtTimeShort(t time.Time) string {
  289. b := make([]byte, 0, 32)
  290. if t.Hour() < 10 {
  291. b = append(b, '0')
  292. }
  293. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  294. b = append(b, lag.timeSeparator...)
  295. if t.Minute() < 10 {
  296. b = append(b, '0')
  297. }
  298. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  299. return string(b)
  300. }
  301. // FmtTimeMedium returns the medium time representation of 't' for 'lag'
  302. func (lag *lag) FmtTimeMedium(t time.Time) string {
  303. b := make([]byte, 0, 32)
  304. if t.Hour() < 10 {
  305. b = append(b, '0')
  306. }
  307. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  308. b = append(b, lag.timeSeparator...)
  309. if t.Minute() < 10 {
  310. b = append(b, '0')
  311. }
  312. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  313. b = append(b, lag.timeSeparator...)
  314. if t.Second() < 10 {
  315. b = append(b, '0')
  316. }
  317. b = strconv.AppendInt(b, int64(t.Second()), 10)
  318. return string(b)
  319. }
  320. // FmtTimeLong returns the long time representation of 't' for 'lag'
  321. func (lag *lag) FmtTimeLong(t time.Time) string {
  322. b := make([]byte, 0, 32)
  323. if t.Hour() < 10 {
  324. b = append(b, '0')
  325. }
  326. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  327. b = append(b, lag.timeSeparator...)
  328. if t.Minute() < 10 {
  329. b = append(b, '0')
  330. }
  331. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  332. b = append(b, lag.timeSeparator...)
  333. if t.Second() < 10 {
  334. b = append(b, '0')
  335. }
  336. b = strconv.AppendInt(b, int64(t.Second()), 10)
  337. b = append(b, []byte{0x20}...)
  338. tz, _ := t.Zone()
  339. b = append(b, tz...)
  340. return string(b)
  341. }
  342. // FmtTimeFull returns the full time representation of 't' for 'lag'
  343. func (lag *lag) FmtTimeFull(t time.Time) string {
  344. b := make([]byte, 0, 32)
  345. if t.Hour() < 10 {
  346. b = append(b, '0')
  347. }
  348. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  349. b = append(b, lag.timeSeparator...)
  350. if t.Minute() < 10 {
  351. b = append(b, '0')
  352. }
  353. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  354. b = append(b, lag.timeSeparator...)
  355. if t.Second() < 10 {
  356. b = append(b, '0')
  357. }
  358. b = strconv.AppendInt(b, int64(t.Second()), 10)
  359. b = append(b, []byte{0x20}...)
  360. tz, _ := t.Zone()
  361. if btz, ok := lag.timezones[tz]; ok {
  362. b = append(b, btz...)
  363. } else {
  364. b = append(b, tz...)
  365. }
  366. return string(b)
  367. }