...

Source file src/github.com/go-playground/locales/root/root.go

Documentation: github.com/go-playground/locales/root

     1  package root
     2  
     3  import (
     4  	"math"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/go-playground/locales"
     9  	"github.com/go-playground/locales/currency"
    10  )
    11  
    12  type root 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 'root' locale
    43  func New() locales.Translator {
    44  	return &root{
    45  		locale:             "root",
    46  		pluralsCardinal:    []locales.PluralRule{6},
    47  		pluralsOrdinal:     []locales.PluralRule{6},
    48  		pluralsRange:       nil,
    49  		decimal:            ".",
    50  		group:              ",",
    51  		minus:              "-",
    52  		percent:            "%",
    53  		perMille:           "‰",
    54  		timeSeparator:      ":",
    55  		inifinity:          "∞",
    56  		currencies:         []string{"ADP", "AED", "AFA", "AFN", "ALK", "ALL", "AMD", "ANG", "Kz", "AOK", "AON", "AOR", "ARA", "ARL", "ARM", "ARP", "$", "ATS", "A$", "AWG", "AZM", "AZN", "BAD", "KM", "BAN", "$", "৳", "BEC", "BEF", "BEL", "BGL", "BGM", "BGN", "BGO", "BHD", "BIF", "$", "$", "Bs", "BOL", "BOP", "BOV", "BRB", "BRC", "BRE", "R$", "BRN", "BRR", "BRZ", "$", "BTN", "BUK", "P", "BYB", "р.", "BYR", "$", "CA$", "CDF", "CHE", "CHF", "CHW", "CLE", "CLF", "$", "CNH", "CNX", "CN¥", "$", "COU", "₡", "CSD", "CSK", "$", "$", "CVE", "CYP", "Kč", "DDM", "DEM", "DJF", "kr", "$", "DZD", "ECS", "ECV", "EEK", "E£", "ERN", "ESA", "ESB", "₧", "ETB", "€", "FIM", "$", "£", "FRF", "£", "GEK", "₾", "GHC", "GHS", "£", "GMD", "FG", "GNS", "GQE", "GRD", "Q", "GWE", "GWP", "$", "HK$", "L", "HRD", "kn", "HTG", "Ft", "Rp", "IEP", "ILP", "ILR", "₪", "₹", "IQD", "IRR", "ISJ", "kr", "ITL", "$", "JOD", "JP¥", "KES", "KGS", "៛", "CF", "₩", "KRH", "KRO", "₩", "KWD", "$", "₸", "₭", "L£", "Rs", "$", "LSL", "Lt", "LTT", "LUC", "LUF", "LUL", "Ls", "LVR", "LYD", "MAD", "MAF", "MCF", "MDC", "MDL", "Ar", "MGF", "MKD", "MKN", "MLF", "K", "₮", "MOP", "MRO", "MRU", "MTL", "MTP", "Rs", "MVP", "MVR", "MWK", "MX$", "MXP", "MXV", "RM", "MZE", "MZM", "MZN", "$", "₦", "NIC", "C$", "NLG", "kr", "Rs", "NZ$", "OMR", "PAB", "PEI", "PEN", "PES", "PGK", "₱", "Rs", "zł", "PLZ", "PTE", "₲", "QAR", "RHD", "ROL", "lei", "RSD", "₽", "р.", "RF", "SAR", "$", "SCR", "SDD", "SDG", "SDP", "kr", "$", "£", "SIT", "SKK", "SLL", "SOS", "$", "SRG", "£", "STD", "Db", "SUR", "SVC", "£", "SZL", "฿", "TJR", "TJS", "TMM", "TMT", "TND", "T$", "TPE", "TRL", "₺", "$", "NT$", "TZS", "₴", "UAK", "UGS", "UGX", "US$", "USN", "USS", "UYI", "UYP", "$", "UYW", "UZS", "VEB", "Bs", "VES", "₫", "VNN", "VUV", "WST", "FCFA", "XAG", "XAU", "XBA", "XBB", "XBC", "XBD", "EC$", "XDR", "XEU", "XFO", "XFU", "CFA", "XPD", "CFPF", "XPT", "XRE", "XSU", "XTS", "XUA", "¤", "YDD", "YER", "YUD", "YUM", "YUN", "YUR", "ZAL", "R", "ZMK", "ZK", "ZRN", "ZRZ", "ZWD", "ZWL", "ZWR"},
    57  		monthsNarrow:       []string{"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
    58  		monthsWide:         []string{"", "M01", "M02", "M03", "M04", "M05", "M06", "M07", "M08", "M09", "M10", "M11", "M12"},
    59  		daysNarrow:         []string{"S", "M", "T", "W", "T", "F", "S"},
    60  		daysWide:           []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},
    61  		periodsAbbreviated: []string{"AM", "PM"},
    62  		periodsNarrow:      []string{"", ""},
    63  		periodsWide:        []string{"", ""},
    64  		erasAbbreviated:    []string{"BCE", "CE"},
    65  		erasNarrow:         []string{"", ""},
    66  		erasWide:           []string{"", ""},
    67  		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", "∅∅∅": "∅∅∅"},
    68  	}
    69  }
    70  
    71  // Locale returns the current translators string locale
    72  func (root *root) Locale() string {
    73  	return root.locale
    74  }
    75  
    76  // PluralsCardinal returns the list of cardinal plural rules associated with 'root'
    77  func (root *root) PluralsCardinal() []locales.PluralRule {
    78  	return root.pluralsCardinal
    79  }
    80  
    81  // PluralsOrdinal returns the list of ordinal plural rules associated with 'root'
    82  func (root *root) PluralsOrdinal() []locales.PluralRule {
    83  	return root.pluralsOrdinal
    84  }
    85  
    86  // PluralsRange returns the list of range plural rules associated with 'root'
    87  func (root *root) PluralsRange() []locales.PluralRule {
    88  	return root.pluralsRange
    89  }
    90  
    91  // CardinalPluralRule returns the cardinal PluralRule given 'num' and digits/precision of 'v' for 'root'
    92  func (root *root) CardinalPluralRule(num float64, v uint64) locales.PluralRule {
    93  	return locales.PluralRuleOther
    94  }
    95  
    96  // OrdinalPluralRule returns the ordinal PluralRule given 'num' and digits/precision of 'v' for 'root'
    97  func (root *root) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
    98  	return locales.PluralRuleOther
    99  }
   100  
   101  // RangePluralRule returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for 'root'
   102  func (root *root) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
   103  	return locales.PluralRuleUnknown
   104  }
   105  
   106  // MonthAbbreviated returns the locales abbreviated month given the 'month' provided
   107  func (root *root) MonthAbbreviated(month time.Month) string {
   108  	return root.monthsAbbreviated[month]
   109  }
   110  
   111  // MonthsAbbreviated returns the locales abbreviated months
   112  func (root *root) MonthsAbbreviated() []string {
   113  	return nil
   114  }
   115  
   116  // MonthNarrow returns the locales narrow month given the 'month' provided
   117  func (root *root) MonthNarrow(month time.Month) string {
   118  	return root.monthsNarrow[month]
   119  }
   120  
   121  // MonthsNarrow returns the locales narrow months
   122  func (root *root) MonthsNarrow() []string {
   123  	return root.monthsNarrow[1:]
   124  }
   125  
   126  // MonthWide returns the locales wide month given the 'month' provided
   127  func (root *root) MonthWide(month time.Month) string {
   128  	return root.monthsWide[month]
   129  }
   130  
   131  // MonthsWide returns the locales wide months
   132  func (root *root) MonthsWide() []string {
   133  	return root.monthsWide[1:]
   134  }
   135  
   136  // WeekdayAbbreviated returns the locales abbreviated weekday given the 'weekday' provided
   137  func (root *root) WeekdayAbbreviated(weekday time.Weekday) string {
   138  	return root.daysAbbreviated[weekday]
   139  }
   140  
   141  // WeekdaysAbbreviated returns the locales abbreviated weekdays
   142  func (root *root) WeekdaysAbbreviated() []string {
   143  	return root.daysAbbreviated
   144  }
   145  
   146  // WeekdayNarrow returns the locales narrow weekday given the 'weekday' provided
   147  func (root *root) WeekdayNarrow(weekday time.Weekday) string {
   148  	return root.daysNarrow[weekday]
   149  }
   150  
   151  // WeekdaysNarrow returns the locales narrow weekdays
   152  func (root *root) WeekdaysNarrow() []string {
   153  	return root.daysNarrow
   154  }
   155  
   156  // WeekdayShort returns the locales short weekday given the 'weekday' provided
   157  func (root *root) WeekdayShort(weekday time.Weekday) string {
   158  	return root.daysShort[weekday]
   159  }
   160  
   161  // WeekdaysShort returns the locales short weekdays
   162  func (root *root) WeekdaysShort() []string {
   163  	return root.daysShort
   164  }
   165  
   166  // WeekdayWide returns the locales wide weekday given the 'weekday' provided
   167  func (root *root) WeekdayWide(weekday time.Weekday) string {
   168  	return root.daysWide[weekday]
   169  }
   170  
   171  // WeekdaysWide returns the locales wide weekdays
   172  func (root *root) WeekdaysWide() []string {
   173  	return root.daysWide
   174  }
   175  
   176  // Decimal returns the decimal point of number
   177  func (root *root) Decimal() string {
   178  	return root.decimal
   179  }
   180  
   181  // Group returns the group of number
   182  func (root *root) Group() string {
   183  	return root.group
   184  }
   185  
   186  // Group returns the minus sign of number
   187  func (root *root) Minus() string {
   188  	return root.minus
   189  }
   190  
   191  // FmtNumber returns 'num' with digits/precision of 'v' for 'root' and handles both Whole and Real numbers based on 'v'
   192  func (root *root) FmtNumber(num float64, v uint64) string {
   193  
   194  	return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   195  }
   196  
   197  // FmtPercent returns 'num' with digits/precision of 'v' for 'root' and handles both Whole and Real numbers based on 'v'
   198  // NOTE: 'num' passed into FmtPercent is assumed to be in percent already
   199  func (root *root) FmtPercent(num float64, v uint64) string {
   200  	return strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   201  }
   202  
   203  // FmtCurrency returns the currency representation of 'num' with digits/precision of 'v' for 'root'
   204  func (root *root) FmtCurrency(num float64, v uint64, currency currency.Type) string {
   205  
   206  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   207  	symbol := root.currencies[currency]
   208  	return string(append(append([]byte{}, symbol...), s...))
   209  }
   210  
   211  // FmtAccounting returns the currency representation of 'num' with digits/precision of 'v' for 'root'
   212  // in accounting notation.
   213  func (root *root) FmtAccounting(num float64, v uint64, currency currency.Type) string {
   214  
   215  	s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64)
   216  	symbol := root.currencies[currency]
   217  	return string(append(append([]byte{}, symbol...), s...))
   218  }
   219  
   220  // FmtDateShort returns the short date representation of 't' for 'root'
   221  func (root *root) FmtDateShort(t time.Time) string {
   222  
   223  	b := make([]byte, 0, 32)
   224  
   225  	if t.Year() > 0 {
   226  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   227  	} else {
   228  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   229  	}
   230  
   231  	b = append(b, []byte{0x2d}...)
   232  
   233  	if t.Month() < 10 {
   234  		b = append(b, '0')
   235  	}
   236  
   237  	b = strconv.AppendInt(b, int64(t.Month()), 10)
   238  
   239  	b = append(b, []byte{0x2d}...)
   240  
   241  	if t.Day() < 10 {
   242  		b = append(b, '0')
   243  	}
   244  
   245  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   246  
   247  	return string(b)
   248  }
   249  
   250  // FmtDateMedium returns the medium date representation of 't' for 'root'
   251  func (root *root) FmtDateMedium(t time.Time) string {
   252  
   253  	b := make([]byte, 0, 32)
   254  
   255  	if t.Year() > 0 {
   256  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   257  	} else {
   258  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   259  	}
   260  
   261  	b = append(b, []byte{0x20}...)
   262  	b = append(b, root.monthsAbbreviated[t.Month()]...)
   263  	b = append(b, []byte{0x20}...)
   264  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   265  
   266  	return string(b)
   267  }
   268  
   269  // FmtDateLong returns the long date representation of 't' for 'root'
   270  func (root *root) FmtDateLong(t time.Time) string {
   271  
   272  	b := make([]byte, 0, 32)
   273  
   274  	if t.Year() > 0 {
   275  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   276  	} else {
   277  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   278  	}
   279  
   280  	b = append(b, []byte{0x20}...)
   281  	b = append(b, root.monthsWide[t.Month()]...)
   282  	b = append(b, []byte{0x20}...)
   283  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   284  
   285  	return string(b)
   286  }
   287  
   288  // FmtDateFull returns the full date representation of 't' for 'root'
   289  func (root *root) FmtDateFull(t time.Time) string {
   290  
   291  	b := make([]byte, 0, 32)
   292  
   293  	if t.Year() > 0 {
   294  		b = strconv.AppendInt(b, int64(t.Year()), 10)
   295  	} else {
   296  		b = strconv.AppendInt(b, int64(-t.Year()), 10)
   297  	}
   298  
   299  	b = append(b, []byte{0x20}...)
   300  	b = append(b, root.monthsWide[t.Month()]...)
   301  	b = append(b, []byte{0x20}...)
   302  	b = strconv.AppendInt(b, int64(t.Day()), 10)
   303  	b = append(b, []byte{0x2c, 0x20}...)
   304  	b = append(b, root.daysWide[t.Weekday()]...)
   305  
   306  	return string(b)
   307  }
   308  
   309  // FmtTimeShort returns the short time representation of 't' for 'root'
   310  func (root *root) FmtTimeShort(t time.Time) string {
   311  
   312  	b := make([]byte, 0, 32)
   313  
   314  	if t.Hour() < 10 {
   315  		b = append(b, '0')
   316  	}
   317  
   318  	b = strconv.AppendInt(b, int64(t.Hour()), 10)
   319  	b = append(b, root.timeSeparator...)
   320  
   321  	if t.Minute() < 10 {
   322  		b = append(b, '0')
   323  	}
   324  
   325  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   326  
   327  	return string(b)
   328  }
   329  
   330  // FmtTimeMedium returns the medium time representation of 't' for 'root'
   331  func (root *root) FmtTimeMedium(t time.Time) string {
   332  
   333  	b := make([]byte, 0, 32)
   334  
   335  	if t.Hour() < 10 {
   336  		b = append(b, '0')
   337  	}
   338  
   339  	b = strconv.AppendInt(b, int64(t.Hour()), 10)
   340  	b = append(b, root.timeSeparator...)
   341  
   342  	if t.Minute() < 10 {
   343  		b = append(b, '0')
   344  	}
   345  
   346  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   347  	b = append(b, root.timeSeparator...)
   348  
   349  	if t.Second() < 10 {
   350  		b = append(b, '0')
   351  	}
   352  
   353  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   354  
   355  	return string(b)
   356  }
   357  
   358  // FmtTimeLong returns the long time representation of 't' for 'root'
   359  func (root *root) FmtTimeLong(t time.Time) string {
   360  
   361  	b := make([]byte, 0, 32)
   362  
   363  	if t.Hour() < 10 {
   364  		b = append(b, '0')
   365  	}
   366  
   367  	b = strconv.AppendInt(b, int64(t.Hour()), 10)
   368  	b = append(b, root.timeSeparator...)
   369  
   370  	if t.Minute() < 10 {
   371  		b = append(b, '0')
   372  	}
   373  
   374  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   375  	b = append(b, root.timeSeparator...)
   376  
   377  	if t.Second() < 10 {
   378  		b = append(b, '0')
   379  	}
   380  
   381  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   382  	b = append(b, []byte{0x20}...)
   383  
   384  	tz, _ := t.Zone()
   385  	b = append(b, tz...)
   386  
   387  	return string(b)
   388  }
   389  
   390  // FmtTimeFull returns the full time representation of 't' for 'root'
   391  func (root *root) FmtTimeFull(t time.Time) string {
   392  
   393  	b := make([]byte, 0, 32)
   394  
   395  	if t.Hour() < 10 {
   396  		b = append(b, '0')
   397  	}
   398  
   399  	b = strconv.AppendInt(b, int64(t.Hour()), 10)
   400  	b = append(b, root.timeSeparator...)
   401  
   402  	if t.Minute() < 10 {
   403  		b = append(b, '0')
   404  	}
   405  
   406  	b = strconv.AppendInt(b, int64(t.Minute()), 10)
   407  	b = append(b, root.timeSeparator...)
   408  
   409  	if t.Second() < 10 {
   410  		b = append(b, '0')
   411  	}
   412  
   413  	b = strconv.AppendInt(b, int64(t.Second()), 10)
   414  	b = append(b, []byte{0x20}...)
   415  
   416  	tz, _ := t.Zone()
   417  
   418  	if btz, ok := root.timezones[tz]; ok {
   419  		b = append(b, btz...)
   420  	} else {
   421  		b = append(b, tz...)
   422  	}
   423  
   424  	return string(b)
   425  }
   426  

View as plain text