diff --git a/context.go b/context.go index 8735bc5..110757a 100644 --- a/context.go +++ b/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 } diff --git a/context_test.go b/context_test.go index a9bedea..091785c 100644 --- a/context_test.go +++ b/context_test.go @@ -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") + } +}