aboutsummaryrefslogtreecommitdiff
path: root/ki18n/ki18n_test.go
blob: 6d1395ea070f623e8089f4a3d4bc9cc86572e4da (plain) (blame)
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

package ki18n

import (
	"bytes"
	"context"
	"fmt"
	"strings"
	"testing"
	"testing/fstest"

	"github.com/stretchr/testify/assert"
)

func TestSimpleText(t *testing.T) {
	initFrenchLocale(map[string]string{
		"Hello you": "Bonjour toi",
	})

	c := Tr("fr-FR", "Hello you")
	w := &bytes.Buffer{}
	err := c.Render(context.Background(), w)
	assert.NoError(t, err)
	assert.Equal(t, "Bonjour toi", w.String())
}

func TestVariables(t *testing.T) {
	initFrenchLocale(map[string]string{
		"Hello %s": "Bonjour %s",
	})

	c := Tr("fr-FR", "Hello %s", "John")
	w := &bytes.Buffer{}
	err := c.Render(context.Background(), w)
	assert.NoError(t, err)
	assert.Equal(t, "Bonjour John", 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()),
		},
	}
	Init(fs)
}