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
|
package ki18n
import (
"bytes"
"context"
"fmt"
"strings"
"testing"
"testing/fstest"
"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",
})
result := render(t, Tr("fr-FR", "Hello you"))
assert.Equal(t, "Bonjour toi", result)
}
func TestVariables(t *testing.T) {
initFrenchLocale(map[string]string{
"Hello %s": "Bonjour %s",
})
result := render(t, Tr("fr-FR", "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>",
})
result := render(t, Tr("fr-FR", "Hello <span class=\"text-bold\">%s</span>", "John"))
assert.Equal(t, "Bonjour <span class=\"text-bold\">John</span>", result)
}
// 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")
}
|