Skip to content

feat(translator): look the translations up in a chain of sources - #81

Merged
Insei merged 1 commit into
mainfrom
feat/translation-sources
Oct 19, 2026
Merged

Insei merged 1 commit into
mainfrom
feat/translation-sources

Conversation

@Insei

@Insei Insei commented Oct 19, 2026

Copy link
Copy Markdown
Owner

The ground of the adapters of external i18n libraries, the YAML of valigo stays the default:

  • translator.Source: Translate(lang, key, args, params) (string, bool) — a translation of a key in one language, or none.
  • translator.WithSources(a, b, ...): for every language of a request in order, the first source with a translation wins — WithSources(goi18n.Source(bundle), translator.Embedded()) is your messages in go-i18n and the built-in ones for the rest; a German request gets the built-in German before your English.
  • translator.Embedded(opts...): the built-in YAML locales as a source; the in-memory storage implements Source.
  • The named parameters of the messages (max, min, field...) reach the sources: Msg.Error uses ErrorParams of the helper of the validator when it has one, the translator of valigo passes them by TParams. shared.Helper, translator.Translator and StorageRO are unchanged; custom helpers and translators work as before.

The valid path is untouched, the change is in the building of the errors. 100% coverage.

translator.Source translates a key in one language or reports that it
has none; WithSources looks every language of a request up in the
sources in order, so your messages go before the built-in ones and
the built-in ones translate the rest. Embedded is the source of the
YAML locales of valigo. The named parameters of the messages, {"max":
64}, reach the sources for the named placeholders of go-i18n and others:
the helper of the validator passes them, the interfaces are unchanged.
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark diff: 2f1c9be → 5479c5b (10 runs)

goos: linux
goarch: amd64
pkg: github.com/insei/valigo
cpu: AMD EPYC 7763 64-Core Processor                
           │  base.txt   │           head.txt            │
           │   sec/op    │   sec/op     vs base          │
Validate-4   147.7n ± 2%   147.3n ± 2%  ~ (p=0.342 n=10)

           │  base.txt  │            head.txt            │
           │    B/op    │    B/op     vs base            │
Validate-4   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

           │  base.txt  │            head.txt            │
           │ allocs/op  │ allocs/op   vs base            │
Validate-4   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (2f1c9be) to head (ae29884).

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #81   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           38        39    +1     
  Lines         1784      1806   +22     
=========================================
+ Hits          1784      1806   +22     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Insei
Insei merged commit 7a18459 into main Oct 19, 2026
7 checks passed
@Insei
Insei deleted the feat/translation-sources branch October 19, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants