-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
181 lines (156 loc) · 4.15 KB
/
context.go
File metadata and controls
181 lines (156 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package html
// Note: this file is WASM-linked. Per RFC §7 the WASM build must stay under the
// 3.5 MB raw / 1 MB gzip size budget, so this shared context type remains free
// of dappco.re/go/core and other heavyweight server-only dependencies.
// 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 {
_ = setter.SetLanguage(serviceLocale(svc, locale))
}
}
func serviceLocale(svc Translator, locale string) string {
base := baseLanguage(locale)
if base == locale {
return locale
}
languages, ok := svc.(interface{ AvailableLanguages() []string })
if !ok {
return locale
}
hasBase := false
for _, lang := range languages.AvailableLanguages() {
if lang == locale {
return locale
}
if lang == base {
hasBase = true
}
}
if hasBase {
return base
}
return locale
}
func baseLanguage(locale string) string {
for i := 0; i < len(locale); i++ {
if locale[i] == '-' || locale[i] == '_' {
return locale[:i]
}
}
return locale
}
// 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
}
func cloneContext(ctx *Context) *Context {
if ctx == nil {
return nil
}
clone := *ctx
// Preserve the shared Data/Metadata alias when callers pointed both fields
// at the same map.
if sameMetadataMap(ctx.Data, ctx.Metadata) {
shared := cloneMetadataMap(ctx.Data)
clone.Data = shared
clone.Metadata = shared
return &clone
}
clone.Data = cloneMetadataMap(ctx.Data)
clone.Metadata = cloneMetadataMap(ctx.Metadata)
return &clone
}
func cloneMetadataMap(src map[string]any) map[string]any {
if src == nil {
return nil
}
dst := make(map[string]any, len(src))
for key, value := range src {
dst[key] = value
}
return dst
}
func sameMetadataMap(a, b map[string]any) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
key := metadataAliasProbeKey(a, b)
marker := &struct{}{}
a[key] = marker
defer delete(a, key)
value, ok := b[key]
return ok && value == marker
}
func metadataAliasProbeKey(a, b map[string]any) string {
key := "__go_html_metadata_alias_probe__"
for {
if _, ok := a[key]; ok {
key += "_"
continue
}
if _, ok := b[key]; ok {
key += "_"
continue
}
return key
}
}