// SPDX-Licence-Identifier: EUPL-1.2 package html import ( "testing" i18n "dappco.re/go/core/i18n" "github.com/stretchr/testify/require" ) 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_NewContext_AppliesLocaleToDefaultService(t *testing.T) { ctx := NewContext("fr-FR") if got := Text("prompt.yes").Render(ctx); got != "o" { t.Fatalf("NewContext(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") } } func TestText_RenderFallsBackToDefaultTranslator(t *testing.T) { svc, _ := i18n.New() i18n.SetDefault(svc) require.NoError(t, svc.SetLanguage("fr")) ctx := &Context{} if got := Text("prompt.yes").Render(ctx); got != "o" { t.Fatalf("Text() fallback translation = %q, want %q", got, "o") } }