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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
package ki18n
import (
"bytes"
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/fstest"
"time"
"github.com/a-h/templ"
"github.com/martinlehoux/kagamigo/kcore"
"github.com/stretchr/testify/assert"
)
func TestSimpleText(t *testing.T) {
initFrenchLocale(map[string]string{
"Hello you": "Bonjour toi",
})
ctx := context.WithValue(context.Background(), contextKey{}, "fr-FR")
result := render(t, Tr(ctx, "Hello you"))
assert.Equal(t, "Bonjour toi", result)
}
func TestVariables(t *testing.T) {
initFrenchLocale(map[string]string{
"Hello %s": "Bonjour %s",
})
ctx := context.WithValue(context.Background(), contextKey{}, "fr-FR")
result := render(t, Tr(ctx, "Hello %s", "John"))
assert.Equal(t, "Bonjour John", result)
}
func TestSpanClass(t *testing.T) {
initFrenchLocale(map[string]string{
"Hello <span class=\"text-bold\">%s</span>": "Bonjour <span class=\"text-bold\">%s</span>",
})
ctx := context.WithValue(context.Background(), contextKey{}, "fr-FR")
result := render(t, Tr(ctx, "Hello <span class=\"text-bold\">%s</span>", "John"))
assert.Equal(t, "Bonjour <span class=\"text-bold\">John</span>", result)
}
func TestTr_withoutLangInContext(t *testing.T) {
initFrenchLocale(map[string]string{
"Hello": "Bonjour",
})
result := render(t, Tr(context.Background(), "Hello"))
assert.Equal(t, "Hello", result)
}
func TestFormatTime_englishLocale(t *testing.T) {
ctx := context.WithValue(context.Background(), contextKey{}, "en-GB")
d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
assert.Equal(t, "12 May 2026", FormatTime(ctx, d))
}
func TestFormatTime_frenchLocale(t *testing.T) {
ctx := context.WithValue(context.Background(), contextKey{}, "fr-FR")
d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
assert.Equal(t, "12 mai 2026", FormatTime(ctx, d))
}
func TestFormatTime_unknownLocale(t *testing.T) {
ctx := context.WithValue(context.Background(), contextKey{}, "de-DE")
d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
assert.Equal(t, "12 May 2026", FormatTime(ctx, d))
}
func TestFormatTime_extraLocale(t *testing.T) {
spanishMonths := [...]string{
"enero", "febrero", "marzo", "abril", "mayo", "junio",
"julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre",
}
fs := fstest.MapFS{
"es-ES/index.yml": &fstest.MapFile{Data: []byte{}},
}
kcore.Expect(Init(fs, Locale{
Lang: "es-ES",
FormatTime: func(t time.Time) string {
return fmt.Sprintf("%d de %s de %d", t.Day(), spanishMonths[t.Month()-1], t.Year())
},
}), "failed to init fs")
ctx := context.WithValue(context.Background(), contextKey{}, "es-ES")
d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
assert.Equal(t, "12 de mayo de 2026", FormatTime(ctx, d))
}
func TestFormatTime_extraLocaleNoFormatter(t *testing.T) {
fs := fstest.MapFS{
"es-ES/index.yml": &fstest.MapFile{Data: []byte{}},
}
kcore.Expect(Init(fs, Locale{Lang: "es-ES"}), "failed to init fs")
ctx := context.WithValue(context.Background(), contextKey{}, "es-ES")
d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
assert.Equal(t, "12 May 2026", FormatTime(ctx, d))
}
func TestFormatTime_noLocaleInContext(t *testing.T) {
d := time.Date(2026, time.May, 12, 0, 0, 0, 0, time.UTC)
assert.Equal(t, "12 May 2026", FormatTime(context.Background(), d))
}
func TestCookieStrategy_found(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.AddCookie(&http.Cookie{Name: "lang", Value: "fr-FR"})
s := CookieStrategy("lang")
assert.Equal(t, "fr-FR", s.Detect(r))
}
func TestCookieStrategy_missing(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
s := CookieStrategy("lang")
assert.Equal(t, "", s.Detect(r))
}
func TestAcceptLanguageStrategy_found(t *testing.T) {
initFrenchLocale(map[string]string{})
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("Accept-Language", "fr-FR,fr;q=0.9")
assert.Equal(t, "fr-FR", AcceptLanguageStrategy.Detect(r))
}
func TestAcceptLanguageStrategy_missing(t *testing.T) {
initFrenchLocale(map[string]string{})
r := httptest.NewRequest(http.MethodGet, "/", nil)
assert.Equal(t, "", AcceptLanguageStrategy.Detect(r))
}
func TestLangMiddleware_firstStrategyWins(t *testing.T) {
first := strategyFunc(func(_ *http.Request) string { return "fr-FR" })
second := strategyFunc(func(_ *http.Request) string { return "de-DE" })
var capturedLang string
handler := LangMiddleware(first, second)(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
capturedLang, _ = r.Context().Value(contextKey{}).(string)
}))
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
assert.Equal(t, "fr-FR", capturedLang)
}
func TestLangMiddleware_fallsThrough(t *testing.T) {
first := strategyFunc(func(_ *http.Request) string { return "" })
second := strategyFunc(func(_ *http.Request) string { return "fr-FR" })
var capturedLang string
handler := LangMiddleware(first, second)(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
capturedLang, _ = r.Context().Value(contextKey{}).(string)
}))
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
assert.Equal(t, "fr-FR", capturedLang)
}
func TestLangMiddleware_fallback(t *testing.T) {
first := strategyFunc(func(_ *http.Request) string { return "" })
var capturedLang string
handler := LangMiddleware(first)(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
capturedLang, _ = r.Context().Value(contextKey{}).(string)
}))
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
assert.Equal(t, "en-GB", capturedLang)
}
// render is a test helper that renders a component and returns the result as a string
func render(t *testing.T, c templ.Component) string {
t.Helper()
w := &bytes.Buffer{}
err := c.Render(context.Background(), w)
assert.NoError(t, err)
return w.String()
}
// initFrenchLocale creates a filesystem with fr-FR locale from a translation map
func initFrenchLocale(translations map[string]string) {
var sb strings.Builder
for key, value := range translations {
fmt.Fprintf(&sb, "%q: %q\n", key, value)
}
fs := fstest.MapFS{
"fr-FR/index.yml": &fstest.MapFile{
Data: []byte(sb.String()),
},
}
kcore.Expect(Init(fs), "failed to init fs")
}
// strategyFunc is a test helper to create a Strategy from a plain function
type strategyFunc func(*http.Request) string
func (f strategyFunc) Detect(r *http.Request) string { return f(r) }
|