diff --git a/cmd/wasm/main.go b/cmd/wasm/main.go index fe79af2..8fb4fed 100644 --- a/cmd/wasm/main.go +++ b/cmd/wasm/main.go @@ -21,11 +21,11 @@ func renderToString(_ js.Value, args []js.Value) any { } variant := args[0].String() - ctx := html.NewContext() - + locale := "" if len(args) >= 2 && args[1].Type() == js.TypeString { - ctx.SetLocale(args[1].String()) + locale = args[1].String() } + ctx := html.NewContext(locale) layout := html.NewLayout(variant) diff --git a/context.go b/context.go index d0864be..cdb8f1c 100644 --- a/context.go +++ b/context.go @@ -8,6 +8,7 @@ type Translator interface { // context.go: Context carries rendering state through the node tree. // Example: NewContext("en-GB") initialises locale-specific rendering state. +// Locale and translator selection happen at construction time. type Context struct { Identity string Locale string @@ -61,7 +62,7 @@ func NewContext(locale ...string) *Context { Data: make(map[string]any), } if len(locale) > 0 { - ctx.SetLocale(locale[0]) + ctx.Locale = locale[0] } return ctx } @@ -71,32 +72,7 @@ func NewContext(locale ...string) *Context { // An optional locale may be provided as the second argument. func NewContextWithService(svc Translator, locale ...string) *Context { ctx := NewContext(locale...) - ctx.SetService(svc) - return ctx -} - -// context.go: SetService swaps the translator used by the context. -// Example: ctx.SetService(svc). -func (ctx *Context) SetService(svc Translator) *Context { - if ctx == nil { - return nil - } - - ensureContextDefaults(ctx) ctx.service = svc applyLocaleToService(svc, ctx.Locale) return ctx } - -// context.go: SetLocale updates the context locale and reapplies it to the active translator. -// Example: ctx.SetLocale("en-US"). -func (ctx *Context) SetLocale(locale string) *Context { - if ctx == nil { - return nil - } - - ensureContextDefaults(ctx) - ctx.Locale = locale - applyLocaleToService(ctx.service, ctx.Locale) - return ctx -} diff --git a/context_test.go b/context_test.go index 272fe85..d316590 100644 --- a/context_test.go +++ b/context_test.go @@ -23,50 +23,28 @@ func (t *localeTranslator) SetLanguage(language string) error { return nil } -func TestContext_SetService_AppliesLocale(t *testing.T) { +func TestContext_NewContextWithService_AppliesLocale(t *testing.T) { svc := &localeTranslator{} - ctx := NewContext("fr-FR") - - if got := ctx.SetService(svc); got != ctx { - t.Fatal("SetService should return the same context for chaining") - } + ctx := NewContextWithService(svc, "fr-FR") if svc.language != "fr" { - t.Fatalf("SetService should apply locale to translator, got %q", svc.language) + t.Fatalf("NewContextWithService should apply locale to translator, got %q", svc.language) } if got := Text("prompt.yes").Render(ctx); got != "o" { - t.Fatalf("SetService locale translation = %q, want %q", got, "o") + t.Fatalf("NewContextWithService locale translation = %q, want %q", got, "o") } } -func TestContext_SetLocale_AppliesLocale(t *testing.T) { +func TestContext_NewContextWithService_UsesLocale(t *testing.T) { svc := &localeTranslator{} - ctx := NewContextWithService(svc) - - if got := ctx.SetLocale("en-GB"); got != ctx { - t.Fatal("SetLocale should return the same context for chaining") - } + ctx := NewContextWithService(svc, "en-GB") if svc.language != "en" { - t.Fatalf("SetLocale should apply locale to translator, got %q", svc.language) + t.Fatalf("NewContextWithService should apply locale to translator, got %q", svc.language) } if got := Text("prompt.yes").Render(ctx); got != "y" { - t.Fatalf("SetLocale translation = %q, want %q", got, "y") - } -} - -func TestContext_SetService_NilContext(t *testing.T) { - var ctx *Context - if got := ctx.SetService(nil); got != nil { - t.Fatal("SetService on nil context should return nil") - } -} - -func TestContext_SetLocale_NilContext(t *testing.T) { - var ctx *Context - if got := ctx.SetLocale("en-GB"); got != nil { - t.Fatal("SetLocale on nil context should return nil") + t.Fatalf("NewContextWithService translation = %q, want %q", got, "y") } }