-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator_extra_test.go
89 lines (70 loc) · 2.14 KB
/
translator_extra_test.go
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
//go:build i18n_extra
package i18n
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/language"
"golang.org/x/text/message/catalog"
"github.com/derfenix/goi18n/internal"
)
func TestOverrideTranslation(t *testing.T) {
t.Parallel()
SetExternalBuilder(func(builder *catalog.Builder) error {
if err := builder.Set(language.Russian, "test", catalog.String("Тост %s")); err != nil {
return err
}
return nil
})
require.NoError(t, Init(internal.TestFS))
translated := GetPrinter(language.Russian).Sprintf("test", "был")
assert.Equal(t, "Тост был", translated)
}
func TestExternalLoader(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/en":
_, _ = w.Write([]byte(`[{"key": "Published","translation": "Foo"}]`))
case "/ru":
_, _ = w.Write([]byte(`[{"key": "Published","translation": "Буу"}]`))
default:
w.WriteHeader(http.StatusNotFound)
}
}))
SetExternalLoader(NewExternalLoader(srv.URL, http.Header{}))
require.NoError(t, Init(internal.TestFS))
{
translated := GetPrinter(language.Russian).Sprintf("Published")
assert.Equal(t, "Буу", translated)
}
{
translated := GetPrinter(language.English).Sprintf("Published")
assert.Equal(t, "Foo", translated)
}
t.Run("refresh", func(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
case "/en":
_, _ = w.Write([]byte(`[{"key": "Published","translation": "Bar"}]`))
case "/ru":
_, _ = w.Write([]byte(`[{"key": "Published","translation": "Бяя"}]`))
default:
w.WriteHeader(http.StatusNotFound)
}
}))
SetExternalLoader(NewExternalLoader(srv.URL, http.Header{}))
require.NoError(t, RefreshTranslations())
{
translated := GetPrinter(language.Russian).Sprintf("Published")
assert.Equal(t, "Бяя", translated)
}
{
translated := GetPrinter(language.English).Sprintf("Published")
assert.Equal(t, "Bar", translated)
}
})
}