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