ig.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. package ig
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type ig 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. currencyNegativePrefix string
  23. currencyNegativeSuffix string
  24. monthsAbbreviated []string
  25. monthsNarrow []string
  26. monthsWide []string
  27. daysAbbreviated []string
  28. daysNarrow []string
  29. daysShort []string
  30. daysWide []string
  31. periodsAbbreviated []string
  32. periodsNarrow []string
  33. periodsShort []string
  34. periodsWide []string
  35. erasAbbreviated []string
  36. erasNarrow []string
  37. erasWide []string
  38. timezones map[string]string
  39. }
  40. // New returns a new instance of translator for the 'ig' locale
  41. func New() locales.Translator {
  42. return &ig{
  43. locale: "ig",
  44. pluralsCardinal: []locales.PluralRule{6},
  45. pluralsOrdinal: nil,
  46. pluralsRange: nil,
  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", "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", "₦", "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. currencyNegativePrefix: "(",
  50. currencyNegativeSuffix: ")",
  51. monthsAbbreviated: []string{"", "Jen", "Feb", "Maa", "Epr", "Mee", "Juu", "Jul", "Ọgọ", "Sep", "Ọkt", "Nov", "Dis"},
  52. monthsWide: []string{"", "Jenụwarị", "Febrụwarị", "Maachị", "Eprel", "Mee", "Juun", "Julaị", "Ọgọọst", "Septemba", "Ọktoba", "Novemba", "Disemba"},
  53. daysAbbreviated: []string{"Ụka", "Mọn", "Tiu", "Wen", "Tọọ", "Fraị", "Sat"},
  54. daysWide: []string{"Mbọsị Ụka", "Mọnde", "Tiuzdee", "Wenezdee", "Tọọzdee", "Fraịdee", "Satọdee"},
  55. periodsAbbreviated: []string{"A.M.", "P.M."},
  56. periodsWide: []string{"A.M.", "P.M."},
  57. erasAbbreviated: []string{"T.K.", "A.K."},
  58. erasNarrow: []string{"", ""},
  59. erasWide: []string{"Tupu Kristi", "Afọ Kristi"},
  60. timezones: map[string]string{"ADT": "ADT", "WAST": "WAST", "COT": "COT", "HAT": "HAT", "WART": "WART", "WARST": "WARST", "GMT": "GMT", "EAT": "EAT", "ACDT": "ACDT", "BT": "BT", "WIB": "WIB", "PDT": "PDT", "ARST": "ARST", "HAST": "HAST", "ACWST": "ACWST", "JST": "JST", "TMT": "TMT", "SGT": "SGT", "SRT": "SRT", "CDT": "CDT", "WEZ": "WEZ", "WESZ": "WESZ", "CHADT": "CHADT", "HADT": "HADT", "OEZ": "OEZ", "COST": "COST", "WIT": "WIT", "VET": "VET", "HKT": "HKT", "PST": "PST", "CLST": "CLST", "GYT": "GYT", "CLT": "CLT", "UYT": "UYT", "MDT": "MDT", "CHAST": "CHAST", "NZDT": "NZDT", "ACWDT": "ACWDT", "LHST": "LHST", "MST": "MST", "NZST": "NZST", "CAT": "CAT", "ACST": "ACST", "LHDT": "LHDT", "UYST": "UYST", "AWST": "AWST", "OESZ": "OESZ", "WITA": "WITA", "AEST": "AEST", "AEDT": "AEDT", "SAST": "SAST", "JDT": "JDT", "HKST": "HKST", "AWDT": "AWDT", "ChST": "ChST", "ART": "ART", "ECT": "ECT", "MESZ": "MESZ", "BOT": "BOT", "EDT": "EDT", "MYT": "MYT", "AKST": "AKST", "WAT": "WAT", "HNT": "HNT", "CST": "CST", "TMST": "TMST", "MEZ": "MEZ", "EST": "EST", "∅∅∅": "∅∅∅", "AKDT": "AKDT", "AST": "AST", "IST": "IST", "GFT": "GFT"},
  61. }
  62. }
  63. // Locale returns the current translators string locale
  64. func (ig *ig) Locale() string {
  65. return ig.locale
  66. }
  67. // PluralsCardinal returns the list of cardinal plural rules associated with 'ig'
  68. func (ig *ig) PluralsCardinal() []locales.PluralRule {
  69. return ig.pluralsCardinal
  70. }
  71. // PluralsOrdinal returns the list of ordinal plural rules associated with 'ig'
  72. func (ig *ig) PluralsOrdinal() []locales.PluralRule {
  73. return ig.pluralsOrdinal
  74. }
  75. // PluralsRange returns the list of range plural rules associated with 'ig'
  76. func (ig *ig) PluralsRange() []locales.PluralRule {
  77. return ig.pluralsRange
  78. }
  79. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'ig'
  80. func (ig *ig) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  81. return locales.PluralRuleOther
  82. }
  83. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'ig'
  84. func (ig *ig) 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 'ig'
  88. func (ig *ig) 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 (ig *ig) MonthAbbreviated(month time.Month) string {
  93. return ig.monthsAbbreviated[month]
  94. }
  95. // MonthsAbbreviated returns the locales abbreviated months
  96. func (ig *ig) MonthsAbbreviated() []string {
  97. return ig.monthsAbbreviated[1:]
  98. }
  99. // MonthNarrow returns the locales narrow month given the 'month' provided
  100. func (ig *ig) MonthNarrow(month time.Month) string {
  101. return ig.monthsNarrow[month]
  102. }
  103. // MonthsNarrow returns the locales narrow months
  104. func (ig *ig) MonthsNarrow() []string {
  105. return nil
  106. }
  107. // MonthWide returns the locales wide month given the 'month' provided
  108. func (ig *ig) MonthWide(month time.Month) string {
  109. return ig.monthsWide[month]
  110. }
  111. // MonthsWide returns the locales wide months
  112. func (ig *ig) MonthsWide() []string {
  113. return ig.monthsWide[1:]
  114. }
  115. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  116. func (ig *ig) WeekdayAbbreviated(weekday time.Weekday) string {
  117. return ig.daysAbbreviated[weekday]
  118. }
  119. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  120. func (ig *ig) WeekdaysAbbreviated() []string {
  121. return ig.daysAbbreviated
  122. }
  123. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  124. func (ig *ig) WeekdayNarrow(weekday time.Weekday) string {
  125. return ig.daysNarrow[weekday]
  126. }
  127. // WeekdaysNarrow returns the locales narrow weekdays
  128. func (ig *ig) WeekdaysNarrow() []string {
  129. return ig.daysNarrow
  130. }
  131. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  132. func (ig *ig) WeekdayShort(weekday time.Weekday) string {
  133. return ig.daysShort[weekday]
  134. }
  135. // WeekdaysShort returns the locales short weekdays
  136. func (ig *ig) WeekdaysShort() []string {
  137. return ig.daysShort
  138. }
  139. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  140. func (ig *ig) WeekdayWide(weekday time.Weekday) string {
  141. return ig.daysWide[weekday]
  142. }
  143. // WeekdaysWide returns the locales wide weekdays
  144. func (ig *ig) WeekdaysWide() []string {
  145. return ig.daysWide
  146. }
  147. // FmtNumber returns 'num' with digits/precision of 'v' for 'ig' and handles both Whole and Real numbers based on 'v'
  148. func (ig *ig) 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 'ig' 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 (ig *ig) 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 'ig'
  157. func (ig *ig) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  158. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  159. symbol := ig.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, ig.decimal[0])
  167. inWhole = true
  168. continue
  169. }
  170. if inWhole {
  171. if count == 3 {
  172. b = append(b, ig.group[0])
  173. count = 1
  174. } else {
  175. count++
  176. }
  177. }
  178. b = append(b, s[i])
  179. }
  180. for j := len(symbol) - 1; j >= 0; j-- {
  181. b = append(b, symbol[j])
  182. }
  183. if num < 0 {
  184. b = append(b, ig.minus[0])
  185. }
  186. // reverse
  187. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  188. b[i], b[j] = b[j], b[i]
  189. }
  190. if int(v) < 2 {
  191. if v == 0 {
  192. b = append(b, ig.decimal...)
  193. }
  194. for i := 0; i < 2-int(v); i++ {
  195. b = append(b, '0')
  196. }
  197. }
  198. return string(b)
  199. }
  200. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'ig'
  201. // in accounting notation.
  202. func (ig *ig) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  203. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  204. symbol := ig.currencies[currency]
  205. l := len(s) + len(symbol) + 2 + 0*len(s[:len(s)-int(v)-1])/3
  206. count := 0
  207. inWhole := v == 0
  208. b := make([]byte, 0, l)
  209. for i := len(s) - 1; i >= 0; i-- {
  210. if s[i] == '.' {
  211. b = append(b, ig.decimal[0])
  212. inWhole = true
  213. continue
  214. }
  215. if inWhole {
  216. if count == 3 {
  217. b = append(b, ig.group[0])
  218. count = 1
  219. } else {
  220. count++
  221. }
  222. }
  223. b = append(b, s[i])
  224. }
  225. if num < 0 {
  226. for j := len(symbol) - 1; j >= 0; j-- {
  227. b = append(b, symbol[j])
  228. }
  229. b = append(b, ig.currencyNegativePrefix[0])
  230. } else {
  231. for j := len(symbol) - 1; j >= 0; j-- {
  232. b = append(b, symbol[j])
  233. }
  234. }
  235. // reverse
  236. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  237. b[i], b[j] = b[j], b[i]
  238. }
  239. if int(v) < 2 {
  240. if v == 0 {
  241. b = append(b, ig.decimal...)
  242. }
  243. for i := 0; i < 2-int(v); i++ {
  244. b = append(b, '0')
  245. }
  246. }
  247. if num < 0 {
  248. b = append(b, ig.currencyNegativeSuffix...)
  249. }
  250. return string(b)
  251. }
  252. // FmtDateShort returns the short date representation of 't' for 'ig'
  253. func (ig *ig) FmtDateShort(t time.Time) string {
  254. b := make([]byte, 0, 32)
  255. if t.Day() < 10 {
  256. b = append(b, '0')
  257. }
  258. b = strconv.AppendInt(b, int64(t.Day()), 10)
  259. b = append(b, []byte{0x2f}...)
  260. if t.Month() < 10 {
  261. b = append(b, '0')
  262. }
  263. b = strconv.AppendInt(b, int64(t.Month()), 10)
  264. b = append(b, []byte{0x2f}...)
  265. b = strconv.AppendInt(b, int64(t.Year()), 10)
  266. return string(b)
  267. }
  268. // FmtDateMedium returns the medium date representation of 't' for 'ig'
  269. func (ig *ig) FmtDateMedium(t time.Time) string {
  270. b := make([]byte, 0, 32)
  271. b = strconv.AppendInt(b, int64(t.Day()), 10)
  272. b = append(b, []byte{0x20}...)
  273. b = append(b, ig.monthsAbbreviated[t.Month()]...)
  274. b = append(b, []byte{0x20}...)
  275. b = strconv.AppendInt(b, int64(t.Year()), 10)
  276. return string(b)
  277. }
  278. // FmtDateLong returns the long date representation of 't' for 'ig'
  279. func (ig *ig) FmtDateLong(t time.Time) string {
  280. b := make([]byte, 0, 32)
  281. b = strconv.AppendInt(b, int64(t.Day()), 10)
  282. b = append(b, []byte{0x20}...)
  283. b = append(b, ig.monthsWide[t.Month()]...)
  284. b = append(b, []byte{0x20}...)
  285. b = strconv.AppendInt(b, int64(t.Year()), 10)
  286. return string(b)
  287. }
  288. // FmtDateFull returns the full date representation of 't' for 'ig'
  289. func (ig *ig) FmtDateFull(t time.Time) string {
  290. b := make([]byte, 0, 32)
  291. b = append(b, ig.daysWide[t.Weekday()]...)
  292. b = append(b, []byte{0x2c, 0x20}...)
  293. b = strconv.AppendInt(b, int64(t.Day()), 10)
  294. b = append(b, []byte{0x20}...)
  295. b = append(b, ig.monthsWide[t.Month()]...)
  296. b = append(b, []byte{0x20}...)
  297. b = strconv.AppendInt(b, int64(t.Year()), 10)
  298. return string(b)
  299. }
  300. // FmtTimeShort returns the short time representation of 't' for 'ig'
  301. func (ig *ig) FmtTimeShort(t time.Time) string {
  302. b := make([]byte, 0, 32)
  303. h := t.Hour()
  304. if h > 12 {
  305. h -= 12
  306. }
  307. b = strconv.AppendInt(b, int64(h), 10)
  308. b = append(b, ig.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, []byte{0x20}...)
  314. if t.Hour() < 12 {
  315. b = append(b, ig.periodsAbbreviated[0]...)
  316. } else {
  317. b = append(b, ig.periodsAbbreviated[1]...)
  318. }
  319. return string(b)
  320. }
  321. // FmtTimeMedium returns the medium time representation of 't' for 'ig'
  322. func (ig *ig) FmtTimeMedium(t time.Time) string {
  323. b := make([]byte, 0, 32)
  324. h := t.Hour()
  325. if h > 12 {
  326. h -= 12
  327. }
  328. b = strconv.AppendInt(b, int64(h), 10)
  329. b = append(b, ig.timeSeparator...)
  330. if t.Minute() < 10 {
  331. b = append(b, '0')
  332. }
  333. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  334. b = append(b, ig.timeSeparator...)
  335. if t.Second() < 10 {
  336. b = append(b, '0')
  337. }
  338. b = strconv.AppendInt(b, int64(t.Second()), 10)
  339. b = append(b, []byte{0x20}...)
  340. if t.Hour() < 12 {
  341. b = append(b, ig.periodsAbbreviated[0]...)
  342. } else {
  343. b = append(b, ig.periodsAbbreviated[1]...)
  344. }
  345. return string(b)
  346. }
  347. // FmtTimeLong returns the long time representation of 't' for 'ig'
  348. func (ig *ig) FmtTimeLong(t time.Time) string {
  349. b := make([]byte, 0, 32)
  350. h := t.Hour()
  351. if h > 12 {
  352. h -= 12
  353. }
  354. b = strconv.AppendInt(b, int64(h), 10)
  355. b = append(b, ig.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, ig.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. if t.Hour() < 12 {
  367. b = append(b, ig.periodsAbbreviated[0]...)
  368. } else {
  369. b = append(b, ig.periodsAbbreviated[1]...)
  370. }
  371. b = append(b, []byte{0x20}...)
  372. tz, _ := t.Zone()
  373. b = append(b, tz...)
  374. return string(b)
  375. }
  376. // FmtTimeFull returns the full time representation of 't' for 'ig'
  377. func (ig *ig) FmtTimeFull(t time.Time) string {
  378. b := make([]byte, 0, 32)
  379. h := t.Hour()
  380. if h > 12 {
  381. h -= 12
  382. }
  383. b = strconv.AppendInt(b, int64(h), 10)
  384. b = append(b, ig.timeSeparator...)
  385. if t.Minute() < 10 {
  386. b = append(b, '0')
  387. }
  388. b = strconv.AppendInt(b, int64(t.Minute()), 10)
  389. b = append(b, ig.timeSeparator...)
  390. if t.Second() < 10 {
  391. b = append(b, '0')
  392. }
  393. b = strconv.AppendInt(b, int64(t.Second()), 10)
  394. b = append(b, []byte{0x20}...)
  395. if t.Hour() < 12 {
  396. b = append(b, ig.periodsAbbreviated[0]...)
  397. } else {
  398. b = append(b, ig.periodsAbbreviated[1]...)
  399. }
  400. b = append(b, []byte{0x20}...)
  401. tz, _ := t.Zone()
  402. if btz, ok := ig.timezones[tz]; ok {
  403. b = append(b, btz...)
  404. } else {
  405. b = append(b, tz...)
  406. }
  407. return string(b)
  408. }