50 lines
1.2 KiB
Go
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")
|
|
}
|
|
}
|