* chore(deps): consolidate dependency and security bumps Dependabot PRs applied: - #12878 qs 6.15.2 -> 6.16.0 (web/web-runtime) - #13013 alpine 3.24.1 -> 3.24.2 - #13061 actions/checkout -> v7.0.1 (missed instance in k8s.yml) - #13062 actions/setup-node -> v7.0.0 (missed instance in k8s.yml) - #13075 simple-git 3.36.0 -> 4.0.1 (security) - #13076 katex 0.16.45 -> 0.18.2+ (security) - #13077 dompurify 3.4.13 -> 3.4.16 (security) - #13079 go deps minor-and-patch group, 18 updates (opa 1.19.0 -> 1.21.1, otel 1.46 -> 1.47, bleve, govips, grpc-gateway, lico, tablewriter, logrus, gjson, x/exp, x/image, genproto, etc.) Skipped (already satisfied on master): #12513, #12624, #12625, #12633 Manual security bumps via pnpm.overrides (no dependabot PR existed): - axios -> 1.20.0 - smol-toml -> 1.9.0 - postcss -> 8.5.23 - postcss-selector-parser -> 7.1.6 - colord -> 2.9.4 - unhead -> 2.1.13 - markdown-it -> 14.3.1 - esbuild -> 0.28.1 - brace-expansion 5.0.11 -> 5.0.12 Not fixable: sprintf-js and elliptic have no patched version released upstream yet; left as-is. go build and pnpm install verified clean. * chore(deps): fix residual transitive vulns found by trivy trivy flagged duplicate/stale resolutions that survived the earlier override pass: dompurify (3.4.12 -> 3.4.16), qs (6.14.2 -> 6.16.0), and fast-uri (3.1.7 -> 3.1.8, CVE-2026-86472). Added/bumped pnpm.overrides accordingly. trivy fs scan of web/pnpm-lock.yaml now clean. * chore(deps): bump reva to bac0e1ee144ab853a81c6b02142032f102dd245a Pins github.com/owncloud/reva/v2 to bac0e1ee144ab853a81c6b02142032f102dd245a, pulling in its go-playground/locales and huandu/xstrings version bumps. Appends this PR to changelog/unreleased/bump-reva.md. go build ./... verified clean after go mod vendor resync. * fix(ci): revert unhead override, breaks web build * revert: keep k8s.yml actions/checkout and setup-node pinned
297 lines
8.4 KiB
Go
297 lines
8.4 KiB
Go
package locales
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-playground/locales/currency"
|
|
)
|
|
|
|
// // ErrBadNumberValue is returned when the number passed for
|
|
// // plural rule determination cannot be parsed
|
|
// type ErrBadNumberValue struct {
|
|
// NumberValue string
|
|
// InnerError error
|
|
// }
|
|
|
|
// // Error returns ErrBadNumberValue error string
|
|
// func (e *ErrBadNumberValue) Error() string {
|
|
// return fmt.Sprintf("Invalid Number Value '%s' %s", e.NumberValue, e.InnerError)
|
|
// }
|
|
|
|
// var _ error = new(ErrBadNumberValue)
|
|
|
|
// PluralRule denotes the type of plural rules
|
|
type PluralRule int
|
|
|
|
// PluralRule's
|
|
const (
|
|
PluralRuleUnknown PluralRule = iota
|
|
PluralRuleZero // zero
|
|
PluralRuleOne // one - singular
|
|
PluralRuleTwo // two - dual
|
|
PluralRuleFew // few - paucal
|
|
PluralRuleMany // many - also used for fractions if they have a separate class
|
|
PluralRuleOther // other - required—general plural form—also used if the language only has a single form
|
|
)
|
|
|
|
const (
|
|
pluralsString = "UnknownZeroOneTwoFewManyOther"
|
|
)
|
|
|
|
// Translator encapsulates an instance of a locale
|
|
// NOTE: some values are returned as a []byte just in case the caller
|
|
// wishes to add more and can help avoid allocations; otherwise just cast as string
|
|
type Translator interface {
|
|
|
|
// The following Functions are for overriding, debugging or developing
|
|
// with a Translator Locale
|
|
|
|
// Locale returns the string value of the translator
|
|
Locale() string
|
|
|
|
// returns an array of cardinal plural rules associated
|
|
// with this translator
|
|
PluralsCardinal() []PluralRule
|
|
|
|
// returns an array of ordinal plural rules associated
|
|
// with this translator
|
|
PluralsOrdinal() []PluralRule
|
|
|
|
// returns an array of range plural rules associated
|
|
// with this translator
|
|
PluralsRange() []PluralRule
|
|
|
|
// returns the cardinal PluralRule given 'num' and digits/precision of 'v' for locale
|
|
CardinalPluralRule(num float64, v uint64) PluralRule
|
|
|
|
// returns the ordinal PluralRule given 'num' and digits/precision of 'v' for locale
|
|
OrdinalPluralRule(num float64, v uint64) PluralRule
|
|
|
|
// returns the ordinal PluralRule given 'num1', 'num2' and digits/precision of 'v1' and 'v2' for locale
|
|
RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) PluralRule
|
|
|
|
// returns the locales abbreviated month given the 'month' provided
|
|
MonthAbbreviated(month time.Month) string
|
|
|
|
// returns the locales abbreviated months
|
|
MonthsAbbreviated() []string
|
|
|
|
// returns the locales narrow month given the 'month' provided
|
|
MonthNarrow(month time.Month) string
|
|
|
|
// returns the locales narrow months
|
|
MonthsNarrow() []string
|
|
|
|
// returns the locales wide month given the 'month' provided
|
|
MonthWide(month time.Month) string
|
|
|
|
// returns the locales wide months
|
|
MonthsWide() []string
|
|
|
|
// returns the locales abbreviated weekday given the 'weekday' provided
|
|
WeekdayAbbreviated(weekday time.Weekday) string
|
|
|
|
// returns the locales abbreviated weekdays
|
|
WeekdaysAbbreviated() []string
|
|
|
|
// returns the locales narrow weekday given the 'weekday' provided
|
|
WeekdayNarrow(weekday time.Weekday) string
|
|
|
|
// WeekdaysNarrow returns the locales narrow weekdays
|
|
WeekdaysNarrow() []string
|
|
|
|
// returns the locales short weekday given the 'weekday' provided
|
|
WeekdayShort(weekday time.Weekday) string
|
|
|
|
// returns the locales short weekdays
|
|
WeekdaysShort() []string
|
|
|
|
// returns the locales wide weekday given the 'weekday' provided
|
|
WeekdayWide(weekday time.Weekday) string
|
|
|
|
// returns the locales wide weekdays
|
|
WeekdaysWide() []string
|
|
|
|
// The following Functions are common Formatting functions for the Translator's Locale
|
|
|
|
// returns 'num' with digits/precision of 'v' for locale and handles both Whole and Real numbers based on 'v'
|
|
FmtNumber(num float64, v uint64) string
|
|
|
|
// returns 'num' with digits/precision of 'v' for locale and handles both Whole and Real numbers based on 'v'
|
|
// NOTE: 'num' passed into FmtPercent is assumed to be in percent already
|
|
FmtPercent(num float64, v uint64) string
|
|
|
|
// returns the currency representation of 'num' with digits/precision of 'v' for locale
|
|
FmtCurrency(num float64, v uint64, currency currency.Type) string
|
|
|
|
// returns the currency representation of 'num' with digits/precision of 'v' for locale
|
|
// in accounting notation.
|
|
FmtAccounting(num float64, v uint64, currency currency.Type) string
|
|
|
|
// returns the short date representation of 't' for locale
|
|
FmtDateShort(t time.Time) string
|
|
|
|
// returns the medium date representation of 't' for locale
|
|
FmtDateMedium(t time.Time) string
|
|
|
|
// returns the long date representation of 't' for locale
|
|
FmtDateLong(t time.Time) string
|
|
|
|
// returns the full date representation of 't' for locale
|
|
FmtDateFull(t time.Time) string
|
|
|
|
// returns the short time representation of 't' for locale
|
|
FmtTimeShort(t time.Time) string
|
|
|
|
// returns the medium time representation of 't' for locale
|
|
FmtTimeMedium(t time.Time) string
|
|
|
|
// returns the long time representation of 't' for locale
|
|
FmtTimeLong(t time.Time) string
|
|
|
|
// returns the full time representation of 't' for locale
|
|
FmtTimeFull(t time.Time) string
|
|
}
|
|
|
|
// String returns the string value of PluralRule
|
|
func (p PluralRule) String() string {
|
|
|
|
switch p {
|
|
case PluralRuleZero:
|
|
return pluralsString[7:11]
|
|
case PluralRuleOne:
|
|
return pluralsString[11:14]
|
|
case PluralRuleTwo:
|
|
return pluralsString[14:17]
|
|
case PluralRuleFew:
|
|
return pluralsString[17:20]
|
|
case PluralRuleMany:
|
|
return pluralsString[20:24]
|
|
case PluralRuleOther:
|
|
return pluralsString[24:]
|
|
default:
|
|
return pluralsString[:7]
|
|
}
|
|
}
|
|
|
|
//
|
|
// Precision Notes:
|
|
//
|
|
// must specify a precision >= 0, and here is why https://play.golang.org/p/LyL90U0Vyh
|
|
//
|
|
// v := float64(3.141)
|
|
// i := float64(int64(v))
|
|
//
|
|
// fmt.Println(v - i)
|
|
//
|
|
// or
|
|
//
|
|
// s := strconv.FormatFloat(v-i, 'f', -1, 64)
|
|
// fmt.Println(s)
|
|
//
|
|
// these will not print what you'd expect: 0.14100000000000001
|
|
// and so this library requires a precision to be specified, or
|
|
// inaccurate plural rules could be applied.
|
|
//
|
|
//
|
|
//
|
|
// n - absolute value of the source number (integer and decimals).
|
|
// i - integer digits of n.
|
|
// v - number of visible fraction digits in n, with trailing zeros.
|
|
// w - number of visible fraction digits in n, without trailing zeros.
|
|
// f - visible fractional digits in n, with trailing zeros.
|
|
// t - visible fractional digits in n, without trailing zeros.
|
|
//
|
|
//
|
|
// Func(num float64, v uint64) // v = digits/precision and prevents -1 as a special case as this can lead to very unexpected behaviour, see precision note's above.
|
|
//
|
|
// n := math.Abs(num)
|
|
// i := int64(n)
|
|
// v := v
|
|
//
|
|
//
|
|
// w := strconv.FormatFloat(num-float64(i), 'f', int(v), 64) // then parse backwards on string until no more zero's....
|
|
// f := strconv.FormatFloat(n, 'f', int(v), 64) // then turn everything after decimal into an int64
|
|
// t := strconv.FormatFloat(n, 'f', int(v), 64) // then parse backwards on string until no more zero's....
|
|
//
|
|
//
|
|
//
|
|
// General Inclusion Rules
|
|
// - v will always be available inherently
|
|
// - all require n
|
|
// - w requires i
|
|
//
|
|
|
|
// W returns the number of visible fraction digits in N, without trailing zeros.
|
|
func W(n float64, v uint64) (w int64) {
|
|
|
|
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
|
|
|
|
// will either be '0' or '0.xxxx', so if 1 then w will be zero
|
|
// otherwise need to parse
|
|
if len(s) != 1 {
|
|
|
|
s = s[2:]
|
|
end := 0
|
|
|
|
for i := len(s) - 1; i >= 0; i-- {
|
|
if s[i] != '0' {
|
|
end = i + 1
|
|
break
|
|
}
|
|
}
|
|
|
|
w = int64(len(s[:end]))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// F returns the visible fractional digits in N, with trailing zeros.
|
|
func F(n float64, v uint64) (f int64) {
|
|
|
|
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
|
|
|
|
// will either be '0' or '0.xxxx', so if 1 then f will be zero
|
|
// otherwise need to parse
|
|
if len(s) != 1 {
|
|
|
|
// ignoring error, because it can't fail as we generated
|
|
// the string internally from a real number
|
|
f, _ = strconv.ParseInt(s[2:], 10, 64)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// T returns the visible fractional digits in N, without trailing zeros.
|
|
func T(n float64, v uint64) (t int64) {
|
|
|
|
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
|
|
|
|
// will either be '0' or '0.xxxx', so if 1 then t will be zero
|
|
// otherwise need to parse
|
|
if len(s) != 1 {
|
|
|
|
s = s[2:]
|
|
end := 0
|
|
|
|
for i := len(s) - 1; i >= 0; i-- {
|
|
if s[i] != '0' {
|
|
end = i + 1
|
|
break
|
|
}
|
|
}
|
|
|
|
// ignoring error, because it can't fail as we generated
|
|
// the string internally from a real number
|
|
if end > 0 {
|
|
t, _ = strconv.ParseInt(s[:end], 10, 64)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|