tzm.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package tzm
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type tzm 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. currencyPositiveSuffix 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 'tzm' locale
  41. func New() locales.Translator {
  42. return &tzm{
  43. locale: "tzm",
  44. pluralsCardinal: []locales.PluralRule{2, 6},
  45. pluralsOrdinal: nil,
  46. pluralsRange: nil,
  47. decimal: ",",
  48. group: " ",
  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 ", "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 "},
  51. currencyPositiveSuffix: " ",
  52. currencyNegativeSuffix: " ",
  53. monthsAbbreviated: []string{"", "Yen", "Yeb", "Mar", "Ibr", "May", "Yun", "Yul", "Ɣuc", "Cut", "Kṭu", "Nwa", "Duj"},
  54. monthsNarrow: []string{"", "Y", "Y", "M", "I", "M", "Y", "Y", "Ɣ", "C", "K", "N", "D"},
  55. monthsWide: []string{"", "Yennayer", "Yebrayer", "Mars", "Ibrir", "Mayyu", "Yunyu", "Yulyuz", "Ɣuct", "Cutanbir", "Kṭuber", "Nwanbir", "Dujanbir"},
  56. daysAbbreviated: []string{"Asa", "Ayn", "Asn", "Akr", "Akw", "Asm", "Asḍ"},
  57. daysNarrow: []string{"A", "A", "A", "A", "A", "A", "A"},
  58. daysWide: []string{"Asamas", "Aynas", "Asinas", "Akras", "Akwas", "Asimwas", "Asiḍyas"},
  59. periodsAbbreviated: []string{"Zdat azal", "Ḍeffir aza"},
  60. periodsWide: []string{"Zdat azal", "Ḍeffir aza"},
  61. erasAbbreviated: []string{"ZƐ", "ḌƐ"},
  62. erasNarrow: []string{"", ""},
  63. erasWide: []string{"Zdat Ɛisa (TAƔ)", "Ḍeffir Ɛisa (TAƔ)"},
  64. timezones: map[string]string{"ARST": "ARST", "CHAST": "CHAST", "WARST": "WARST", "MDT": "MDT", "UYST": "UYST", "MEZ": "MEZ", "ECT": "ECT", "HNT": "HNT", "JDT": "JDT", "CST": "CST", "HKST": "HKST", "WAST": "WAST", "WIT": "WIT", "EAT": "EAT", "LHST": "LHST", "WAT": "WAT", "HAT": "HAT", "GMT": "GMT", "AKST": "AKST", "AEDT": "AEDT", "BT": "BT", "WITA": "WITA", "OEZ": "OEZ", "JST": "JST", "PST": "PST", "EST": "EST", "AEST": "AEST", "HKT": "HKT", "SAST": "SAST", "MESZ": "MESZ", "CLT": "CLT", "ACWST": "ACWST", "AST": "AST", "ADT": "ADT", "TMST": "TMST", "COT": "COT", "WART": "WART", "UYT": "UYT", "CDT": "CDT", "NZST": "NZST", "TMT": "TMT", "GYT": "GYT", "MYT": "MYT", "SRT": "SRT", "AKDT": "AKDT", "SGT": "SGT", "IST": "IST", "ACST": "ACST", "LHDT": "LHDT", "BOT": "BOT", "WEZ": "WEZ", "PDT": "PDT", "VET": "VET", "ACWDT": "ACWDT", "GFT": "GFT", "OESZ": "OESZ", "ChST": "ChST", "HAST": "HAST", "HADT": "HADT", "ART": "ART", "MST": "MST", "AWDT": "AWDT", "CHADT": "CHADT", "CLST": "CLST", "CAT": "CAT", "COST": "COST", "∅∅∅": "∅∅∅", "ACDT": "ACDT", "AWST": "AWST", "NZDT": "NZDT", "WESZ": "WESZ", "EDT": "EDT", "WIB": "WIB"},
  65. }
  66. }
  67. // Locale returns the current translators string locale
  68. func (tzm *tzm) Locale() string {
  69. return tzm.locale
  70. }
  71. // PluralsCardinal returns the list of cardinal plural rules associated with 'tzm'
  72. func (tzm *tzm) PluralsCardinal() []locales.PluralRule {
  73. return tzm.pluralsCardinal
  74. }
  75. // PluralsOrdinal returns the list of ordinal plural rules associated with 'tzm'
  76. func (tzm *tzm) PluralsOrdinal() []locales.PluralRule {
  77. return tzm.pluralsOrdinal
  78. }
  79. // PluralsRange returns the list of range plural rules associated with 'tzm'
  80. func (tzm *tzm) PluralsRange() []locales.PluralRule {
  81. return tzm.pluralsRange
  82. }
  83. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'tzm'
  84. func (tzm *tzm) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  85. n := math.Abs(num)
  86. if (n >= 0 && n <= 1) || (n >= 11 && n <= 99) {
  87. return locales.PluralRuleOne
  88. }
  89. return locales.PluralRuleOther
  90. }
  91. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'tzm'
  92. func (tzm *tzm) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  93. return locales.PluralRuleUnknown
  94. }
  95. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'tzm'
  96. func (tzm *tzm) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  97. return locales.PluralRuleUnknown
  98. }
  99. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  100. func (tzm *tzm) MonthAbbreviated(month time.Month) string {
  101. return tzm.monthsAbbreviated[month]
  102. }
  103. // MonthsAbbreviated returns the locales abbreviated months
  104. func (tzm *tzm) MonthsAbbreviated() []string {
  105. return tzm.monthsAbbreviated[1:]
  106. }
  107. // MonthNarrow returns the locales narrow month given the 'month' provided
  108. func (tzm *tzm) MonthNarrow(month time.Month) string {
  109. return tzm.monthsNarrow[month]
  110. }
  111. // MonthsNarrow returns the locales narrow months
  112. func (tzm *tzm) MonthsNarrow() []string {
  113. return tzm.monthsNarrow[1:]
  114. }
  115. // MonthWide returns the locales wide month given the 'month' provided
  116. func (tzm *tzm) MonthWide(month time.Month) string {
  117. return tzm.monthsWide[month]
  118. }
  119. // MonthsWide returns the locales wide months
  120. func (tzm *tzm) MonthsWide() []string {
  121. return tzm.monthsWide[1:]
  122. }
  123. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  124. func (tzm *tzm) WeekdayAbbreviated(weekday time.Weekday) string {
  125. return tzm.daysAbbreviated[weekday]
  126. }
  127. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  128. func (tzm *tzm) WeekdaysAbbreviated() []string {
  129. return tzm.daysAbbreviated
  130. }
  131. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  132. func (tzm *tzm) WeekdayNarrow(weekday time.Weekday) string {
  133. return tzm.daysNarrow[weekday]
  134. }
  135. // WeekdaysNarrow returns the locales narrow weekdays
  136. func (tzm *tzm) WeekdaysNarrow() []string {
  137. return tzm.daysNarrow
  138. }
  139. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  140. func (tzm *tzm) WeekdayShort(weekday time.Weekday) string {
  141. return tzm.daysShort[weekday]
  142. }
  143. // WeekdaysShort returns the locales short weekdays
  144. func (tzm *tzm) WeekdaysShort() []string {
  145. return tzm.daysShort
  146. }
  147. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  148. func (tzm *tzm) WeekdayWide(weekday time.Weekday) string {
  149. return tzm.daysWide[weekday]
  150. }
  151. // WeekdaysWide returns the locales wide weekdays
  152. func (tzm *tzm) WeekdaysWide() []string {
  153. return tzm.daysWide
  154. }
  155. // FmtNumber returns 'num' with digits/precision of 'v' for 'tzm' and handles both Whole and Real numbers based on 'v'
  156. func (tzm *tzm) FmtNumber(num float64, v uint64) string {
  157. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  158. }
  159. // FmtPercent returns 'num' with digits/precision of 'v' for 'tzm' and handles both Whole and Real numbers based on 'v'
  160. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  161. func (tzm *tzm) FmtPercent(num float64, v uint64) string {
  162. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  163. }
  164. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'tzm'
  165. func (tzm *tzm) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  166. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  167. symbol := tzm.currencies[currency]
  168. l := len(s) + len(symbol) + 3 + 2*len(s[:len(s)-int(v)-1])/3
  169. count := 0
  170. inWhole := v == 0
  171. b := make([]byte, 0, l)
  172. for i := len(s) - 1; i >= 0; i-- {
  173. if s[i] == '.' {
  174. b = append(b, tzm.decimal[0])
  175. inWhole = true
  176. continue
  177. }
  178. if inWhole {
  179. if count == 3 {
  180. for j := len(tzm.group) - 1; j >= 0; j-- {
  181. b = append(b, tzm.group[j])
  182. }
  183. count = 1
  184. } else {
  185. count++
  186. }
  187. }
  188. b = append(b, s[i])
  189. }
  190. if num < 0 {
  191. b = append(b, tzm.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, tzm.decimal...)
  200. }
  201. for i := 0; i < 2-int(v); i++ {
  202. b = append(b, '0')
  203. }
  204. }
  205. b = append(b, tzm.currencyPositiveSuffix...)
  206. b = append(b, symbol...)
  207. return string(b)
  208. }
  209. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'tzm'
  210. // in accounting notation.
  211. func (tzm *tzm) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  212. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  213. symbol := tzm.currencies[currency]
  214. l := len(s) + len(symbol) + 3 + 2*len(s[:len(s)-int(v)-1])/3
  215. count := 0
  216. inWhole := v == 0
  217. b := make([]byte, 0, l)
  218. for i := len(s) - 1; i >= 0; i-- {
  219. if s[i] == '.' {
  220. b = append(b, tzm.decimal[0])
  221. inWhole = true
  222. continue
  223. }
  224. if inWhole {
  225. if count == 3 {
  226. for j := len(tzm.group) - 1; j >= 0; j-- {
  227. b = append(b, tzm.group[j])
  228. }
  229. count = 1
  230. } else {
  231. count++
  232. }
  233. }
  234. b = append(b, s[i])
  235. }
  236. if num < 0 {
  237. b = append(b, tzm.minus[0])
  238. }
  239. // reverse
  240. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  241. b[i], b[j] = b[j], b[i]
  242. }
  243. if int(v) < 2 {
  244. if v == 0 {
  245. b = append(b, tzm.decimal...)
  246. }
  247. for i := 0; i < 2-int(v); i++ {
  248. b = append(b, '0')
  249. }
  250. }
  251. if num < 0 {
  252. b = append(b, tzm.currencyNegativeSuffix...)
  253. b = append(b, symbol...)
  254. } else {
  255. b = append(b, tzm.currencyPositiveSuffix...)
  256. b = append(b, symbol...)
  257. }
  258. return string(b)
  259. }
  260. // FmtDateShort returns the short date representation of 't' for 'tzm'
  261. func (tzm *tzm) FmtDateShort(t time.Time) string {
  262. b := make([]byte, 0, 32)
  263. if t.Day() < 10 {
  264. b = append(b, '0')
  265. }
  266. b = strconv.AppendInt(b, int64(t.Day()), 10)
  267. b = append(b, []byte{0x2f}...)
  268. if t.Month() < 10 {
  269. b = append(b, '0')
  270. }
  271. b = strconv.AppendInt(b, int64(t.Month()), 10)
  272. b = append(b, []byte{0x2f}...)
  273. b = strconv.AppendInt(b, int64(t.Year()), 10)
  274. return string(b)
  275. }
  276. // FmtDateMedium returns the medium date representation of 't' for 'tzm'
  277. func (tzm *tzm) FmtDateMedium(t time.Time) string {
  278. b := make([]byte, 0, 32)
  279. b = strconv.AppendInt(b, int64(t.Day()), 10)
  280. b = append(b, []byte{0x20}...)
  281. b = append(b, tzm.monthsAbbreviated[t.Month()]...)
  282. b = append(b, []byte{0x20}...)
  283. b = strconv.AppendInt(b, int64(t.Year()), 10)
  284. return string(b)
  285. }
  286. // FmtDateLong returns the long date representation of 't' for 'tzm'
  287. func (tzm *tzm) FmtDateLong(t time.Time) string {
  288. b := make([]byte, 0, 32)
  289. b = strconv.AppendInt(b, int64(t.Day()), 10)
  290. b = append(b, []byte{0x20}...)
  291. b = append(b, tzm.monthsWide[t.Month()]...)
  292. b = append(b, []byte{0x20}...)
  293. b = strconv.AppendInt(b, int64(t.Year()), 10)
  294. return string(b)
  295. }
  296. // FmtDateFull returns the full date representation of 't' for 'tzm'
  297. func (tzm *tzm) FmtDateFull(t time.Time) string {
  298. b := make([]byte, 0, 32)
  299. b = append(b, tzm.daysWide[t.Weekday()]...)
  300. b = append(b, []byte{0x2c, 0x20}...)
  301. b = strconv.AppendInt(b, int64(t.Day()), 10)
  302. b = append(b, []byte{0x20}...)
  303. b = append(b, tzm.monthsWide[t.Month()]...)
  304. b = append(b, []byte{0x20}...)
  305. b = strconv.AppendInt(b, int64(t.Year()), 10)
  306. return string(b)
  307. }
  308. // FmtTimeShort returns the short time representation of 't' for 'tzm'
  309. func (tzm *tzm) FmtTimeShort(t time.Time) string {
  310. b := make([]byte, 0, 32)
  311. return string(b)
  312. }
  313. // FmtTimeMedium returns the medium time representation of 't' for 'tzm'
  314. func (tzm *tzm) FmtTimeMedium(t time.Time) string {
  315. b := make([]byte, 0, 32)
  316. return string(b)
  317. }
  318. // FmtTimeLong returns the long time representation of 't' for 'tzm'
  319. func (tzm *tzm) FmtTimeLong(t time.Time) string {
  320. b := make([]byte, 0, 32)
  321. return string(b)
  322. }
  323. // FmtTimeFull returns the full time representation of 't' for 'tzm'
  324. func (tzm *tzm) FmtTimeFull(t time.Time) string {
  325. b := make([]byte, 0, 32)
  326. return string(b)
  327. }