fix(html): isolate cloned default translators
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
c1e7cfaab0
commit
a4d2341211
4 changed files with 58 additions and 0 deletions
|
|
@ -35,6 +35,7 @@ func (ctx *Context) Clone() *Context {
|
|||
clone.Data[key] = value
|
||||
}
|
||||
}
|
||||
clone.service = cloneTranslator(clone.service, clone.Locale)
|
||||
return &clone
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -175,6 +175,24 @@ func TestContext_CloneCopiesDataWithoutSharingMap(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContext_CloneDoesNotShareDefaultTranslator(t *testing.T) {
|
||||
ctx := NewContext("en-GB")
|
||||
|
||||
clone := ctx.Clone()
|
||||
if clone == nil {
|
||||
t.Fatal("Clone should return a context")
|
||||
}
|
||||
|
||||
clone.SetLocale("fr-FR")
|
||||
|
||||
if got := Text("prompt.yes").Render(ctx); got != "y" {
|
||||
t.Fatalf("Clone should not mutate original default translator, got %q", got)
|
||||
}
|
||||
if got := Text("prompt.yes").Render(clone); got != "o" {
|
||||
t.Fatalf("Clone should keep its own default translator, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext_WithDataReturnsClonedContext(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
ctx.SetData("theme", "dark")
|
||||
|
|
|
|||
21
translator_clone_default.go
Normal file
21
translator_clone_default.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
//go:build !js
|
||||
|
||||
// SPDX-Licence-Identifier: EUPL-1.2
|
||||
|
||||
package html
|
||||
|
||||
import i18n "dappco.re/go/core/i18n"
|
||||
|
||||
func cloneTranslator(svc Translator, locale string) Translator {
|
||||
if svc == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if current, ok := svc.(*i18n.Service); ok && current != nil {
|
||||
clone := &i18n.Service{}
|
||||
applyLocaleToService(clone, locale)
|
||||
return clone
|
||||
}
|
||||
|
||||
return svc
|
||||
}
|
||||
18
translator_clone_js.go
Normal file
18
translator_clone_js.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
//go:build js
|
||||
|
||||
// SPDX-Licence-Identifier: EUPL-1.2
|
||||
|
||||
package html
|
||||
|
||||
func cloneTranslator(svc Translator, _ string) Translator {
|
||||
if svc == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if current, ok := svc.(*defaultTranslator); ok && current != nil {
|
||||
clone := *current
|
||||
return &clone
|
||||
}
|
||||
|
||||
return svc
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue