-
Notifications
You must be signed in to change notification settings - Fork 4
/
locale_test.go
139 lines (115 loc) · 4.08 KB
/
locale_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
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
package spreak
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/language"
"github.com/vorlif/spreak/catalog"
)
func getLocale(t *testing.T) *locale {
bundle, errB := NewBundle(
WithDefaultDomain("a"),
WithDomainPath("a", testTranslationDir),
WithDomainPath("z", testTranslationDir),
WithLanguage(language.German),
)
require.NoError(t, errB)
require.NotNil(t, bundle)
localizer := NewLocalizer(bundle, language.German)
require.NotNil(t, localizer)
return localizer.locale
}
func TestLocale_MainFunctions(t *testing.T) {
tLocale := getLocale(t)
t.Run("translate singular", func(t *testing.T) {
for _, tt := range singularTestData {
got, err := tLocale.lookupSingularTranslation("a", tt.ctx, tt.msgID, tt.params...)
tt.wantErr(t, err, fmt.Sprintf("lookupSingularTranslation(%q, %v)", tt.ctx, tt.msgID))
if err != nil {
assert.Equalf(t, "", got, "lookupSingularTranslation(%q, %v)", tt.ctx, tt.msgID)
} else {
assert.Equalf(t, tt.translated, got, "lookupSingularTranslation(%q, %v)", tt.ctx, tt.msgID)
}
}
})
t.Run("translate plural", func(t *testing.T) {
for idx, tt := range pluralTestData {
got, err := tLocale.lookupPluralTranslation("a", tt.ctx, tt.msgID, tt.plural, tt.n, tt.params...)
tt.wantErr(t, err, fmt.Sprintf("tLocale idx=%d lookupPluralTranslation(%q, %q)", idx, tt.ctx, tt.msgID))
if err != nil {
assert.Equalf(t, "", got, "tLocale idx=%d lookupPluralTranslation(%q, %q)", idx, tt.ctx, tt.msgID)
} else {
assert.Equalf(t, tt.translated, got, "tLocale idx=%d lookupPluralTranslation(%q, %q)", idx, tt.ctx, tt.msgID)
}
}
})
t.Run("empty text is returned for a non-existent domain", func(t *testing.T) {
tr, err := tLocale.lookupPluralTranslation("unknown", NoCtx, "%d day", "%d days", 1, 1)
assert.Error(t, err)
assert.Equal(t, "", tr)
tr, err = tLocale.lookupPluralTranslation("unknown", NoCtx, "%d day", "%d days", 10, 10)
assert.Error(t, err)
assert.Equal(t, "", tr)
tr, err = tLocale.lookupSingularTranslation("unknown", NoCtx, "%d day", 1)
assert.Error(t, err)
assert.Equal(t, "", tr)
})
}
func TestNewLocale_UseSourceLanguage(t *testing.T) {
bundle, errB := NewBundle(
WithSourceLanguage(language.Italian),
WithDefaultDomain("a"),
WithDomainPath("a", testTranslationDir),
WithDomainPath("z", testTranslationDir),
WithLanguage(language.German),
)
require.NoError(t, errB)
require.NotNil(t, bundle)
locale := buildSourceLocale(bundle, language.Italian)
require.NotNil(t, locale)
assert.Equal(t, language.Italian, locale.language)
assert.True(t, locale.isSourceLanguage)
}
func TestLocaleMissingCallback(t *testing.T) {
var lastErr error
bundle, errB := NewBundle(
WithDefaultDomain("a"),
WithDomainPath("a", testTranslationDir),
WithDomainPath("z", testTranslationDir),
WithLanguage(language.German),
WithMissingTranslationCallback(func(err error) {
lastErr = err
}),
)
require.NoError(t, errB)
require.NotNil(t, bundle)
locale := NewLocalizer(bundle, language.German)
require.NotNil(t, locale)
want := "empty translation"
get := locale.Get(want)
assert.Equal(t, want, get)
assert.IsType(t, &catalog.ErrMissingTranslation{}, lastErr)
get = locale.NGet(want, "plural", 1)
assert.Equal(t, want, get)
assert.IsType(t, &catalog.ErrMissingTranslation{}, lastErr)
get = locale.PGet("unknown", want)
assert.Equal(t, want, get)
assert.IsType(t, &catalog.ErrMissingContext{}, lastErr)
get = locale.NPGet("unknown", want, "plural", 1)
assert.Equal(t, want, get)
assert.IsType(t, &catalog.ErrMissingContext{}, lastErr)
get = locale.DGet("unknown-domain", want)
assert.Equal(t, want, get)
assert.IsType(t, &ErrMissingDomain{}, lastErr)
get = locale.DNGet("unknown-domain", want, "plural", 1)
assert.Equal(t, want, get)
assert.IsType(t, &ErrMissingDomain{}, lastErr)
want = "#+/?%=§$=§$"
get = locale.Get(want)
assert.Equal(t, want, get)
assert.IsType(t, &catalog.ErrMissingMessageID{}, lastErr)
get = locale.NGet(want, "plural", 1)
assert.Equal(t, want, get)
assert.IsType(t, &catalog.ErrMissingMessageID{}, lastErr)
}