Brings the portable pieces of mini's feat/main-session-work onto dev:
- context.go: Context.Metadata field — alias for Data, references the same
underlying map. Both fields interchangeable at read/write sites.
- responsive.go: responsiveVariant gains an optional media field for CSS
media-query hints (e.g. "(min-width: 1024px)"); new Responsive.Add(name,
layout, media...) method. Existing Variant(name, layout) becomes a thin
alias over Add.
- go.mod: drop stale dappco.re/go/core/{inference,log} indirect requires
(refreshed by go mod tidy — forge.lthn.ai/core/{go-inference,go-log}).
Mini's local deps/go-i18n/reversal grammar-vector rewrite does not apply to
dev: dev consumes reversal as an external package (dappco.re/go/core/i18n/reversal)
rather than an internal fork. That portion of the branch is left on
feat/main-session-work for future re-integration.
Verified: GOWORK=off go build ./... + go test ./... passes.
Co-Authored-By: Virgil <virgil@lethean.io>
87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package html
|
|
|
|
// Translator provides Text() lookups for a rendering context.
|
|
// Usage example: ctx := NewContextWithService(myTranslator)
|
|
//
|
|
// The default server build uses go-i18n. Alternate builds, including WASM,
|
|
// can provide any implementation with the same T() method.
|
|
type Translator interface {
|
|
T(key string, args ...any) string
|
|
}
|
|
|
|
// Context carries rendering state through the node tree.
|
|
// Usage example: ctx := NewContext()
|
|
//
|
|
// Metadata is an alias for Data — both fields reference the same underlying map.
|
|
// Treat them as interchangeable; use whichever reads best in context.
|
|
type Context struct {
|
|
Identity string
|
|
Locale string
|
|
Entitlements func(feature string) bool
|
|
Data map[string]any
|
|
Metadata map[string]any
|
|
service Translator
|
|
}
|
|
|
|
func applyLocaleToService(svc Translator, locale string) {
|
|
if svc == nil || locale == "" {
|
|
return
|
|
}
|
|
|
|
if setter, ok := svc.(interface{ SetLanguage(string) error }); ok {
|
|
base := locale
|
|
for i := 0; i < len(base); i++ {
|
|
if base[i] == '-' || base[i] == '_' {
|
|
base = base[:i]
|
|
break
|
|
}
|
|
}
|
|
_ = setter.SetLanguage(base)
|
|
}
|
|
}
|
|
|
|
// NewContext creates a new rendering context with sensible defaults.
|
|
// Usage example: html := Render(Text("welcome"), NewContext("en-GB"))
|
|
func NewContext(locale ...string) *Context {
|
|
data := make(map[string]any)
|
|
ctx := &Context{
|
|
Data: data,
|
|
Metadata: data, // alias — same underlying map
|
|
}
|
|
if len(locale) > 0 {
|
|
ctx.SetLocale(locale[0])
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
// NewContextWithService creates a rendering context backed by a specific translator.
|
|
// Usage example: ctx := NewContextWithService(myTranslator, "en-GB")
|
|
func NewContextWithService(svc Translator, locale ...string) *Context {
|
|
ctx := NewContext(locale...)
|
|
ctx.SetService(svc)
|
|
return ctx
|
|
}
|
|
|
|
// SetService swaps the translator used by the context.
|
|
// Usage example: ctx.SetService(myTranslator)
|
|
func (ctx *Context) SetService(svc Translator) *Context {
|
|
if ctx == nil {
|
|
return nil
|
|
}
|
|
|
|
ctx.service = svc
|
|
applyLocaleToService(svc, ctx.Locale)
|
|
return ctx
|
|
}
|
|
|
|
// SetLocale updates the context locale and reapplies it to the active translator.
|
|
// Usage example: ctx.SetLocale("en-GB")
|
|
func (ctx *Context) SetLocale(locale string) *Context {
|
|
if ctx == nil {
|
|
return nil
|
|
}
|
|
|
|
ctx.Locale = locale
|
|
applyLocaleToService(ctx.service, ctx.Locale)
|
|
return ctx
|
|
}
|