fix(html): isolate cloned default translators
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 00:49:13 +00:00
parent c1e7cfaab0
commit a4d2341211
4 changed files with 58 additions and 0 deletions

View file

@ -35,6 +35,7 @@ func (ctx *Context) Clone() *Context {
clone.Data[key] = value
}
}
clone.service = cloneTranslator(clone.service, clone.Locale)
return &clone
}

View file

@ -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")

View 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
View 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
}