1package rn 2 3import ( 4 "math" 5 "strconv" 6 "time" 7 8 "github.com/gohugoio/locales" 9 "github.com/gohugoio/locales/currency" 10) 11 12type rn 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 percentSuffix string 22 perMille string 23 timeSeparator string 24 inifinity string 25 currencies []string // idx = enum of currency code 26 monthsAbbreviated []string 27 monthsNarrow []string 28 monthsWide []string 29 daysAbbreviated []string 30 daysNarrow []string 31 daysShort []string 32 daysWide []string 33 periodsAbbreviated []string 34 periodsNarrow []string 35 periodsShort []string 36 periodsWide []string 37 erasAbbreviated []string 38 erasNarrow []string 39 erasWide []string 40 timezones map[string]string 41} 42 43// New returns a new instance of translator for the 'rn' locale 44func New() locales.Translator { 45 return &rn{ 46 locale: "rn", 47 pluralsCardinal: nil, 48 pluralsOrdinal: nil, 49 pluralsRange: nil, 50 decimal: ",", 51 group: ".", 52 timeSeparator: ":", 53 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", "FBu", "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", "MRU", "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", "UYW", "UZS", "VEB", "VEF", "VES", "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"}, 54 percentSuffix: " ", 55 monthsAbbreviated: []string{"", "Mut.", "Gas.", "Wer.", "Mat.", "Gic.", "Kam.", "Nya.", "Kan.", "Nze.", "Ukw.", "Ugu.", "Uku."}, 56 monthsWide: []string{"", "Nzero", "Ruhuhuma", "Ntwarante", "Ndamukiza", "Rusama", "Ruheshi", "Mukakaro", "Nyandagaro", "Nyakanga", "Gitugutu", "Munyonyo", "Kigarama"}, 57 daysAbbreviated: []string{"cu.", "mbe.", "kab.", "gtu.", "kan.", "gnu.", "gnd."}, 58 daysWide: []string{"Ku w’indwi", "Ku wa mbere", "Ku wa kabiri", "Ku wa gatatu", "Ku wa kane", "Ku wa gatanu", "Ku wa gatandatu"}, 59 periodsAbbreviated: []string{"Z.MU.", "Z.MW."}, 60 periodsWide: []string{"Z.MU.", "Z.MW."}, 61 erasAbbreviated: []string{"Mb.Y.", "Ny.Y"}, 62 erasNarrow: []string{"", ""}, 63 erasWide: []string{"Mbere ya Yezu", "Nyuma ya Yezu"}, 64 timezones: map[string]string{"ACDT": "ACDT", "ACST": "ACST", "ACWDT": "ACWDT", "ACWST": "ACWST", "ADT": "ADT", "AEDT": "AEDT", "AEST": "AEST", "AKDT": "AKDT", "AKST": "AKST", "ARST": "ARST", "ART": "ART", "AST": "AST", "AWDT": "AWDT", "AWST": "AWST", "BOT": "BOT", "BT": "BT", "CAT": "CAT", "CDT": "CDT", "CHADT": "CHADT", "CHAST": "CHAST", "CLST": "CLST", "CLT": "CLT", "COST": "COST", "COT": "COT", "CST": "CST", "ChST": "ChST", "EAT": "EAT", "ECT": "ECT", "EDT": "EDT", "EST": "EST", "GFT": "GFT", "GMT": "GMT", "GST": "GST", "GYT": "GYT", "HADT": "HADT", "HAST": "HAST", "HAT": "HAT", "HECU": "HECU", "HEEG": "HEEG", "HENOMX": "HENOMX", "HEOG": "HEOG", "HEPM": "HEPM", "HEPMX": "HEPMX", "HKST": "HKST", "HKT": "HKT", "HNCU": "HNCU", "HNEG": "HNEG", "HNNOMX": "HNNOMX", "HNOG": "HNOG", "HNPM": "HNPM", "HNPMX": "HNPMX", "HNT": "HNT", "IST": "IST", "JDT": "JDT", "JST": "JST", "LHDT": "LHDT", "LHST": "LHST", "MDT": "MDT", "MESZ": "MESZ", "MEZ": "MEZ", "MST": "MST", "MYT": "MYT", "NZDT": "NZDT", "NZST": "NZST", "OESZ": "OESZ", "OEZ": "OEZ", "PDT": "PDT", "PST": "PST", "SAST": "SAST", "SGT": "SGT", "SRT": "SRT", "TMST": "TMST", "TMT": "TMT", "UYST": "UYST", "UYT": "UYT", "VET": "VET", "WARST": "WARST", "WART": "WART", "WAST": "WAST", "WAT": "WAT", "WESZ": "WESZ", "WEZ": "WEZ", "WIB": "WIB", "WIT": "WIT", "WITA": "WITA", "∅∅∅": "∅∅∅"}, 65 } 66} 67 68// Locale returns the current translators string locale 69func (rn *rn) Locale() string { 70 return rn.locale 71} 72 73// PluralsCardinal returns the list of cardinal plural rules associated with 'rn' 74func (rn *rn) PluralsCardinal() []locales.PluralRule { 75 return rn.pluralsCardinal 76} 77 78// PluralsOrdinal returns the list of ordinal plural rules associated with 'rn' 79func (rn *rn) PluralsOrdinal() []locales.PluralRule { 80 return rn.pluralsOrdinal 81} 82 83// PluralsRange returns the list of range plural rules associated with 'rn' 84func (rn *rn) PluralsRange() []locales.PluralRule { 85 return rn.pluralsRange 86} 87 88// CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'rn' 89func (rn *rn) CardinalPluralRule(num float64, v uint64) locales.PluralRule { 90 return locales.PluralRuleUnknown 91} 92 93// OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'rn' 94func (rn *rn) OrdinalPluralRule(num float64, v uint64) locales.PluralRule { 95 return locales.PluralRuleUnknown 96} 97 98// RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'rn' 99func (rn *rn) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule { 100 return locales.PluralRuleUnknown 101} 102 103// MonthAbbreviated returns the locales abbreviated month given the 'month' provided 104func (rn *rn) MonthAbbreviated(month time.Month) string { 105 return rn.monthsAbbreviated[month] 106} 107 108// MonthsAbbreviated returns the locales abbreviated months 109func (rn *rn) MonthsAbbreviated() []string { 110 return rn.monthsAbbreviated[1:] 111} 112 113// MonthNarrow returns the locales narrow month given the 'month' provided 114func (rn *rn) MonthNarrow(month time.Month) string { 115 return rn.monthsNarrow[month] 116} 117 118// MonthsNarrow returns the locales narrow months 119func (rn *rn) MonthsNarrow() []string { 120 return nil 121} 122 123// MonthWide returns the locales wide month given the 'month' provided 124func (rn *rn) MonthWide(month time.Month) string { 125 return rn.monthsWide[month] 126} 127 128// MonthsWide returns the locales wide months 129func (rn *rn) MonthsWide() []string { 130 return rn.monthsWide[1:] 131} 132 133// WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided 134func (rn *rn) WeekdayAbbreviated(weekday time.Weekday) string { 135 return rn.daysAbbreviated[weekday] 136} 137 138// WeekdaysAbbreviated returns the locales abbreviated weekdays 139func (rn *rn) WeekdaysAbbreviated() []string { 140 return rn.daysAbbreviated 141} 142 143// WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided 144func (rn *rn) WeekdayNarrow(weekday time.Weekday) string { 145 return rn.daysNarrow[weekday] 146} 147 148// WeekdaysNarrow returns the locales narrow weekdays 149func (rn *rn) WeekdaysNarrow() []string { 150 return rn.daysNarrow 151} 152 153// WeekdayShort returns the locales short weekday given the 'weekday' provided 154func (rn *rn) WeekdayShort(weekday time.Weekday) string { 155 return rn.daysShort[weekday] 156} 157 158// WeekdaysShort returns the locales short weekdays 159func (rn *rn) WeekdaysShort() []string { 160 return rn.daysShort 161} 162 163// WeekdayWide returns the locales wide weekday given the 'weekday' provided 164func (rn *rn) WeekdayWide(weekday time.Weekday) string { 165 return rn.daysWide[weekday] 166} 167 168// WeekdaysWide returns the locales wide weekdays 169func (rn *rn) WeekdaysWide() []string { 170 return rn.daysWide 171} 172 173// Decimal returns the decimal point of number 174func (rn *rn) Decimal() string { 175 return rn.decimal 176} 177 178// Group returns the group of number 179func (rn *rn) Group() string { 180 return rn.group 181} 182 183// Group returns the minus sign of number 184func (rn *rn) Minus() string { 185 return rn.minus 186} 187 188// FmtNumber returns 'num' with digits/precision of 'v' for 'rn' and handles both Whole and Real numbers based on 'v' 189func (rn *rn) FmtNumber(num float64, v uint64) string { 190 191 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) 192 l := len(s) + 1 + 1*len(s[:len(s)-int(v)-1])/3 193 count := 0 194 inWhole := v == 0 195 b := make([]byte, 0, l) 196 197 for i := len(s) - 1; i >= 0; i-- { 198 199 if s[i] == '.' { 200 b = append(b, rn.decimal[0]) 201 inWhole = true 202 continue 203 } 204 205 if inWhole { 206 if count == 3 { 207 b = append(b, rn.group[0]) 208 count = 1 209 } else { 210 count++ 211 } 212 } 213 214 b = append(b, s[i]) 215 } 216 217 if num < 0 { 218 b = append(b, rn.minus[0]) 219 } 220 221 // reverse 222 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 { 223 b[i], b[j] = b[j], b[i] 224 } 225 226 return string(b) 227} 228 229// FmtPercent returns 'num' with digits/precision of 'v' for 'rn' and handles both Whole and Real numbers based on 'v' 230// NOTE: 'num' passed into FmtPercent is assumed to be in percent already 231func (rn *rn) FmtPercent(num float64, v uint64) string { 232 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) 233 l := len(s) + 3 234 b := make([]byte, 0, l) 235 236 for i := len(s) - 1; i >= 0; i-- { 237 238 if s[i] == '.' { 239 b = append(b, rn.decimal[0]) 240 continue 241 } 242 243 b = append(b, s[i]) 244 } 245 246 if num < 0 { 247 b = append(b, rn.minus[0]) 248 } 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 255 b = append(b, rn.percentSuffix...) 256 257 b = append(b, rn.percent...) 258 259 return string(b) 260} 261 262// FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'rn' 263func (rn *rn) FmtCurrency(num float64, v uint64, currency currency.Type) string { 264 265 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) 266 symbol := rn.currencies[currency] 267 l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3 268 count := 0 269 inWhole := v == 0 270 b := make([]byte, 0, l) 271 272 for i := len(s) - 1; i >= 0; i-- { 273 274 if s[i] == '.' { 275 b = append(b, rn.decimal[0]) 276 inWhole = true 277 continue 278 } 279 280 if inWhole { 281 if count == 3 { 282 b = append(b, rn.group[0]) 283 count = 1 284 } else { 285 count++ 286 } 287 } 288 289 b = append(b, s[i]) 290 } 291 292 if num < 0 { 293 b = append(b, rn.minus[0]) 294 } 295 296 // reverse 297 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 { 298 b[i], b[j] = b[j], b[i] 299 } 300 301 if int(v) < 2 { 302 303 if v == 0 { 304 b = append(b, rn.decimal...) 305 } 306 307 for i := 0; i < 2-int(v); i++ { 308 b = append(b, '0') 309 } 310 } 311 312 b = append(b, symbol...) 313 314 return string(b) 315} 316 317// FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'rn' 318// in accounting notation. 319func (rn *rn) FmtAccounting(num float64, v uint64, currency currency.Type) string { 320 321 s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) 322 symbol := rn.currencies[currency] 323 l := len(s) + len(symbol) + 1 + 1*len(s[:len(s)-int(v)-1])/3 324 count := 0 325 inWhole := v == 0 326 b := make([]byte, 0, l) 327 328 for i := len(s) - 1; i >= 0; i-- { 329 330 if s[i] == '.' { 331 b = append(b, rn.decimal[0]) 332 inWhole = true 333 continue 334 } 335 336 if inWhole { 337 if count == 3 { 338 b = append(b, rn.group[0]) 339 count = 1 340 } else { 341 count++ 342 } 343 } 344 345 b = append(b, s[i]) 346 } 347 348 if num < 0 { 349 350 b = append(b, rn.minus[0]) 351 352 } 353 354 // reverse 355 for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 { 356 b[i], b[j] = b[j], b[i] 357 } 358 359 if int(v) < 2 { 360 361 if v == 0 { 362 b = append(b, rn.decimal...) 363 } 364 365 for i := 0; i < 2-int(v); i++ { 366 b = append(b, '0') 367 } 368 } 369 370 if num < 0 { 371 b = append(b, symbol...) 372 } else { 373 374 b = append(b, symbol...) 375 } 376 377 return string(b) 378} 379 380// FmtDateShort returns the short date representation of 't' for 'rn' 381func (rn *rn) FmtDateShort(t time.Time) string { 382 383 b := make([]byte, 0, 32) 384 385 b = strconv.AppendInt(b, int64(t.Day()), 10) 386 b = append(b, []byte{0x2f}...) 387 b = strconv.AppendInt(b, int64(t.Month()), 10) 388 b = append(b, []byte{0x2f}...) 389 390 if t.Year() > 0 { 391 b = strconv.AppendInt(b, int64(t.Year()), 10) 392 } else { 393 b = strconv.AppendInt(b, int64(-t.Year()), 10) 394 } 395 396 return string(b) 397} 398 399// FmtDateMedium returns the medium date representation of 't' for 'rn' 400func (rn *rn) FmtDateMedium(t time.Time) string { 401 402 b := make([]byte, 0, 32) 403 404 b = strconv.AppendInt(b, int64(t.Day()), 10) 405 b = append(b, []byte{0x20}...) 406 b = append(b, rn.monthsAbbreviated[t.Month()]...) 407 b = append(b, []byte{0x20}...) 408 409 if t.Year() > 0 { 410 b = strconv.AppendInt(b, int64(t.Year()), 10) 411 } else { 412 b = strconv.AppendInt(b, int64(-t.Year()), 10) 413 } 414 415 return string(b) 416} 417 418// FmtDateLong returns the long date representation of 't' for 'rn' 419func (rn *rn) FmtDateLong(t time.Time) string { 420 421 b := make([]byte, 0, 32) 422 423 b = strconv.AppendInt(b, int64(t.Day()), 10) 424 b = append(b, []byte{0x20}...) 425 b = append(b, rn.monthsWide[t.Month()]...) 426 b = append(b, []byte{0x20}...) 427 428 if t.Year() > 0 { 429 b = strconv.AppendInt(b, int64(t.Year()), 10) 430 } else { 431 b = strconv.AppendInt(b, int64(-t.Year()), 10) 432 } 433 434 return string(b) 435} 436 437// FmtDateFull returns the full date representation of 't' for 'rn' 438func (rn *rn) FmtDateFull(t time.Time) string { 439 440 b := make([]byte, 0, 32) 441 442 b = append(b, rn.daysWide[t.Weekday()]...) 443 b = append(b, []byte{0x20}...) 444 b = strconv.AppendInt(b, int64(t.Day()), 10) 445 b = append(b, []byte{0x20}...) 446 b = append(b, rn.monthsWide[t.Month()]...) 447 b = append(b, []byte{0x20}...) 448 449 if t.Year() > 0 { 450 b = strconv.AppendInt(b, int64(t.Year()), 10) 451 } else { 452 b = strconv.AppendInt(b, int64(-t.Year()), 10) 453 } 454 455 return string(b) 456} 457 458// FmtTimeShort returns the short time representation of 't' for 'rn' 459func (rn *rn) FmtTimeShort(t time.Time) string { 460 461 b := make([]byte, 0, 32) 462 463 if t.Hour() < 10 { 464 b = append(b, '0') 465 } 466 467 b = strconv.AppendInt(b, int64(t.Hour()), 10) 468 b = append(b, rn.timeSeparator...) 469 470 if t.Minute() < 10 { 471 b = append(b, '0') 472 } 473 474 b = strconv.AppendInt(b, int64(t.Minute()), 10) 475 476 return string(b) 477} 478 479// FmtTimeMedium returns the medium time representation of 't' for 'rn' 480func (rn *rn) FmtTimeMedium(t time.Time) string { 481 482 b := make([]byte, 0, 32) 483 484 if t.Hour() < 10 { 485 b = append(b, '0') 486 } 487 488 b = strconv.AppendInt(b, int64(t.Hour()), 10) 489 b = append(b, rn.timeSeparator...) 490 491 if t.Minute() < 10 { 492 b = append(b, '0') 493 } 494 495 b = strconv.AppendInt(b, int64(t.Minute()), 10) 496 b = append(b, rn.timeSeparator...) 497 498 if t.Second() < 10 { 499 b = append(b, '0') 500 } 501 502 b = strconv.AppendInt(b, int64(t.Second()), 10) 503 504 return string(b) 505} 506 507// FmtTimeLong returns the long time representation of 't' for 'rn' 508func (rn *rn) FmtTimeLong(t time.Time) string { 509 510 b := make([]byte, 0, 32) 511 512 if t.Hour() < 10 { 513 b = append(b, '0') 514 } 515 516 b = strconv.AppendInt(b, int64(t.Hour()), 10) 517 b = append(b, rn.timeSeparator...) 518 519 if t.Minute() < 10 { 520 b = append(b, '0') 521 } 522 523 b = strconv.AppendInt(b, int64(t.Minute()), 10) 524 b = append(b, rn.timeSeparator...) 525 526 if t.Second() < 10 { 527 b = append(b, '0') 528 } 529 530 b = strconv.AppendInt(b, int64(t.Second()), 10) 531 b = append(b, []byte{0x20}...) 532 533 tz, _ := t.Zone() 534 b = append(b, tz...) 535 536 return string(b) 537} 538 539// FmtTimeFull returns the full time representation of 't' for 'rn' 540func (rn *rn) FmtTimeFull(t time.Time) string { 541 542 b := make([]byte, 0, 32) 543 544 if t.Hour() < 10 { 545 b = append(b, '0') 546 } 547 548 b = strconv.AppendInt(b, int64(t.Hour()), 10) 549 b = append(b, rn.timeSeparator...) 550 551 if t.Minute() < 10 { 552 b = append(b, '0') 553 } 554 555 b = strconv.AppendInt(b, int64(t.Minute()), 10) 556 b = append(b, rn.timeSeparator...) 557 558 if t.Second() < 10 { 559 b = append(b, '0') 560 } 561 562 b = strconv.AppendInt(b, int64(t.Second()), 10) 563 b = append(b, []byte{0x20}...) 564 565 tz, _ := t.Zone() 566 567 if btz, ok := rn.timezones[tz]; ok { 568 b = append(b, btz...) 569 } else { 570 b = append(b, tz...) 571 } 572 573 return string(b) 574} 575