MSP-8299 vendor update

This commit is contained in:
ziminis committed 2026-11-27 20:10:32 +07:00
1 parent c8e4a014a0
commit 59d487646c
607 files changed
+48706 -13176

No files matched your search

+7 -5
View File
@@ -139,11 +139,13 @@ func main() {
fmt.Println(l.CardinalPluralRule(1.0, 1))
fmt.Println(l.CardinalPluralRule(3, 0))
// Ordinal Plural Rules
fmt.Println(l.OrdinalPluralRule(21, 0)) // 21st
fmt.Println(l.OrdinalPluralRule(22, 0)) // 22nd
fmt.Println(l.OrdinalPluralRule(33, 0)) // 33rd
fmt.Println(l.OrdinalPluralRule(34, 0)) // 34th
// Ordinal Plural Rules. The method returns the locales/plurals.Rule
// (e.g. One, Two, Few, Other) for the number; converting that rule into
// the actual suffix ("st", "nd", "rd", "th") is left to the caller.
fmt.Println(l.OrdinalPluralRule(21, 0)) // One (-> "21st")
fmt.Println(l.OrdinalPluralRule(22, 0)) // Two (-> "22nd")
fmt.Println(l.OrdinalPluralRule(33, 0)) // Few (-> "33rd")
fmt.Println(l.OrdinalPluralRule(34, 0)) // Other (-> "34th")
// Range Plural Rules
fmt.Println(l.RangePluralRule(1, 0, 1, 0)) // 1-1
+12 -16
View File
@@ -498,10 +498,9 @@ func (en *en) FmtTimeShort(t time.Time) string {
b := make([]byte, 0, 32)
h := t.Hour()
if h > 12 {
h -= 12
h := t.Hour() % 12
if h == 0 {
h = 12
}
b = strconv.AppendInt(b, int64(h), 10)
@@ -528,10 +527,9 @@ func (en *en) FmtTimeMedium(t time.Time) string {
b := make([]byte, 0, 32)
h := t.Hour()
if h > 12 {
h -= 12
h := t.Hour() % 12
if h == 0 {
h = 12
}
b = strconv.AppendInt(b, int64(h), 10)
@@ -565,10 +563,9 @@ func (en *en) FmtTimeLong(t time.Time) string {
b := make([]byte, 0, 32)
h := t.Hour()
if h > 12 {
h -= 12
h := t.Hour() % 12
if h == 0 {
h = 12
}
b = strconv.AppendInt(b, int64(h), 10)
@@ -607,10 +604,9 @@ func (en *en) FmtTimeFull(t time.Time) string {
b := make([]byte, 0, 32)
h := t.Hour()
if h > 12 {
h -= 12
h := t.Hour() % 12
if h == 0 {
h = 12
}
b = strconv.AppendInt(b, int64(h), 10)
+13 -10
View File
@@ -98,7 +98,7 @@ type Translator interface {
// returns the locales narrow weekday given the 'weekday' provided
WeekdayNarrow(weekday time.Weekday) string
// WeekdaysNarrowreturns the locales narrow weekdays
// WeekdaysNarrow returns the locales narrow weekdays
WeekdaysNarrow() []string
// returns the locales short weekday given the 'weekday' provided
@@ -113,7 +113,7 @@ type Translator interface {
// returns the locales wide weekdays
WeekdaysWide() []string
// The following Functions are common Formatting functionsfor the Translator's Locale
// 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
@@ -228,14 +228,14 @@ func W(n float64, v uint64) (w int64) {
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
// with either be '0' or '0.xxxx', so if 1 then w will be zero
// 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 := len(s) + 1
end := 0
for i := end; i >= 0; i-- {
for i := len(s) - 1; i >= 0; i-- {
if s[i] != '0' {
end = i + 1
break
@@ -253,7 +253,7 @@ func F(n float64, v uint64) (f int64) {
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
// with either be '0' or '0.xxxx', so if 1 then f will be zero
// will either be '0' or '0.xxxx', so if 1 then f will be zero
// otherwise need to parse
if len(s) != 1 {
@@ -270,14 +270,14 @@ func T(n float64, v uint64) (t int64) {
s := strconv.FormatFloat(n-float64(int64(n)), 'f', int(v), 64)
// with either be '0' or '0.xxxx', so if 1 then t will be zero
// 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 := len(s) + 1
end := 0
for i := end; i >= 0; i-- {
for i := len(s) - 1; i >= 0; i-- {
if s[i] != '0' {
end = i + 1
break
@@ -286,8 +286,11 @@ func T(n float64, v uint64) (t int64) {
// ignoring error, because it can't fail as we generated
// the string internally from a real number
t, _ = strconv.ParseInt(s[:end], 10, 64)
if end > 0 {
t, _ = strconv.ParseInt(s[:end], 10, 64)
}
}
return
}
+42
View File
@@ -0,0 +1,42 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project
`universal-translator` (`ut`) is a Go i18n library that wraps `github.com/go-playground/locales` (CLDR locale data) to provide translation registration, plural rule handling (cardinal, ordinal, range), and `{0}`-style parameter substitution. Translations can be registered programmatically or loaded from JSON files.
## Commands
```bash
# Run tests with race detection and coverage
make test
# Run benchmarks
make bench
# Lint
make lint # or: golangci-lint run
# Run a single test
go test -run TestTranslatorAdd -race -v ./...
```
## Architecture
Flat package `ut` at the repo root — no sub-packages.
- **`universal_translator.go`** — `UniversalTranslator` struct: holds a `map[string]Translator` (keyed by lowercased locale) plus a fallback. Constructor: `New(fallback, ...supportedLocales)`. Methods: `FindTranslator`, `GetTranslator`, `AddTranslator`, `VerifyTranslations`.
- **`translator.go`** — `Translator` interface (embeds `locales.Translator`) and private `translator` impl. Registration: `Add`, `AddCardinal`, `AddOrdinal`, `AddRange`. Resolution: `T`, `C`, `O`, `R`. Each translation key is `interface{}` (string or int). Parameter substitution indexes are precomputed at registration time via `transText`.
- **`import_export.go`** — JSON import/export. `Export` writes per-locale JSON files. `Import` reads them (supports recursive directory traversal). `ImportByReader` for streaming.
- **`errors.go`** — Typed error structs: `ErrUnknownTranslation`, `ErrExistingTranslator`, `ErrConflictingTranslation`, `ErrRangeTranslation`, `ErrOrdinalTranslation`, `ErrCardinalTranslation`, `ErrMissingPluralTranslation`, `ErrMissingBracket`, `ErrBadParamSyntax`, `ErrMissingLocale`, `ErrBadPluralDefinition`.
Note: the private field names `cardinalTanslations`, `ordinalTanslations`, `rangeTanslations` are historical typos — do not "fix" them as it would break serialization/deserialization compatibility.
## Testing
Tests live at the repo root alongside source files. Test data JSON files are in `testdata/`. The library is goroutine-safe for reads once translations are registered — benchmarks in `benchmarks_test.go` test both serial and parallel `T()` calls.
## CI
GitHub Actions (`.github/workflows/workflow.yml`): Go 1.19.x matrix across ubuntu/macos/windows with race detection, coverage via goveralls, and golangci-lint.
+9
View File
@@ -0,0 +1,9 @@
## AI Agents
AI-assisted contributions are welcome but must be clearly attributed. If you are an AI agent or using an AI agent to contribute:
- **Commits**: Include a `Co-Authored-By:` trailer identifying the AI model used.
- **Pull requests and issues**: State that the content was AI-generated or AI-assisted in the description.
- **Code review comments**: Prefix with `[AI]` when posting automated review comments.
Contributions that obscure their AI origin may be closed.
+11
View File
@@ -82,6 +82,17 @@ Help With Tests
To anyone interesting in helping or contributing, I sure could use some help creating tests for each language.
Please see issue [here](https://github.com/go-playground/locales/issues/1) for details.
Supported Go Versions
---------------------
This package is aligned with the [Go release policy](https://go.dev/doc/devel/release) in that support is guaranteed for
the two most recent major versions.
This does not mean the package will not work with older versions of Go, only that we reserve the right to increase the
MSGV(Minimum Supported Go Version) when the need arises to address Security issues/patches, OS issues & support or newly
introduced functionality that would greatly benefit the maintenance and/or usage of this package.
If and when the MSGV is increased it will be done so in a minimum of a `Minor` release bump.
License
------
Distributed under MIT License, please see license file in code for more details.
+5 -2
View File
@@ -8,8 +8,11 @@ import (
)
var (
// ErrUnknowTranslation indicates the translation could not be found
ErrUnknowTranslation = errors.New("Unknown Translation")
// ErrUnknownTranslation indicates the translation could not be found
ErrUnknownTranslation = errors.New("unknown translation")
// Deprecated: Use ErrUnknownTranslation instead.
ErrUnknowTranslation = ErrUnknownTranslation
)
var _ error = new(ErrConflictingTranslation)
+1 -1
View File
@@ -163,7 +163,7 @@ func (t *UniversalTranslator) Import(format ImportExportFormat, dirnameOrFilenam
if err != nil {
return err
}
defer f.Close()
defer func() { _ = f.Close() }()
return t.ImportByReader(format, f)
}
+83 -33
View File
@@ -101,35 +101,87 @@ func (t *translator) Add(key interface{}, text string, override bool) error {
return &ErrConflictingTranslation{locale: t.Locale(), key: key, text: text}
}
lb := strings.Count(text, "{")
rb := strings.Count(text, "}")
if lb != rb {
return &ErrMissingBracket{locale: t.Locale(), key: key, text: text}
indexes, err := parseParamPlaceholders(text, t.Locale(), key)
if err != nil {
return err
}
trans := &transText{
text: text,
}
var idx int
for i := 0; i < lb; i++ {
s := "{" + strconv.Itoa(i) + "}"
idx = strings.Index(text, s)
if idx == -1 {
return &ErrBadParamSyntax{locale: t.Locale(), param: s, key: key, text: text}
}
trans.indexes = append(trans.indexes, idx)
trans.indexes = append(trans.indexes, idx+len(s))
}
t.translations[key] = trans
t.translations[key] = &transText{text: text, indexes: indexes}
return nil
}
// parseParamPlaceholders scans text for {N} placeholders where N is a canonical
// non-negative integer (no leading zeros). Returns triplets [start, end, paramIdx, ...]
// sorted by text position. Requires consecutive param indices starting from 0.
func parseParamPlaceholders(text string, locale string, key interface{}) ([]int, error) {
var indexes []int
seen := make(map[int]struct{})
maxParam := -1
for i := 0; i < len(text); i++ {
if text[i] != '{' {
continue
}
j := i + 1
for j < len(text) && text[j] >= '0' && text[j] <= '9' {
j++
}
digitLen := j - i - 1
if digitLen == 0 {
continue
}
if j >= len(text) {
return nil, &ErrMissingBracket{locale: locale, key: key, text: text}
}
if text[j] != '}' {
i = j - 1
continue
}
if digitLen > 1 && text[i+1] == '0' {
i = j
continue
}
if digitLen > 9 {
i = j
continue
}
n := 0
for k := i + 1; k < j; k++ {
n = n*10 + int(text[k]-'0')
}
indexes = append(indexes, i, j+1, n)
seen[n] = struct{}{}
if n > maxParam {
maxParam = n
}
i = j
}
if len(seen) == 0 {
return nil, nil
}
if maxParam != len(seen)-1 {
for i := 0; i <= maxParam; i++ {
if _, ok := seen[i]; !ok {
return nil, &ErrBadParamSyntax{locale: locale, param: "{" + strconv.Itoa(i) + "}", key: key, text: text}
}
}
}
return indexes, nil
}
// AddCardinal adds a cardinal plural translation for a particular language/locale
// {0} is the only replacement type accepted and only one variable is accepted as
// multiple cannot be used for a plural rule determination, unless it is a range;
@@ -298,20 +350,18 @@ func (t *translator) T(key interface{}, params ...string) (string, error) {
trans, ok := t.translations[key]
if !ok {
return unknownTranslation, ErrUnknowTranslation
return unknownTranslation, ErrUnknownTranslation
}
b := make([]byte, 0, 64)
var start, end, count int
var start, end int
for i := 0; i < len(trans.indexes); i++ {
for i := 0; i < len(trans.indexes); i += 3 {
end = trans.indexes[i]
b = append(b, trans.text[start:end]...)
b = append(b, params[count]...)
i++
start = trans.indexes[i]
count++
b = append(b, params[trans.indexes[i+2]]...)
start = trans.indexes[i+1]
}
b = append(b, trans.text[start:]...)
@@ -324,7 +374,7 @@ func (t *translator) C(key interface{}, num float64, digits uint64, param string
tarr, ok := t.cardinalTanslations[key]
if !ok {
return unknownTranslation, ErrUnknowTranslation
return unknownTranslation, ErrUnknownTranslation
}
rule := t.CardinalPluralRule(num, digits)
@@ -344,7 +394,7 @@ func (t *translator) O(key interface{}, num float64, digits uint64, param string
tarr, ok := t.ordinalTanslations[key]
if !ok {
return unknownTranslation, ErrUnknowTranslation
return unknownTranslation, ErrUnknownTranslation
}
rule := t.OrdinalPluralRule(num, digits)
@@ -365,7 +415,7 @@ func (t *translator) R(key interface{}, num1 float64, digits1 uint64, num2 float
tarr, ok := t.rangeTanslations[key]
if !ok {
return unknownTranslation, ErrUnknowTranslation
return unknownTranslation, ErrUnknownTranslation
}
rule := t.RangePluralRule(num1, digits1, num2, digits2)