lg.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. package lg
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type lg 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. monthsAbbreviated []string
  23. monthsNarrow []string
  24. monthsWide []string
  25. daysAbbreviated []string
  26. daysNarrow []string
  27. daysShort []string
  28. daysWide []string
  29. periodsAbbreviated []string
  30. periodsNarrow []string
  31. periodsShort []string
  32. periodsWide []string
  33. erasAbbreviated []string
  34. erasNarrow []string
  35. erasWide []string
  36. timezones map[string]string
  37. }
  38. // New returns a new instance of translator for the 'lg' locale
  39. func New() locales.Translator {
  40. return &lg{
  41. locale: "lg",
  42. pluralsCardinal: []locales.PluralRule{2, 6},
  43. pluralsOrdinal: nil,
  44. pluralsRange: nil,
  45. timeSeparator: ":",
  46. 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 ", "TZS ", "UAH ", "UAK ", "UGS ", "USh", "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 "},
  47. monthsAbbreviated: []string{"", "Jan", "Feb", "Mar", "Apu", "Maa", "Juu", "Jul", "Agu", "Seb", "Oki", "Nov", "Des"},
  48. monthsNarrow: []string{"", "J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"},
  49. monthsWide: []string{"", "Janwaliyo", "Febwaliyo", "Marisi", "Apuli", "Maayi", "Juuni", "Julaayi", "Agusito", "Sebuttemba", "Okitobba", "Novemba", "Desemba"},
  50. daysAbbreviated: []string{"Sab", "Bal", "Lw2", "Lw3", "Lw4", "Lw5", "Lw6"},
  51. daysNarrow: []string{"S", "B", "L", "L", "L", "L", "L"},
  52. daysWide: []string{"Sabbiiti", "Balaza", "Lwakubiri", "Lwakusatu", "Lwakuna", "Lwakutaano", "Lwamukaaga"},
  53. erasAbbreviated: []string{"BC", "AD"},
  54. erasNarrow: []string{"", ""},
  55. erasWide: []string{"Kulisito nga tannaza", "Bukya Kulisito Azaal"},
  56. timezones: map[string]string{"CLST": "CLST", "PST": "PST", "CHADT": "CHADT", "MYT": "MYT", "SRT": "SRT", "LHDT": "LHDT", "ADT": "ADT", "GFT": "GFT", "MST": "MST", "JST": "JST", "PDT": "PDT", "MEZ": "MEZ", "WESZ": "WESZ", "ChST": "ChST", "COT": "COT", "HKT": "HKT", "MDT": "MDT", "ECT": "ECT", "JDT": "JDT", "NZST": "NZST", "∅∅∅": "∅∅∅", "AEST": "AEST", "WARST": "WARST", "GMT": "GMT", "HNT": "HNT", "UYST": "UYST", "CHAST": "CHAST", "AST": "AST", "HADT": "HADT", "SAST": "SAST", "OEZ": "OEZ", "UYT": "UYT", "BOT": "BOT", "ACWST": "ACWST", "ACWDT": "ACWDT", "ARST": "ARST", "AWST": "AWST", "ART": "ART", "HAST": "HAST", "IST": "IST", "ACDT": "ACDT", "EDT": "EDT", "TMT": "TMT", "AKDT": "AKDT", "MESZ": "MESZ", "VET": "VET", "CAT": "CAT", "EST": "EST", "SGT": "SGT", "WAST": "WAST", "TMST": "TMST", "AWDT": "AWDT", "ACST": "ACST", "NZDT": "NZDT", "WIB": "WIB", "COST": "COST", "HKST": "HKST", "WITA": "WITA", "WIT": "WIT", "GYT": "GYT", "AKST": "AKST", "LHST": "LHST", "AEDT": "AEDT", "WART": "WART", "WAT": "WAT", "HAT": "HAT", "WEZ": "WEZ", "EAT": "EAT", "CST": "CST", "CDT": "CDT", "CLT": "CLT", "BT": "BT", "OESZ": "OESZ"},
  57. }
  58. }
  59. // Locale returns the current translators string locale
  60. func (lg *lg) Locale() string {
  61. return lg.locale
  62. }
  63. // PluralsCardinal returns the list of cardinal plural rules associated with 'lg'
  64. func (lg *lg) PluralsCardinal() []locales.PluralRule {
  65. return lg.pluralsCardinal
  66. }
  67. // PluralsOrdinal returns the list of ordinal plural rules associated with 'lg'
  68. func (lg *lg) PluralsOrdinal() []locales.PluralRule {
  69. return lg.pluralsOrdinal
  70. }
  71. // PluralsRange returns the list of range plural rules associated with 'lg'
  72. func (lg *lg) PluralsRange() []locales.PluralRule {
  73. return lg.pluralsRange
  74. }
  75. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'lg'
  76. func (lg *lg) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  77. n := math.Abs(num)
  78. if n == 1 {
  79. return locales.PluralRuleOne
  80. }
  81. return locales.PluralRuleOther
  82. }
  83. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'lg'
  84. func (lg *lg) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  85. return locales.PluralRuleUnknown
  86. }
  87. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'lg'
  88. func (lg *lg) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  89. return locales.PluralRuleUnknown
  90. }
  91. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  92. func (lg *lg) MonthAbbreviated(month time.Month) string {
  93. return lg.monthsAbbreviated[month]
  94. }
  95. // MonthsAbbreviated returns the locales abbreviated months
  96. func (lg *lg) MonthsAbbreviated() []string {
  97. return lg.monthsAbbreviated[1:]
  98. }
  99. // MonthNarrow returns the locales narrow month given the 'month' provided
  100. func (lg *lg) MonthNarrow(month time.Month) string {
  101. return lg.monthsNarrow[month]
  102. }
  103. // MonthsNarrow returns the locales narrow months
  104. func (lg *lg) MonthsNarrow() []string {
  105. return lg.monthsNarrow[1:]
  106. }
  107. // MonthWide returns the locales wide month given the 'month' provided
  108. func (lg *lg) MonthWide(month time.Month) string {
  109. return lg.monthsWide[month]
  110. }
  111. // MonthsWide returns the locales wide months
  112. func (lg *lg) MonthsWide() []string {
  113. return lg.monthsWide[1:]
  114. }
  115. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  116. func (lg *lg) WeekdayAbbreviated(weekday time.Weekday) string {
  117. return lg.daysAbbreviated[weekday]
  118. }
  119. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  120. func (lg *lg) WeekdaysAbbreviated() []string {
  121. return lg.daysAbbreviated
  122. }
  123. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  124. func (lg *lg) WeekdayNarrow(weekday time.Weekday) string {
  125. return lg.daysNarrow[weekday]
  126. }
  127. // WeekdaysNarrow returns the locales narrow weekdays
  128. func (lg *lg) WeekdaysNarrow() []string {
  129. return lg.daysNarrow
  130. }
  131. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  132. func (lg *lg) WeekdayShort(weekday time.Weekday) string {
  133. return lg.daysShort[weekday]
  134. }
  135. // WeekdaysShort returns the locales short weekdays
  136. func (lg *lg) WeekdaysShort() []string {
  137. return lg.daysShort
  138. }
  139. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  140. func (lg *lg) WeekdayWide(weekday time.Weekday) string {
  141. return lg.daysWide[weekday]
  142. }
  143. // WeekdaysWide returns the locales wide weekdays
  144. func (lg *lg) WeekdaysWide() []string {
  145. return lg.daysWide
  146. }
  147. // FmtNumber returns 'num' with digits/precision of 'v' for 'lg' and handles both Whole and Real numbers based on 'v'
  148. func (lg *lg) FmtNumber(num float64, v uint64) string {
  149. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  150. }
  151. // FmtPercent returns 'num' with digits/precision of 'v' for 'lg' and handles both Whole and Real numbers based on 'v'
  152. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  153. func (lg *lg) FmtPercent(num float64, v uint64) string {
  154. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  155. }
  156. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'lg'
  157. func (lg *lg) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  158. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  159. symbol := lg.currencies[currency]
  160. l := len(s) + len(symbol) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  161. count := 0
  162. inWhole := v == 0
  163. b := make([]byte, 0, l)
  164. for i := len(s) - 1; i >= 0; i-- {
  165. if s[i] == '.' {
  166. b = append(b, lg.decimal[0])
  167. inWhole = true
  168. continue
  169. }
  170. if inWhole {
  171. if count == 3 {
  172. b = append(b, lg.group[0])
  173. count = 1
  174. } else {
  175. count++
  176. }
  177. }
  178. b = append(b, s[i])
  179. }
  180. if num < 0 {
  181. b = append(b, lg.minus[0])
  182. }
  183. // reverse
  184. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  185. b[i], b[j] = b[j], b[i]
  186. }
  187. if int(v) < 2 {
  188. if v == 0 {
  189. b = append(b, lg.decimal...)
  190. }
  191. for i := 0; i < 2-int(v); i++ {
  192. b = append(b, '0')
  193. }
  194. }
  195. b = append(b, symbol...)
  196. return string(b)
  197. }
  198. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'lg'
  199. // in accounting notation.
  200. func (lg *lg) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  201. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  202. symbol := lg.currencies[currency]
  203. l := len(s) + len(symbol) + 0 + 0*len(s[:len(s)-int(v)-1])/3
  204. count := 0
  205. inWhole := v == 0
  206. b := make([]byte, 0, l)
  207. for i := len(s) - 1; i >= 0; i-- {
  208. if s[i] == '.' {
  209. b = append(b, lg.decimal[0])
  210. inWhole = true
  211. continue
  212. }
  213. if inWhole {
  214. if count == 3 {
  215. b = append(b, lg.group[0])
  216. count = 1
  217. } else {
  218. count++
  219. }
  220. }
  221. b = append(b, s[i])
  222. }
  223. if num < 0 {
  224. b = append(b, lg.minus[0])
  225. }
  226. // reverse
  227. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  228. b[i], b[j] = b[j], b[i]
  229. }
  230. if int(v) < 2 {
  231. if v == 0 {
  232. b = append(b, lg.decimal...)
  233. }
  234. for i := 0; i < 2-int(v); i++ {
  235. b = append(b, '0')
  236. }
  237. }
  238. if num < 0 {
  239. b = append(b, symbol...)
  240. } else {
  241. b = append(b, symbol...)
  242. }
  243. return string(b)
  244. }
  245. // FmtDateShort returns the short date representation of 't' for 'lg'
  246. func (lg *lg) FmtDateShort(t time.Time) string {
  247. b := make([]byte, 0, 32)
  248. if t.Day() < 10 {
  249. b = append(b, '0')
  250. }
  251. b = strconv.AppendInt(b, int64(t.Day()), 10)
  252. b = append(b, []byte{0x2f}...)
  253. if t.Month() < 10 {
  254. b = append(b, '0')
  255. }
  256. b = strconv.AppendInt(b, int64(t.Month()), 10)
  257. b = append(b, []byte{0x2f}...)
  258. b = strconv.AppendInt(b, int64(t.Year()), 10)
  259. return string(b)
  260. }
  261. // FmtDateMedium returns the medium date representation of 't' for 'lg'
  262. func (lg *lg) FmtDateMedium(t time.Time) string {
  263. b := make([]byte, 0, 32)
  264. b = strconv.AppendInt(b, int64(t.Day()), 10)
  265. b = append(b, []byte{0x20}...)
  266. b = append(b, lg.monthsAbbreviated[t.Month()]...)
  267. b = append(b, []byte{0x20}...)
  268. b = strconv.AppendInt(b, int64(t.Year()), 10)
  269. return string(b)
  270. }
  271. // FmtDateLong returns the long date representation of 't' for 'lg'
  272. func (lg *lg) FmtDateLong(t time.Time) string {
  273. b := make([]byte, 0, 32)
  274. b = strconv.AppendInt(b, int64(t.Day()), 10)
  275. b = append(b, []byte{0x20}...)
  276. b = append(b, lg.monthsWide[t.Month()]...)
  277. b = append(b, []byte{0x20}...)
  278. b = strconv.AppendInt(b, int64(t.Year()), 10)
  279. return string(b)
  280. }
  281. // FmtDateFull returns the full date representation of 't' for 'lg'
  282. func (lg *lg) FmtDateFull(t time.Time) string {
  283. b := make([]byte, 0, 32)
  284. b = append(b, lg.daysWide[t.Weekday()]...)
  285. b = append(b, []byte{0x2c, 0x20}...)
  286. b = strconv.AppendInt(b, int64(t.Day()), 10)
  287. b = append(b, []byte{0x20}...)
  288. b = append(b, lg.monthsWide[t.Month()]...)
  289. b = append(b, []byte{0x20}...)
  290. b = strconv.AppendInt(b, int64(t.Year()), 10)
  291. return string(b)
  292. }
  293. // FmtTimeShort returns the short time representation of 't' for 'lg'
  294. func (lg *lg) FmtTimeShort(t time.Time) string {
  295. b := make([]byte, 0, 32)
  296. if t.Hour() < 10 {
  297. b = append(b, '0')
  298. }
  299. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  300. b = append(b, lg.timeSeparator...)
  301. if t.Minute() < 10 {
  302. b = append(b, '0')
  303. }
  304. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  305. return string(b)
  306. }
  307. // FmtTimeMedium returns the medium time representation of 't' for 'lg'
  308. func (lg *lg) FmtTimeMedium(t time.Time) string {
  309. b := make([]byte, 0, 32)
  310. if t.Hour() < 10 {
  311. b = append(b, '0')
  312. }
  313. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  314. b = append(b, lg.timeSeparator...)
  315. if t.Minute() < 10 {
  316. b = append(b, '0')
  317. }
  318. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  319. b = append(b, lg.timeSeparator...)
  320. if t.Second() < 10 {
  321. b = append(b, '0')
  322. }
  323. b = strconv.AppendInt(b, int64(t.Second()), 10)
  324. return string(b)
  325. }
  326. // FmtTimeLong returns the long time representation of 't' for 'lg'
  327. func (lg *lg) FmtTimeLong(t time.Time) string {
  328. b := make([]byte, 0, 32)
  329. if t.Hour() < 10 {
  330. b = append(b, '0')
  331. }
  332. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  333. b = append(b, lg.timeSeparator...)
  334. if t.Minute() < 10 {
  335. b = append(b, '0')
  336. }
  337. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  338. b = append(b, lg.timeSeparator...)
  339. if t.Second() < 10 {
  340. b = append(b, '0')
  341. }
  342. b = strconv.AppendInt(b, int64(t.Second()), 10)
  343. b = append(b, []byte{0x20}...)
  344. tz, _ := t.Zone()
  345. b = append(b, tz...)
  346. return string(b)
  347. }
  348. // FmtTimeFull returns the full time representation of 't' for 'lg'
  349. func (lg *lg) FmtTimeFull(t time.Time) string {
  350. b := make([]byte, 0, 32)
  351. if t.Hour() < 10 {
  352. b = append(b, '0')
  353. }
  354. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  355. b = append(b, lg.timeSeparator...)
  356. if t.Minute() < 10 {
  357. b = append(b, '0')
  358. }
  359. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  360. b = append(b, lg.timeSeparator...)
  361. if t.Second() < 10 {
  362. b = append(b, '0')
  363. }
  364. b = strconv.AppendInt(b, int64(t.Second()), 10)
  365. b = append(b, []byte{0x20}...)
  366. tz, _ := t.Zone()
  367. if btz, ok := lg.timezones[tz]; ok {
  368. b = append(b, btz...)
  369. } else {
  370. b = append(b, tz...)
  371. }
  372. return string(b)
  373. }