feat(html): add context locale setters
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
0ecef2e72f
commit
831a47f1d3
2 changed files with 73 additions and 0 deletions
28
context.go
28
context.go
|
|
@ -87,3 +87,31 @@ func NewContextWithService(svc Translator, locale ...string) *Context {
|
|||
applyLocaleToService(ctx.service, ctx.Locale)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// SetService swaps the translator used by the context and reapplies the
|
||||
// current locale to it.
|
||||
// Example: ctx.SetService(svc).
|
||||
func (ctx *Context) SetService(svc Translator) *Context {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ensureContextDefaults(ctx)
|
||||
ctx.service = svc
|
||||
applyLocaleToService(ctx.service, ctx.Locale)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,6 +62,51 @@ func TestContext_NewContextWithService_UsesLocale(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContext_SetLocale_ReappliesToTranslator(t *testing.T) {
|
||||
svc := &localeTranslator{}
|
||||
ctx := NewContextWithService(svc, "en-GB")
|
||||
|
||||
ctx.SetLocale("fr-FR")
|
||||
|
||||
if ctx.Locale != "fr-FR" {
|
||||
t.Fatalf("SetLocale should update context locale, got %q", ctx.Locale)
|
||||
}
|
||||
if svc.language != "fr" {
|
||||
t.Fatalf("SetLocale should reapply locale 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_ReappliesCurrentLocale(t *testing.T) {
|
||||
ctx := NewContext("fr-FR")
|
||||
svc := &localeTranslator{}
|
||||
|
||||
ctx.SetService(svc)
|
||||
|
||||
if ctx.service != svc {
|
||||
t.Fatalf("SetService should replace the active translator")
|
||||
}
|
||||
if svc.language != "fr" {
|
||||
t.Fatalf("SetService should apply the existing locale to the new translator, got %q", svc.language)
|
||||
}
|
||||
if got := Text("prompt.yes").Render(ctx); got != "o" {
|
||||
t.Fatalf("SetService translation = %q, want %q", got, "o")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_Setters_NilReceiver(t *testing.T) {
|
||||
var ctx *Context
|
||||
|
||||
if got := ctx.SetLocale("en-GB"); got != nil {
|
||||
t.Fatalf("nil Context.SetLocale should return nil, got %v", got)
|
||||
}
|
||||
if got := ctx.SetService(&localeTranslator{}); got != nil {
|
||||
t.Fatalf("nil Context.SetService should return nil, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestText_RenderFallsBackToDefaultTranslator(t *testing.T) {
|
||||
svc, _ := i18n.New()
|
||||
i18n.SetDefault(svc)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue