MSP-8976 Up deps

This commit is contained in:
Иван Перлов committed 2026-11-20 17:05:48 +03:00
1 parent ebc3c1dbc5
commit 0d9921bbce
603 files changed
+47715 -13202

No files matched your search

+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)