feat(html): apply locale to context translators
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
cb901dbb71
commit
8e9ca0091c
2 changed files with 22 additions and 0 deletions
12
context.go
12
context.go
|
|
@ -36,5 +36,17 @@ func NewContext(locale ...string) *Context {
|
|||
func NewContextWithService(svc Translator, locale ...string) *Context {
|
||||
ctx := NewContext(locale...)
|
||||
ctx.service = svc
|
||||
if len(locale) > 0 {
|
||||
if setter, ok := svc.(interface{ SetLanguage(string) error }); ok {
|
||||
base := locale[0]
|
||||
for i := 0; i < len(base); i++ {
|
||||
if base[i] == '-' || base[i] == '_' {
|
||||
base = base[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
_ = setter.SetLanguage(base)
|
||||
}
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,3 +36,13 @@ func TestNewContextWithService_OptionalLocale_Good(t *testing.T) {
|
|||
t.Fatal("NewContextWithService should set translator service")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewContextWithService_AppliesLocaleToService_Good(t *testing.T) {
|
||||
svc, _ := i18n.New()
|
||||
ctx := NewContextWithService(svc, "fr-FR")
|
||||
|
||||
got := Text("prompt.yes").Render(ctx)
|
||||
if got != "o" {
|
||||
t.Fatalf("NewContextWithService locale translation = %q, want %q", got, "o")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue