lu.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. package lu
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type lu 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 'lu' locale
  39. func New() locales.Translator {
  40. return &lu{
  41. locale: "lu",
  42. pluralsCardinal: nil,
  43. pluralsOrdinal: nil,
  44. pluralsRange: nil,
  45. decimal: ",",
  46. group: ".",
  47. timeSeparator: ":",
  48. 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 ", "FC", "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 ", "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 "},
  49. monthsAbbreviated: []string{"", "Cio", "Lui", "Lus", "Muu", "Lum", "Luf", "Kab", "Lush", "Lut", "Lun", "Kas", "Cis"},
  50. monthsNarrow: []string{"", "C", "L", "L", "M", "L", "L", "K", "L", "L", "L", "K", "C"},
  51. monthsWide: []string{"", "Ciongo", "Lùishi", "Lusòlo", "Mùuyà", "Lumùngùlù", "Lufuimi", "Kabàlàshìpù", "Lùshìkà", "Lutongolo", "Lungùdi", "Kaswèkèsè", "Ciswà"},
  52. daysAbbreviated: []string{"Lum", "Nko", "Ndy", "Ndg", "Njw", "Ngv", "Lub"},
  53. daysNarrow: []string{"L", "N", "N", "N", "N", "N", "L"},
  54. daysWide: []string{"Lumingu", "Nkodya", "Ndàayà", "Ndangù", "Njòwa", "Ngòvya", "Lubingu"},
  55. periodsAbbreviated: []string{"Dinda", "Dilolo"},
  56. periodsWide: []string{"Dinda", "Dilolo"},
  57. erasAbbreviated: []string{"kmp. Y.K.", "kny. Y. K."},
  58. erasNarrow: []string{"", ""},
  59. erasWide: []string{"Kumpala kwa Yezu Kli", "Kunyima kwa Yezu Kli"},
  60. timezones: map[string]string{"MDT": "MDT", "LHDT": "LHDT", "GYT": "GYT", "ChST": "ChST", "ACWST": "ACWST", "∅∅∅": "∅∅∅", "HKT": "HKT", "WIT": "WIT", "WESZ": "WESZ", "AKST": "AKST", "CST": "CST", "CHADT": "CHADT", "AWST": "AWST", "EST": "EST", "SAST": "SAST", "TMT": "TMT", "CLT": "CLT", "EDT": "EDT", "AWDT": "AWDT", "NZST": "NZST", "UYST": "UYST", "AEDT": "AEDT", "WITA": "WITA", "JDT": "JDT", "UYT": "UYT", "ART": "ART", "WART": "WART", "WAST": "WAST", "OESZ": "OESZ", "HAT": "HAT", "MYT": "MYT", "LHST": "LHST", "CLST": "CLST", "AEST": "AEST", "HADT": "HADT", "HKST": "HKST", "ACST": "ACST", "HNT": "HNT", "WIB": "WIB", "GMT": "GMT", "TMST": "TMST", "MST": "MST", "ECT": "ECT", "CAT": "CAT", "BT": "BT", "BOT": "BOT", "CHAST": "CHAST", "GFT": "GFT", "PDT": "PDT", "WEZ": "WEZ", "WAT": "WAT", "CDT": "CDT", "MEZ": "MEZ", "AST": "AST", "ADT": "ADT", "HAST": "HAST", "COT": "COT", "SGT": "SGT", "PST": "PST", "SRT": "SRT", "ACWDT": "ACWDT", "COST": "COST", "IST": "IST", "EAT": "EAT", "VET": "VET", "WARST": "WARST", "ACDT": "ACDT", "AKDT": "AKDT", "OEZ": "OEZ", "MESZ": "MESZ", "ARST": "ARST", "JST": "JST", "NZDT": "NZDT"},
  61. }
  62. }
  63. // Locale returns the current translators string locale
  64. func (lu *lu) Locale() string {
  65. return lu.locale
  66. }
  67. // PluralsCardinal returns the list of cardinal plural rules associated with 'lu'
  68. func (lu *lu) PluralsCardinal() []locales.PluralRule {
  69. return lu.pluralsCardinal
  70. }
  71. // PluralsOrdinal returns the list of ordinal plural rules associated with 'lu'
  72. func (lu *lu) PluralsOrdinal() []locales.PluralRule {
  73. return lu.pluralsOrdinal
  74. }
  75. // PluralsRange returns the list of range plural rules associated with 'lu'
  76. func (lu *lu) PluralsRange() []locales.PluralRule {
  77. return lu.pluralsRange
  78. }
  79. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'lu'
  80. func (lu *lu) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  81. return locales.PluralRuleUnknown
  82. }
  83. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'lu'
  84. func (lu *lu) 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 'lu'
  88. func (lu *lu) 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 (lu *lu) MonthAbbreviated(month time.Month) string {
  93. return lu.monthsAbbreviated[month]
  94. }
  95. // MonthsAbbreviated returns the locales abbreviated months
  96. func (lu *lu) MonthsAbbreviated() []string {
  97. return lu.monthsAbbreviated[1:]
  98. }
  99. // MonthNarrow returns the locales narrow month given the 'month' provided
  100. func (lu *lu) MonthNarrow(month time.Month) string {
  101. return lu.monthsNarrow[month]
  102. }
  103. // MonthsNarrow returns the locales narrow months
  104. func (lu *lu) MonthsNarrow() []string {
  105. return lu.monthsNarrow[1:]
  106. }
  107. // MonthWide returns the locales wide month given the 'month' provided
  108. func (lu *lu) MonthWide(month time.Month) string {
  109. return lu.monthsWide[month]
  110. }
  111. // MonthsWide returns the locales wide months
  112. func (lu *lu) MonthsWide() []string {
  113. return lu.monthsWide[1:]
  114. }
  115. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  116. func (lu *lu) WeekdayAbbreviated(weekday time.Weekday) string {
  117. return lu.daysAbbreviated[weekday]
  118. }
  119. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  120. func (lu *lu) WeekdaysAbbreviated() []string {
  121. return lu.daysAbbreviated
  122. }
  123. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  124. func (lu *lu) WeekdayNarrow(weekday time.Weekday) string {
  125. return lu.daysNarrow[weekday]
  126. }
  127. // WeekdaysNarrow returns the locales narrow weekdays
  128. func (lu *lu) WeekdaysNarrow() []string {
  129. return lu.daysNarrow
  130. }
  131. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  132. func (lu *lu) WeekdayShort(weekday time.Weekday) string {
  133. return lu.daysShort[weekday]
  134. }
  135. // WeekdaysShort returns the locales short weekdays
  136. func (lu *lu) WeekdaysShort() []string {
  137. return lu.daysShort
  138. }
  139. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  140. func (lu *lu) WeekdayWide(weekday time.Weekday) string {
  141. return lu.daysWide[weekday]
  142. }
  143. // WeekdaysWide returns the locales wide weekdays
  144. func (lu *lu) WeekdaysWide() []string {
  145. return lu.daysWide
  146. }
  147. // FmtNumber returns 'num' with digits/precision of 'v' for 'lu' and handles both Whole and Real numbers based on 'v'
  148. func (lu *lu) FmtNumber(num float64, v uint64) string {
  149. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  150. l := len(s) + 1 + 1*len(s[:len(s)-int(v)-1])/3
  151. count := 0
  152. inWhole := v == 0
  153. b := make([]byte, 0, l)
  154. for i := len(s) - 1; i >= 0; i-- {
  155. if s[i] == '.' {
  156. b = append(b, lu.decimal[0])
  157. inWhole = true
  158. continue
  159. }
  160. if inWhole {
  161. if count == 3 {
  162. b = append(b, lu.group[0])
  163. count = 1
  164. } else {
  165. count++
  166. }
  167. }
  168. b = append(b, s[i])
  169. }
  170. if num < 0 {
  171. b = append(b, lu.minus[0])
  172. }
  173. // reverse
  174. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  175. b[i], b[j] = b[j], b[i]
  176. }
  177. return string(b)
  178. }
  179. // FmtPercent returns 'num' with digits/precision of 'v' for 'lu' and handles both Whole and Real numbers based on 'v'
  180. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  181. func (lu *lu) FmtPercent(num float64, v uint64) string {
  182. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  183. }
  184. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'lu'
  185. func (lu *lu) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  186. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  187. symbol := lu.currencies[currency]
  188. l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3
  189. count := 0
  190. inWhole := v == 0
  191. b := make([]byte, 0, l)
  192. for i := len(s) - 1; i >= 0; i-- {
  193. if s[i] == '.' {
  194. b = append(b, lu.decimal[0])
  195. inWhole = true
  196. continue
  197. }
  198. if inWhole {
  199. if count == 3 {
  200. b = append(b, lu.group[0])
  201. count = 1
  202. } else {
  203. count++
  204. }
  205. }
  206. b = append(b, s[i])
  207. }
  208. if num < 0 {
  209. b = append(b, lu.minus[0])
  210. }
  211. // reverse
  212. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  213. b[i], b[j] = b[j], b[i]
  214. }
  215. if int(v) < 2 {
  216. if v == 0 {
  217. b = append(b, lu.decimal...)
  218. }
  219. for i := 0; i < 2-int(v); i++ {
  220. b = append(b, '0')
  221. }
  222. }
  223. b = append(b, symbol...)
  224. return string(b)
  225. }
  226. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'lu'
  227. // in accounting notation.
  228. func (lu *lu) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  229. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  230. symbol := lu.currencies[currency]
  231. l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3
  232. count := 0
  233. inWhole := v == 0
  234. b := make([]byte, 0, l)
  235. for i := len(s) - 1; i >= 0; i-- {
  236. if s[i] == '.' {
  237. b = append(b, lu.decimal[0])
  238. inWhole = true
  239. continue
  240. }
  241. if inWhole {
  242. if count == 3 {
  243. b = append(b, lu.group[0])
  244. count = 1
  245. } else {
  246. count++
  247. }
  248. }
  249. b = append(b, s[i])
  250. }
  251. if num < 0 {
  252. b = append(b, lu.minus[0])
  253. }
  254. // reverse
  255. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  256. b[i], b[j] = b[j], b[i]
  257. }
  258. if int(v) < 2 {
  259. if v == 0 {
  260. b = append(b, lu.decimal...)
  261. }
  262. for i := 0; i < 2-int(v); i++ {
  263. b = append(b, '0')
  264. }
  265. }
  266. if num < 0 {
  267. b = append(b, symbol...)
  268. } else {
  269. b = append(b, symbol...)
  270. }
  271. return string(b)
  272. }
  273. // FmtDateShort returns the short date representation of 't' for 'lu'
  274. func (lu *lu) FmtDateShort(t time.Time) string {
  275. b := make([]byte, 0, 32)
  276. b = strconv.AppendInt(b, int64(t.Day()), 10)
  277. b = append(b, []byte{0x2f}...)
  278. b = strconv.AppendInt(b, int64(t.Month()), 10)
  279. b = append(b, []byte{0x2f}...)
  280. b = strconv.AppendInt(b, int64(t.Year()), 10)
  281. return string(b)
  282. }
  283. // FmtDateMedium returns the medium date representation of 't' for 'lu'
  284. func (lu *lu) FmtDateMedium(t time.Time) string {
  285. b := make([]byte, 0, 32)
  286. b = strconv.AppendInt(b, int64(t.Day()), 10)
  287. b = append(b, []byte{0x20}...)
  288. b = append(b, lu.monthsAbbreviated[t.Month()]...)
  289. b = append(b, []byte{0x20}...)
  290. b = strconv.AppendInt(b, int64(t.Year()), 10)
  291. return string(b)
  292. }
  293. // FmtDateLong returns the long date representation of 't' for 'lu'
  294. func (lu *lu) FmtDateLong(t time.Time) string {
  295. b := make([]byte, 0, 32)
  296. b = strconv.AppendInt(b, int64(t.Day()), 10)
  297. b = append(b, []byte{0x20}...)
  298. b = append(b, lu.monthsWide[t.Month()]...)
  299. b = append(b, []byte{0x20}...)
  300. b = strconv.AppendInt(b, int64(t.Year()), 10)
  301. return string(b)
  302. }
  303. // FmtDateFull returns the full date representation of 't' for 'lu'
  304. func (lu *lu) FmtDateFull(t time.Time) string {
  305. b := make([]byte, 0, 32)
  306. b = append(b, lu.daysWide[t.Weekday()]...)
  307. b = append(b, []byte{0x20}...)
  308. b = strconv.AppendInt(b, int64(t.Day()), 10)
  309. b = append(b, []byte{0x20}...)
  310. b = append(b, lu.monthsWide[t.Month()]...)
  311. b = append(b, []byte{0x20}...)
  312. b = strconv.AppendInt(b, int64(t.Year()), 10)
  313. return string(b)
  314. }
  315. // FmtTimeShort returns the short time representation of 't' for 'lu'
  316. func (lu *lu) FmtTimeShort(t time.Time) string {
  317. b := make([]byte, 0, 32)
  318. if t.Hour() < 10 {
  319. b = append(b, '0')
  320. }
  321. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  322. b = append(b, lu.timeSeparator...)
  323. if t.Minute() < 10 {
  324. b = append(b, '0')
  325. }
  326. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  327. return string(b)
  328. }
  329. // FmtTimeMedium returns the medium time representation of 't' for 'lu'
  330. func (lu *lu) FmtTimeMedium(t time.Time) string {
  331. b := make([]byte, 0, 32)
  332. if t.Hour() < 10 {
  333. b = append(b, '0')
  334. }
  335. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  336. b = append(b, lu.timeSeparator...)
  337. if t.Minute() < 10 {
  338. b = append(b, '0')
  339. }
  340. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  341. b = append(b, lu.timeSeparator...)
  342. if t.Second() < 10 {
  343. b = append(b, '0')
  344. }
  345. b = strconv.AppendInt(b, int64(t.Second()), 10)
  346. return string(b)
  347. }
  348. // FmtTimeLong returns the long time representation of 't' for 'lu'
  349. func (lu *lu) FmtTimeLong(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, lu.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, lu.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. b = append(b, tz...)
  368. return string(b)
  369. }
  370. // FmtTimeFull returns the full time representation of 't' for 'lu'
  371. func (lu *lu) FmtTimeFull(t time.Time) string {
  372. b := make([]byte, 0, 32)
  373. if t.Hour() < 10 {
  374. b = append(b, '0')
  375. }
  376. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  377. b = append(b, lu.timeSeparator...)
  378. if t.Minute() < 10 {
  379. b = append(b, '0')
  380. }
  381. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  382. b = append(b, lu.timeSeparator...)
  383. if t.Second() < 10 {
  384. b = append(b, '0')
  385. }
  386. b = strconv.AppendInt(b, int64(t.Second()), 10)
  387. b = append(b, []byte{0x20}...)
  388. tz, _ := t.Zone()
  389. if btz, ok := lu.timezones[tz]; ok {
  390. b = append(b, btz...)
  391. } else {
  392. b = append(b, tz...)
  393. }
  394. return string(b)
  395. }