feat(html): apply locale to context translators
All checks were successful
Security Scan / security (push) Successful in 9s
Test / test (push) Successful in 1m1s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-03-31 20:25:09 +00:00
parent cb901dbb71
commit 8e9ca0091c
2 changed files with 22 additions and 0 deletions

View file

@ -36,5 +36,17 @@ func NewContext(locale ...string) *Context {
func NewContextWithService(svc Translator, locale ...string) *Context {
ctx := NewContext(locale...)
ctx.service = svc
if len(locale) > 0 {
if setter, ok := svc.(interface{ SetLanguage(string) error }); ok {
base := locale[0]
for i := 0; i < len(base); i++ {
if base[i] == '-' || base[i] == '_' {
base = base[:i]
break
}
}
_ = setter.SetLanguage(base)
}
}
return ctx
}

View file

@ -36,3 +36,13 @@ func TestNewContextWithService_OptionalLocale_Good(t *testing.T) {
t.Fatal("NewContextWithService should set translator service")
}
}
func TestNewContextWithService_AppliesLocaleToService_Good(t *testing.T) {
svc, _ := i18n.New()
ctx := NewContextWithService(svc, "fr-FR")
got := Text("prompt.yes").Render(ctx)
if got != "o" {
t.Fatalf("NewContextWithService locale translation = %q, want %q", got, "o")
}
}