feat(html): add locale setter for context translators
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
b9e2630da3
commit
2e2af31c1d
2 changed files with 33 additions and 0 deletions
12
context.go
12
context.go
|
|
@ -67,3 +67,15 @@ func (ctx *Context) SetService(svc Translator) *Context {
|
|||
applyLocaleToService(svc, ctx.Locale)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// SetLocale updates the context locale and reapplies it to the active translator.
|
||||
// Usage example: ctx.SetLocale("en-GB")
|
||||
func (ctx *Context) SetLocale(locale string) *Context {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx.Locale = locale
|
||||
applyLocaleToService(ctx.service, ctx.Locale)
|
||||
return ctx
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,3 +67,24 @@ func TestContext_SetService_NilContext_Ugly(t *testing.T) {
|
|||
t.Fatal("SetService on nil context should return nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_SetLocale_AppliesLocale_Good(t *testing.T) {
|
||||
svc, _ := i18n.New()
|
||||
ctx := NewContextWithService(svc)
|
||||
|
||||
if got := ctx.SetLocale("fr-FR"); got != ctx {
|
||||
t.Fatal("SetLocale should return the same context for chaining")
|
||||
}
|
||||
|
||||
got := Text("prompt.yes").Render(ctx)
|
||||
if got != "o" {
|
||||
t.Fatalf("SetLocale translation = %q, want %q", got, "o")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_SetLocale_NilContext_Ugly(t *testing.T) {
|
||||
var ctx *Context
|
||||
if got := ctx.SetLocale("en-GB"); got != nil {
|
||||
t.Fatal("SetLocale on nil context should return nil")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue