zgh.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package zgh
  2. import (
  3. "math"
  4. "strconv"
  5. "time"
  6. "github.com/go-playground/locales"
  7. "github.com/go-playground/locales/currency"
  8. )
  9. type zgh 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. percentSuffix string
  19. perMille string
  20. timeSeparator string
  21. inifinity string
  22. currencies []string // idx = enum of currency code
  23. monthsAbbreviated []string
  24. monthsNarrow []string
  25. monthsWide []string
  26. daysAbbreviated []string
  27. daysNarrow []string
  28. daysShort []string
  29. daysWide []string
  30. periodsAbbreviated []string
  31. periodsNarrow []string
  32. periodsShort []string
  33. periodsWide []string
  34. erasAbbreviated []string
  35. erasNarrow []string
  36. erasWide []string
  37. timezones map[string]string
  38. }
  39. // New returns a new instance of translator for the 'zgh' locale
  40. func New() locales.Translator {
  41. return &zgh{
  42. locale: "zgh",
  43. pluralsCardinal: nil,
  44. pluralsOrdinal: nil,
  45. pluralsRange: nil,
  46. decimal: ",",
  47. group: " ",
  48. timeSeparator: ":",
  49. 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 "},
  50. percentSuffix: " ",
  51. monthsAbbreviated: []string{"", "ⵉⵏⵏ", "ⴱⵕⴰ", "ⵎⴰⵕ", "ⵉⴱⵔ", "ⵎⴰⵢ", "ⵢⵓⵏ", "ⵢⵓⵍ", "ⵖⵓⵛ", "ⵛⵓⵜ", "ⴽⵜⵓ", "ⵏⵓⵡ", "ⴷⵓⵊ"},
  52. monthsNarrow: []string{"", "ⵉ", "ⴱ", "ⵎ", "ⵉ", "ⵎ", "ⵢ", "ⵢ", "ⵖ", "ⵛ", "ⴽ", "ⵏ", "ⴷ"},
  53. monthsWide: []string{"", "ⵉⵏⵏⴰⵢⵔ", "ⴱⵕⴰⵢⵕ", "ⵎⴰⵕⵚ", "ⵉⴱⵔⵉⵔ", "ⵎⴰⵢⵢⵓ", "ⵢⵓⵏⵢⵓ", "ⵢⵓⵍⵢⵓⵣ", "ⵖⵓⵛⵜ", "ⵛⵓⵜⴰⵏⴱⵉⵔ", "ⴽⵜⵓⴱⵔ", "ⵏⵓⵡⴰⵏⴱⵉⵔ", "ⴷⵓⵊⴰⵏⴱⵉⵔ"},
  54. daysAbbreviated: []string{"ⴰⵙⴰ", "ⴰⵢⵏ", "ⴰⵙⵉ", "ⴰⴽⵕ", "ⴰⴽⵡ", "ⴰⵙⵉⵎ", "ⴰⵙⵉⴹ"},
  55. daysWide: []string{"ⴰⵙⴰⵎⴰⵙ", "ⴰⵢⵏⴰⵙ", "ⴰⵙⵉⵏⴰⵙ", "ⴰⴽⵕⴰⵙ", "ⴰⴽⵡⴰⵙ", "ⴰⵙⵉⵎⵡⴰⵙ", "ⴰⵙⵉⴹⵢⴰⵙ"},
  56. periodsAbbreviated: []string{"ⵜⵉⴼⴰⵡⵜ", "ⵜⴰⴷⴳⴳⵯⴰⵜ"},
  57. periodsWide: []string{"ⵜⵉⴼⴰⵡⵜ", "ⵜⴰⴷⴳⴳⵯⴰⵜ"},
  58. erasAbbreviated: []string{"ⴷⴰⵄ", "ⴷⴼⵄ"},
  59. erasNarrow: []string{"", ""},
  60. erasWide: []string{"ⴷⴰⵜ ⵏ ⵄⵉⵙⴰ", "ⴷⴼⴼⵉⵔ ⵏ ⵄⵉⵙⴰ"},
  61. timezones: map[string]string{"ARST": "ARST", "GMT": "GMT", "CLT": "CLT", "HNT": "HNT", "IST": "IST", "ACST": "ACST", "LHST": "LHST", "CHAST": "CHAST", "PDT": "PDT", "WIB": "WIB", "CST": "CST", "MESZ": "MESZ", "WAT": "WAT", "WITA": "WITA", "TMST": "TMST", "EDT": "EDT", "COT": "COT", "JST": "JST", "HADT": "HADT", "OESZ": "OESZ", "AKDT": "AKDT", "AEDT": "AEDT", "SAST": "SAST", "MDT": "MDT", "ACDT": "ACDT", "MYT": "MYT", "ChST": "ChST", "CLST": "CLST", "VET": "VET", "TMT": "TMT", "EAT": "EAT", "UYT": "UYT", "UYST": "UYST", "WESZ": "WESZ", "GFT": "GFT", "ACWST": "ACWST", "ECT": "ECT", "EST": "EST", "AST": "AST", "WAST": "WAST", "ART": "ART", "∅∅∅": "∅∅∅", "CDT": "CDT", "AWDT": "AWDT", "CHADT": "CHADT", "AEST": "AEST", "GYT": "GYT", "SRT": "SRT", "PST": "PST", "COST": "COST", "LHDT": "LHDT", "AWST": "AWST", "ACWDT": "ACWDT", "HKT": "HKT", "WARST": "WARST", "BT": "BT", "HAT": "HAT", "WIT": "WIT", "MST": "MST", "JDT": "JDT", "HAST": "HAST", "HKST": "HKST", "OEZ": "OEZ", "NZDT": "NZDT", "CAT": "CAT", "SGT": "SGT", "NZST": "NZST", "WEZ": "WEZ", "ADT": "ADT", "WART": "WART", "AKST": "AKST", "BOT": "BOT", "MEZ": "MEZ"},
  62. }
  63. }
  64. // Locale returns the current translators string locale
  65. func (zgh *zgh) Locale() string {
  66. return zgh.locale
  67. }
  68. // PluralsCardinal returns the list of cardinal plural rules associated with 'zgh'
  69. func (zgh *zgh) PluralsCardinal() []locales.PluralRule {
  70. return zgh.pluralsCardinal
  71. }
  72. // PluralsOrdinal returns the list of ordinal plural rules associated with 'zgh'
  73. func (zgh *zgh) PluralsOrdinal() []locales.PluralRule {
  74. return zgh.pluralsOrdinal
  75. }
  76. // PluralsRange returns the list of range plural rules associated with 'zgh'
  77. func (zgh *zgh) PluralsRange() []locales.PluralRule {
  78. return zgh.pluralsRange
  79. }
  80. // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'zgh'
  81. func (zgh *zgh) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
  82. return locales.PluralRuleUnknown
  83. }
  84. // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'zgh'
  85. func (zgh *zgh) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
  86. return locales.PluralRuleUnknown
  87. }
  88. // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'zgh'
  89. func (zgh *zgh) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
  90. return locales.PluralRuleUnknown
  91. }
  92. // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
  93. func (zgh *zgh) MonthAbbreviated(month time.Month) string {
  94. return zgh.monthsAbbreviated[month]
  95. }
  96. // MonthsAbbreviated returns the locales abbreviated months
  97. func (zgh *zgh) MonthsAbbreviated() []string {
  98. return zgh.monthsAbbreviated[1:]
  99. }
  100. // MonthNarrow returns the locales narrow month given the 'month' provided
  101. func (zgh *zgh) MonthNarrow(month time.Month) string {
  102. return zgh.monthsNarrow[month]
  103. }
  104. // MonthsNarrow returns the locales narrow months
  105. func (zgh *zgh) MonthsNarrow() []string {
  106. return zgh.monthsNarrow[1:]
  107. }
  108. // MonthWide returns the locales wide month given the 'month' provided
  109. func (zgh *zgh) MonthWide(month time.Month) string {
  110. return zgh.monthsWide[month]
  111. }
  112. // MonthsWide returns the locales wide months
  113. func (zgh *zgh) MonthsWide() []string {
  114. return zgh.monthsWide[1:]
  115. }
  116. // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
  117. func (zgh *zgh) WeekdayAbbreviated(weekday time.Weekday) string {
  118. return zgh.daysAbbreviated[weekday]
  119. }
  120. // WeekdaysAbbreviated returns the locales abbreviated weekdays
  121. func (zgh *zgh) WeekdaysAbbreviated() []string {
  122. return zgh.daysAbbreviated
  123. }
  124. // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
  125. func (zgh *zgh) WeekdayNarrow(weekday time.Weekday) string {
  126. return zgh.daysNarrow[weekday]
  127. }
  128. // WeekdaysNarrow returns the locales narrow weekdays
  129. func (zgh *zgh) WeekdaysNarrow() []string {
  130. return zgh.daysNarrow
  131. }
  132. // WeekdayShort returns the locales short weekday given the 'weekday' provided
  133. func (zgh *zgh) WeekdayShort(weekday time.Weekday) string {
  134. return zgh.daysShort[weekday]
  135. }
  136. // WeekdaysShort returns the locales short weekdays
  137. func (zgh *zgh) WeekdaysShort() []string {
  138. return zgh.daysShort
  139. }
  140. // WeekdayWide returns the locales wide weekday given the 'weekday' provided
  141. func (zgh *zgh) WeekdayWide(weekday time.Weekday) string {
  142. return zgh.daysWide[weekday]
  143. }
  144. // WeekdaysWide returns the locales wide weekdays
  145. func (zgh *zgh) WeekdaysWide() []string {
  146. return zgh.daysWide
  147. }
  148. // FmtNumber returns 'num' with digits/precision of 'v' for 'zgh' and handles both Whole and Real numbers based on 'v'
  149. func (zgh *zgh) FmtNumber(num float64, v uint64) string {
  150. return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  151. }
  152. // FmtPercent returns 'num' with digits/precision of 'v' for 'zgh' and handles both Whole and Real numbers based on 'v'
  153. // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
  154. func (zgh *zgh) FmtPercent(num float64, v uint64) string {
  155. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  156. l := len(s) + 3
  157. b := make([]byte, 0, l)
  158. for i := len(s) - 1; i >= 0; i-- {
  159. if s[i] == '.' {
  160. b = append(b, zgh.decimal[0])
  161. continue
  162. }
  163. b = append(b, s[i])
  164. }
  165. if num < 0 {
  166. b = append(b, zgh.minus[0])
  167. }
  168. // reverse
  169. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  170. b[i], b[j] = b[j], b[i]
  171. }
  172. b = append(b, zgh.percentSuffix...)
  173. b = append(b, zgh.percent...)
  174. return string(b)
  175. }
  176. // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'zgh'
  177. func (zgh *zgh) FmtCurrency(num float64, v uint64, currency currency.Type) string {
  178. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  179. symbol := zgh.currencies[currency]
  180. l := len(s) + len(symbol) + 1 + 2*len(s[:len(s)-int(v)-1])/3
  181. count := 0
  182. inWhole := v == 0
  183. b := make([]byte, 0, l)
  184. for i := len(s) - 1; i >= 0; i-- {
  185. if s[i] == '.' {
  186. b = append(b, zgh.decimal[0])
  187. inWhole = true
  188. continue
  189. }
  190. if inWhole {
  191. if count == 3 {
  192. for j := len(zgh.group) - 1; j >= 0; j-- {
  193. b = append(b, zgh.group[j])
  194. }
  195. count = 1
  196. } else {
  197. count++
  198. }
  199. }
  200. b = append(b, s[i])
  201. }
  202. if num < 0 {
  203. b = append(b, zgh.minus[0])
  204. }
  205. // reverse
  206. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  207. b[i], b[j] = b[j], b[i]
  208. }
  209. if int(v) < 2 {
  210. if v == 0 {
  211. b = append(b, zgh.decimal...)
  212. }
  213. for i := 0; i < 2-int(v); i++ {
  214. b = append(b, '0')
  215. }
  216. }
  217. b = append(b, symbol...)
  218. return string(b)
  219. }
  220. // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'zgh'
  221. // in accounting notation.
  222. func (zgh *zgh) FmtAccounting(num float64, v uint64, currency currency.Type) string {
  223. s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
  224. symbol := zgh.currencies[currency]
  225. l := len(s) + len(symbol) + 1 + 2*len(s[:len(s)-int(v)-1])/3
  226. count := 0
  227. inWhole := v == 0
  228. b := make([]byte, 0, l)
  229. for i := len(s) - 1; i >= 0; i-- {
  230. if s[i] == '.' {
  231. b = append(b, zgh.decimal[0])
  232. inWhole = true
  233. continue
  234. }
  235. if inWhole {
  236. if count == 3 {
  237. for j := len(zgh.group) - 1; j >= 0; j-- {
  238. b = append(b, zgh.group[j])
  239. }
  240. count = 1
  241. } else {
  242. count++
  243. }
  244. }
  245. b = append(b, s[i])
  246. }
  247. if num < 0 {
  248. b = append(b, zgh.minus[0])
  249. }
  250. // reverse
  251. for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
  252. b[i], b[j] = b[j], b[i]
  253. }
  254. if int(v) < 2 {
  255. if v == 0 {
  256. b = append(b, zgh.decimal...)
  257. }
  258. for i := 0; i < 2-int(v); i++ {
  259. b = append(b, '0')
  260. }
  261. }
  262. if num < 0 {
  263. b = append(b, symbol...)
  264. } else {
  265. b = append(b, symbol...)
  266. }
  267. return string(b)
  268. }
  269. // FmtDateShort returns the short date representation of 't' for 'zgh'
  270. func (zgh *zgh) FmtDateShort(t time.Time) string {
  271. b := make([]byte, 0, 32)
  272. b = strconv.AppendInt(b, int64(t.Day()), 10)
  273. b = append(b, []byte{0x2f}...)
  274. b = strconv.AppendInt(b, int64(t.Month()), 10)
  275. b = append(b, []byte{0x2f}...)
  276. b = strconv.AppendInt(b, int64(t.Year()), 10)
  277. return string(b)
  278. }
  279. // FmtDateMedium returns the medium date representation of 't' for 'zgh'
  280. func (zgh *zgh) FmtDateMedium(t time.Time) string {
  281. b := make([]byte, 0, 32)
  282. b = strconv.AppendInt(b, int64(t.Day()), 10)
  283. b = append(b, []byte{0x20}...)
  284. b = append(b, zgh.monthsAbbreviated[t.Month()]...)
  285. b = append(b, []byte{0x2c, 0x20}...)
  286. b = strconv.AppendInt(b, int64(t.Year()), 10)
  287. return string(b)
  288. }
  289. // FmtDateLong returns the long date representation of 't' for 'zgh'
  290. func (zgh *zgh) 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, zgh.monthsWide[t.Month()]...)
  295. b = append(b, []byte{0x20}...)
  296. b = strconv.AppendInt(b, int64(t.Year()), 10)
  297. return string(b)
  298. }
  299. // FmtDateFull returns the full date representation of 't' for 'zgh'
  300. func (zgh *zgh) FmtDateFull(t time.Time) string {
  301. b := make([]byte, 0, 32)
  302. b = append(b, zgh.daysWide[t.Weekday()]...)
  303. b = append(b, []byte{0x20}...)
  304. b = strconv.AppendInt(b, int64(t.Day()), 10)
  305. b = append(b, []byte{0x20}...)
  306. b = append(b, zgh.monthsWide[t.Month()]...)
  307. b = append(b, []byte{0x20}...)
  308. b = strconv.AppendInt(b, int64(t.Year()), 10)
  309. return string(b)
  310. }
  311. // FmtTimeShort returns the short time representation of 't' for 'zgh'
  312. func (zgh *zgh) FmtTimeShort(t time.Time) string {
  313. b := make([]byte, 0, 32)
  314. return string(b)
  315. }
  316. // FmtTimeMedium returns the medium time representation of 't' for 'zgh'
  317. func (zgh *zgh) FmtTimeMedium(t time.Time) string {
  318. b := make([]byte, 0, 32)
  319. return string(b)
  320. }
  321. // FmtTimeLong returns the long time representation of 't' for 'zgh'
  322. func (zgh *zgh) FmtTimeLong(t time.Time) string {
  323. b := make([]byte, 0, 32)
  324. return string(b)
  325. }
  326. // FmtTimeFull returns the full time representation of 't' for 'zgh'
  327. func (zgh *zgh) FmtTimeFull(t time.Time) string {
  328. b := make([]byte, 0, 32)
  329. return string(b)
  330. }