feat(html): restore context mutators
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
3d841efa12
commit
6f65fc903c
3 changed files with 56 additions and 1 deletions
24
context.go
24
context.go
|
|
@ -72,7 +72,31 @@ 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...)
|
||||
return ctx.SetService(svc)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,3 +48,34 @@ func TestContext_NewContextWithService_UsesLocale(t *testing.T) {
|
|||
t.Fatalf("NewContextWithService translation = %q, want %q", got, "y")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_SetLocale_ReappliesTranslatorLanguage(t *testing.T) {
|
||||
svc := &localeTranslator{}
|
||||
ctx := NewContextWithService(svc, "en-GB")
|
||||
|
||||
ctx.SetLocale("fr-FR")
|
||||
|
||||
if ctx.Locale != "fr-FR" {
|
||||
t.Fatalf("SetLocale() Locale = %q, want %q", ctx.Locale, "fr-FR")
|
||||
}
|
||||
if svc.language != "fr" {
|
||||
t.Fatalf("SetLocale() should reapply language to translator, got %q", svc.language)
|
||||
}
|
||||
if got := Text("prompt.yes").Render(ctx); got != "o" {
|
||||
t.Fatalf("SetLocale() translation = %q, want %q", got, "o")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_SetService_AppliesCurrentLocale(t *testing.T) {
|
||||
svc := &localeTranslator{}
|
||||
ctx := NewContext("fr-FR")
|
||||
|
||||
ctx.SetService(svc)
|
||||
|
||||
if ctx.service != svc {
|
||||
t.Fatal("SetService() should retain the provided service")
|
||||
}
|
||||
if svc.language != "fr" {
|
||||
t.Fatalf("SetService() should apply current locale to translator, got %q", svc.language)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -299,6 +299,6 @@ func TestGenerateClass_Good(t *testing.T) {
|
|||
|
||||
- `NewLayout("XYZ")` silently produces empty output for unrecognised slot letters. Valid letters are `H`, `L`, `C`, `R`, `F`. There is no error or warning.
|
||||
- `Responsive.Variant()` accepts only `*Layout`, not arbitrary `Node` values. Arbitrary subtrees must be wrapped in a single-slot layout first.
|
||||
- `Context.service` is unexported. Custom i18n service injection requires `NewContextWithService()`. There is no way to swap the service after construction.
|
||||
- `Context.service` is unexported. Custom i18n service injection uses `NewContextWithService()` or `SetService()`, and `SetLocale()` reapplies the active locale to the current translator.
|
||||
- The WASM module has no integration test for the JavaScript exports. `size_test.go` tests binary size only; it does not exercise `renderToString` behaviour from JavaScript.
|
||||
- `codegen.GenerateBundle()` iterates a `map`, so the order of class definitions in the output is non-deterministic. This does not affect correctness but may cause cosmetic diffs between runs.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue