luo.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. package luo
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type luo 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 'luo' locale
  39. func New() locales.Translator {
  40. return &luo{
  41. locale: "luo",
  42. pluralsCardinal: nil,
  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", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "CLP", "CNH", "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", "Ksh", "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", "STN", "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"},
  47. monthsAbbreviated: []string{"", "DAC", "DAR", "DAD", "DAN", "DAH", "DAU", "DAO", "DAB", "DOC", "DAP", "DGI", "DAG"},
  48. monthsNarrow: []string{"", "C", "R", "D", "N", "B", "U", "B", "B", "C", "P", "C", "P"},
  49. monthsWide: []string{"", "Dwe mar Achiel", "Dwe mar Ariyo", "Dwe mar Adek", "Dwe mar Ang’wen", "Dwe mar Abich", "Dwe mar Auchiel", "Dwe mar Abiriyo", "Dwe mar Aboro", "Dwe mar Ochiko", "Dwe mar Apar", "Dwe mar gi achiel", "Dwe mar Apar gi ariyo"},
  50. daysAbbreviated: []string{"JMP", "WUT", "TAR", "TAD", "TAN", "TAB", "NGS"},
  51. daysNarrow: []string{"J", "W", "T", "T", "T", "T", "N"},
  52. daysWide: []string{"Jumapil", "Wuok Tich", "Tich Ariyo", "Tich Adek", "Tich Ang’wen", "Tich Abich", "Ngeso"},
  53. periodsAbbreviated: []string{"OD", "OT"},
  54. periodsWide: []string{"OD", "OT"},
  55. erasAbbreviated: []string{"BC", "AD"},
  56. erasNarrow: []string{"", ""},
  57. erasWide: []string{"Kapok Kristo obiro", "Ka Kristo osebiro"},
  58. timezones: map[string]string{"WAT": "WAT", "ACDT": "ACDT", "ACWST": "ACWST", "WITA": "WITA", "TMT": "TMT", "GMT": "GMT", "UYT": "UYT", "HNCU": "HNCU", "AEDT": "AEDT", "BOT": "BOT", "HEEG": "HEEG", "MEZ": "MEZ", "ART": "ART", "HNPMX": "HNPMX", "JST": "JST", "MESZ": "MESZ", "WARST": "WARST", "HEPM": "HEPM", "HNNOMX": "HNNOMX", "EAT": "EAT", "HADT": "HADT", "PDT": "PDT", "LHDT": "LHDT", "CLT": "CLT", "OEZ": "OEZ", "CHADT": "CHADT", "AST": "AST", "ACST": "ACST", "HNEG": "HNEG", "COST": "COST", "EST": "EST", "MDT": "MDT", "TMST": "TMST", "AWDT": "AWDT", "BT": "BT", "HKT": "HKT", "SAST": "SAST", "HEOG": "HEOG", "HAT": "HAT", "AEST": "AEST", "WESZ": "WESZ", "WIB": "WIB", "VET": "VET", "SRT": "SRT", "CLST": "CLST", "COT": "COT", "UYST": "UYST", "MYT": "MYT", "EDT": "EDT", "HECU": "HECU", "CDT": "CDT", "PST": "PST", "HEPMX": "HEPMX", "ADT": "ADT", "WEZ": "WEZ", "OESZ": "OESZ", "HAST": "HAST", "CHAST": "CHAST", "WAST": "WAST", "NZDT": "NZDT", "AKDT": "AKDT", "SGT": "SGT", "AKST": "AKST", "HKST": "HKST", "HENOMX": "HENOMX", "ARST": "ARST", "ChST": "ChST", "CST": "CST", "NZST": "NZST", "GFT": "GFT", "ACWDT": "ACWDT", "IST": "IST", "LHST": "LHST", "HNPM": "HNPM", "MST": "MST", "CAT": "CAT", "AWST": "AWST", "ECT": "ECT", "∅∅∅": "∅∅∅", "HNT": "HNT", "HNOG": "HNOG", "WART": "WART", "WIT": "WIT", "GYT": "GYT", "JDT": "JDT"},
  59. }
  60. }
  61. // Locale returns the current translators string locale
  62. func (luo *luo) Locale() string {
  63. return luo.locale
  64. }
  65. // PluralsCardinal returns the list of cardinal plural rules associated with 'luo'
  66. func (luo *luo) PluralsCardinal() []locales.PluralRule {
  67. return luo.pluralsCardinal
  68. }
  69. // PluralsOrdinal returns the list of ordinal plural rules associated with 'luo'
  70. func (luo *luo) PluralsOrdinal() []locales.PluralRule {
  71. return luo.pluralsOrdinal
  72. }
  73. // PluralsRange returns the list of range plural rules associated with 'luo'
  74. func (luo *luo) PluralsRange() []locales.PluralRule {
  75. return luo.pluralsRange
  76. }
  77. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'luo'
  78. func (luo *luo) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  79. return locales.PluralRuleUnknown
  80. }
  81. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'luo'
  82. func (luo *luo) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  83. return locales.PluralRuleUnknown
  84. }
  85. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'luo'
  86. func (luo *luo) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  87. return locales.PluralRuleUnknown
  88. }
  89. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  90. func (luo *luo) MonthAbbreviated(month time.Month) string {
  91. return luo.monthsAbbreviated[month]
  92. }
  93. // MonthsAbbreviated returns the locales abbreviated months
  94. func (luo *luo) MonthsAbbreviated() []string {
  95. return luo.monthsAbbreviated[1:]
  96. }
  97. // MonthNarrow returns the locales narrow month given the 'month' provided
  98. func (luo *luo) MonthNarrow(month time.Month) string {
  99. return luo.monthsNarrow[month]
  100. }
  101. // MonthsNarrow returns the locales narrow months
  102. func (luo *luo) MonthsNarrow() []string {
  103. return luo.monthsNarrow[1:]
  104. }
  105. // MonthWide returns the locales wide month given the 'month' provided
  106. func (luo *luo) MonthWide(month time.Month) string {
  107. return luo.monthsWide[month]
  108. }
  109. // MonthsWide returns the locales wide months
  110. func (luo *luo) MonthsWide() []string {
  111. return luo.monthsWide[1:]
  112. }
  113. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  114. func (luo *luo) WeekdayAbbreviated(weekday time.Weekday) string {
  115. return luo.daysAbbreviated[weekday]
  116. }
  117. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  118. func (luo *luo) WeekdaysAbbreviated() []string {
  119. return luo.daysAbbreviated
  120. }
  121. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  122. func (luo *luo) WeekdayNarrow(weekday time.Weekday) string {
  123. return luo.daysNarrow[weekday]
  124. }
  125. // WeekdaysNarrow returns the locales narrow weekdays
  126. func (luo *luo) WeekdaysNarrow() []string {
  127. return luo.daysNarrow
  128. }
  129. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  130. func (luo *luo) WeekdayShort(weekday time.Weekday) string {
  131. return luo.daysShort[weekday]
  132. }
  133. // WeekdaysShort returns the locales short weekdays
  134. func (luo *luo) WeekdaysShort() []string {
  135. return luo.daysShort
  136. }
  137. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  138. func (luo *luo) WeekdayWide(weekday time.Weekday) string {
  139. return luo.daysWide[weekday]
  140. }
  141. // WeekdaysWide returns the locales wide weekdays
  142. func (luo *luo) WeekdaysWide() []string {
  143. return luo.daysWide
  144. }
  145. // Decimal returns the decimal point of number
  146. func (luo *luo) Decimal() string {
  147. return luo.decimal
  148. }
  149. // Group returns the group of number
  150. func (luo *luo) Group() string {
  151. return luo.group
  152. }
  153. // Group returns the minus sign of number
  154. func (luo *luo) Minus() string {
  155. return luo.minus
  156. }
  157. // FmtNumber returns 'num' with digits/precision of 'v' for 'luo' and handles both Whole and Real numbers based on 'v'
  158. func (luo *luo) FmtNumber(num float64, v uint64) string {
  159. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  160. }
  161. // FmtPercent returns 'num' with digits/precision of 'v' for 'luo' and handles both Whole and Real numbers based on 'v'
  162. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  163. func (luo *luo) FmtPercent(num float64, v uint64) string {
  164. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  165. }
  166. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'luo'
  167. func (luo *luo) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  168. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  169. symbol := luo.currencies[currency]
  170. l := len(s) + len(symbol) + 0
  171. count := 0
  172. inWhole := v == 0
  173. b := make([]byte, 0, l)
  174. for i := len(s) - 1; i >= 0; i-- {
  175. if s[i] == '.' {
  176. b = append(b, luo.decimal[0])
  177. inWhole = true
  178. continue
  179. }
  180. if inWhole {
  181. if count == 3 {
  182. b = append(b, luo.group[0])
  183. count = 1
  184. } else {
  185. count++
  186. }
  187. }
  188. b = append(b, s[i])
  189. }
  190. if num < 0 {
  191. b = append(b, luo.minus[0])
  192. }
  193. // reverse
  194. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  195. b[i], b[j] = b[j], b[i]
  196. }
  197. if int(v) < 2 {
  198. if v == 0 {
  199. b = append(b, luo.decimal...)
  200. }
  201. for i := 0; i < 2-int(v); i++ {
  202. b = append(b, '0')
  203. }
  204. }
  205. b = append(b, symbol...)
  206. return string(b)
  207. }
  208. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'luo'
  209. // in accounting notation.
  210. func (luo *luo) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  211. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  212. symbol := luo.currencies[currency]
  213. l := len(s) + len(symbol) + 0
  214. count := 0
  215. inWhole := v == 0
  216. b := make([]byte, 0, l)
  217. for i := len(s) - 1; i >= 0; i-- {
  218. if s[i] == '.' {
  219. b = append(b, luo.decimal[0])
  220. inWhole = true
  221. continue
  222. }
  223. if inWhole {
  224. if count == 3 {
  225. b = append(b, luo.group[0])
  226. count = 1
  227. } else {
  228. count++
  229. }
  230. }
  231. b = append(b, s[i])
  232. }
  233. if num < 0 {
  234. b = append(b, luo.minus[0])
  235. }
  236. // reverse
  237. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  238. b[i], b[j] = b[j], b[i]
  239. }
  240. if int(v) < 2 {
  241. if v == 0 {
  242. b = append(b, luo.decimal...)
  243. }
  244. for i := 0; i < 2-int(v); i++ {
  245. b = append(b, '0')
  246. }
  247. }
  248. if num < 0 {
  249. b = append(b, symbol...)
  250. } else {
  251. b = append(b, symbol...)
  252. }
  253. return string(b)
  254. }
  255. // FmtDateShort returns the short date representation of 't' for 'luo'
  256. func (luo *luo) FmtDateShort(t time.Time) string {
  257. b := make([]byte, 0, 32)
  258. if t.Day() < 10 {
  259. b = append(b, '0')
  260. }
  261. b = strconv.AppendInt(b, int64(t.Day()), 10)
  262. b = append(b, []byte{0x2f}...)
  263. if t.Month() < 10 {
  264. b = append(b, '0')
  265. }
  266. b = strconv.AppendInt(b, int64(t.Month()), 10)
  267. b = append(b, []byte{0x2f}...)
  268. if t.Year() > 0 {
  269. b = strconv.AppendInt(b, int64(t.Year()), 10)
  270. } else {
  271. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  272. }
  273. return string(b)
  274. }
  275. // FmtDateMedium returns the medium date representation of 't' for 'luo'
  276. func (luo *luo) FmtDateMedium(t time.Time) string {
  277. b := make([]byte, 0, 32)
  278. b = strconv.AppendInt(b, int64(t.Day()), 10)
  279. b = append(b, []byte{0x20}...)
  280. b = append(b, luo.monthsAbbreviated[t.Month()]...)
  281. b = append(b, []byte{0x20}...)
  282. if t.Year() > 0 {
  283. b = strconv.AppendInt(b, int64(t.Year()), 10)
  284. } else {
  285. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  286. }
  287. return string(b)
  288. }
  289. // FmtDateLong returns the long date representation of 't' for 'luo'
  290. func (luo *luo) FmtDateLong(t time.Time) string {
  291. b := make([]byte, 0, 32)
  292. b = strconv.AppendInt(b, int64(t.Day()), 10)
  293. b = append(b, []byte{0x20}...)
  294. b = append(b, luo.monthsWide[t.Month()]...)
  295. b = append(b, []byte{0x20}...)
  296. if t.Year() > 0 {
  297. b = strconv.AppendInt(b, int64(t.Year()), 10)
  298. } else {
  299. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  300. }
  301. return string(b)
  302. }
  303. // FmtDateFull returns the full date representation of 't' for 'luo'
  304. func (luo *luo) FmtDateFull(t time.Time) string {
  305. b := make([]byte, 0, 32)
  306. b = append(b, luo.daysWide[t.Weekday()]...)
  307. b = append(b, []byte{0x2c, 0x20}...)
  308. b = strconv.AppendInt(b, int64(t.Day()), 10)
  309. b = append(b, []byte{0x20}...)
  310. b = append(b, luo.monthsWide[t.Month()]...)
  311. b = append(b, []byte{0x20}...)
  312. if t.Year() > 0 {
  313. b = strconv.AppendInt(b, int64(t.Year()), 10)
  314. } else {
  315. b = strconv.AppendInt(b, int64(-t.Year()), 10)
  316. }
  317. return string(b)
  318. }
  319. // FmtTimeShort returns the short time representation of 't' for 'luo'
  320. func (luo *luo) FmtTimeShort(t time.Time) string {
  321. b := make([]byte, 0, 32)
  322. if t.Hour() < 10 {
  323. b = append(b, '0')
  324. }
  325. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  326. b = append(b, luo.timeSeparator...)
  327. if t.Minute() < 10 {
  328. b = append(b, '0')
  329. }
  330. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  331. return string(b)
  332. }
  333. // FmtTimeMedium returns the medium time representation of 't' for 'luo'
  334. func (luo *luo) FmtTimeMedium(t time.Time) string {
  335. b := make([]byte, 0, 32)
  336. if t.Hour() < 10 {
  337. b = append(b, '0')
  338. }
  339. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  340. b = append(b, luo.timeSeparator...)
  341. if t.Minute() < 10 {
  342. b = append(b, '0')
  343. }
  344. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  345. b = append(b, luo.timeSeparator...)
  346. if t.Second() < 10 {
  347. b = append(b, '0')
  348. }
  349. b = strconv.AppendInt(b, int64(t.Second()), 10)
  350. return string(b)
  351. }
  352. // FmtTimeLong returns the long time representation of 't' for 'luo'
  353. func (luo *luo) FmtTimeLong(t time.Time) string {
  354. b := make([]byte, 0, 32)
  355. if t.Hour() < 10 {
  356. b = append(b, '0')
  357. }
  358. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  359. b = append(b, luo.timeSeparator...)
  360. if t.Minute() < 10 {
  361. b = append(b, '0')
  362. }
  363. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  364. b = append(b, luo.timeSeparator...)
  365. if t.Second() < 10 {
  366. b = append(b, '0')
  367. }
  368. b = strconv.AppendInt(b, int64(t.Second()), 10)
  369. b = append(b, []byte{0x20}...)
  370. tz, _ := t.Zone()
  371. b = append(b, tz...)
  372. return string(b)
  373. }
  374. // FmtTimeFull returns the full time representation of 't' for 'luo'
  375. func (luo *luo) FmtTimeFull(t time.Time) string {
  376. b := make([]byte, 0, 32)
  377. if t.Hour() < 10 {
  378. b = append(b, '0')
  379. }
  380. b = strconv.AppendInt(b, int64(t.Hour()), 10)
  381. b = append(b, luo.timeSeparator...)
  382. if t.Minute() < 10 {
  383. b = append(b, '0')
  384. }
  385. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  386. b = append(b, luo.timeSeparator...)
  387. if t.Second() < 10 {
  388. b = append(b, '0')
  389. }
  390. b = strconv.AppendInt(b, int64(t.Second()), 10)
  391. b = append(b, []byte{0x20}...)
  392. tz, _ := t.Zone()
  393. if btz, ok := luo.timezones[tz]; ok {
  394. b = append(b, btz...)
  395. } else {
  396. b = append(b, tz...)
  397. }
  398. return string(b)
  399. }