go-html/context_test.go
Virgil 2b796e57eb
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run
fix(html): remove mutable context setters
Co-Authored-By: Virgil <virgil@lethean.io>
2026-04-03 18:40:46 +00:00

50 lines
1.2 KiB
Go

// SPDX-Licence-Identifier: EUPL-1.2
package html
import "testing"
type localeTranslator struct {
language string
}
func (t *localeTranslator) T(key string, args ...any) string {
if key == "prompt.yes" && t.language == "fr" {
return "o"
}
if key == "prompt.yes" && t.language == "en" {
return "y"
}
return key
}
func (t *localeTranslator) SetLanguage(language string) error {
t.language = language
return nil
}
func TestContext_NewContextWithService_AppliesLocale(t *testing.T) {
svc := &localeTranslator{}
ctx := NewContextWithService(svc, "fr-FR")
if svc.language != "fr" {
t.Fatalf("NewContextWithService should apply locale to translator, got %q", svc.language)
}
if got := Text("prompt.yes").Render(ctx); got != "o" {
t.Fatalf("NewContextWithService locale translation = %q, want %q", got, "o")
}
}
func TestContext_NewContextWithService_UsesLocale(t *testing.T) {
svc := &localeTranslator{}
ctx := NewContextWithService(svc, "en-GB")
if svc.language != "en" {
t.Fatalf("NewContextWithService should apply locale to translator, got %q", svc.language)
}
if got := Text("prompt.yes").Render(ctx); got != "y" {
t.Fatalf("NewContextWithService translation = %q, want %q", got, "y")
}
}