@@ -12,6 +12,26 @@ import (
12
12
"github.com/stretchr/testify/require"
13
13
)
14
14
15
+ var MockPluralRule PluralFormRule = func (n int64 ) PluralFormIndex {
16
+ if n == 0 {
17
+ return PluralFormZero
18
+ }
19
+ if n == 1 {
20
+ return PluralFormOne
21
+ }
22
+ if n >= 2 && n <= 4 {
23
+ return PluralFormFew
24
+ }
25
+ return PluralFormOther
26
+ }
27
+
28
+ var MockPluralRuleEnglish PluralFormRule = func (n int64 ) PluralFormIndex {
29
+ if n == 1 {
30
+ return PluralFormOne
31
+ }
32
+ return PluralFormOther
33
+ }
34
+
15
35
func TestLocaleStore (t * testing.T ) {
16
36
testData1 := []byte (`
17
37
.dot.name = Dot Name
@@ -27,11 +47,48 @@ fmt = %[2]s %[1]s
27
47
28
48
[section]
29
49
sub = Changed Sub String
50
+ commits = fallback value for commits
51
+ ` )
52
+
53
+ testDataJSON2 := []byte (`
54
+ {
55
+ "section.json": "the JSON is %s",
56
+ "section.commits": {
57
+ "one": "one %d commit",
58
+ "few": "some %d commits",
59
+ "other": "lots of %d commits"
60
+ },
61
+ "section.incomplete": {
62
+ "few": "some %d objects (translated)"
63
+ },
64
+ "nested": {
65
+ "outer": {
66
+ "inner": {
67
+ "json": "Hello World",
68
+ "issue": {
69
+ "one": "one %d issue",
70
+ "few": "some %d issues",
71
+ "other": "lots of %d issues"
72
+ }
73
+ }
74
+ }
75
+ }
76
+ }
77
+ ` )
78
+ testDataJSON1 := []byte (`
79
+ {
80
+ "section.incomplete": {
81
+ "one": "[untranslated] some %d object",
82
+ "other": "[untranslated] some %d objects"
83
+ }
84
+ }
30
85
` )
31
86
32
87
ls := NewLocaleStore ()
33
- require .NoError (t , ls .AddLocaleByIni ("lang1" , "Lang1" , testData1 , nil ))
34
- require .NoError (t , ls .AddLocaleByIni ("lang2" , "Lang2" , testData2 , nil ))
88
+ require .NoError (t , ls .AddLocaleByIni ("lang1" , "Lang1" , MockPluralRuleEnglish , testData1 , nil ))
89
+ require .NoError (t , ls .AddLocaleByIni ("lang2" , "Lang2" , MockPluralRule , testData2 , nil ))
90
+ require .NoError (t , ls .AddToLocaleFromJSON ("lang1" , testDataJSON1 ))
91
+ require .NoError (t , ls .AddToLocaleFromJSON ("lang2" , testDataJSON2 ))
35
92
ls .SetDefaultLang ("lang1" )
36
93
37
94
lang1 , _ := ls .Locale ("lang1" )
@@ -56,6 +113,45 @@ sub = Changed Sub String
56
113
result2 := lang2 .TrHTML ("section.mixed" , "a&b" )
57
114
assert .EqualValues (t , `test value; <span style="color: red; background: none;">a&b</span>` , result2 )
58
115
116
+ result = lang2 .TrString ("section.json" , "valid" )
117
+ assert .Equal (t , "the JSON is valid" , result )
118
+
119
+ result = lang2 .TrString ("nested.outer.inner.json" )
120
+ assert .Equal (t , "Hello World" , result )
121
+
122
+ result = lang2 .TrString ("section.commits" )
123
+ assert .Equal (t , "lots of %d commits" , result )
124
+
125
+ result2 = lang2 .TrPluralString (1 , "section.commits" , 1 )
126
+ assert .EqualValues (t , "one 1 commit" , result2 )
127
+
128
+ result2 = lang2 .TrPluralString (3 , "section.commits" , 3 )
129
+ assert .EqualValues (t , "some 3 commits" , result2 )
130
+
131
+ result2 = lang2 .TrPluralString (8 , "section.commits" , 8 )
132
+ assert .EqualValues (t , "lots of 8 commits" , result2 )
133
+
134
+ result2 = lang2 .TrPluralString (0 , "section.commits" )
135
+ assert .EqualValues (t , "section.commits" , result2 )
136
+
137
+ result2 = lang2 .TrPluralString (1 , "nested.outer.inner.issue" , 1 )
138
+ assert .EqualValues (t , "one 1 issue" , result2 )
139
+
140
+ result2 = lang2 .TrPluralString (3 , "nested.outer.inner.issue" , 3 )
141
+ assert .EqualValues (t , "some 3 issues" , result2 )
142
+
143
+ result2 = lang2 .TrPluralString (9 , "nested.outer.inner.issue" , 9 )
144
+ assert .EqualValues (t , "lots of 9 issues" , result2 )
145
+
146
+ result2 = lang2 .TrPluralString (3 , "section.incomplete" , 3 )
147
+ assert .EqualValues (t , "some 3 objects (translated)" , result2 )
148
+
149
+ result2 = lang2 .TrPluralString (1 , "section.incomplete" , 1 )
150
+ assert .EqualValues (t , "[untranslated] some 1 object" , result2 )
151
+
152
+ result2 = lang2 .TrPluralString (7 , "section.incomplete" , 7 )
153
+ assert .EqualValues (t , "[untranslated] some 7 objects" , result2 )
154
+
59
155
langs , descs := ls .ListLangNameDesc ()
60
156
assert .ElementsMatch (t , []string {"lang1" , "lang2" }, langs )
61
157
assert .ElementsMatch (t , []string {"Lang1" , "Lang2" }, descs )
77
173
` )
78
174
79
175
ls := NewLocaleStore ()
80
- require .NoError (t , ls .AddLocaleByIni ("lang1" , "Lang1" , testData1 , testData2 ))
176
+ require .NoError (t , ls .AddLocaleByIni ("lang1" , "Lang1" , MockPluralRule , testData1 , testData2 ))
81
177
lang1 , _ := ls .Locale ("lang1" )
82
178
assert .Equal (t , "11" , lang1 .TrString ("a" ))
83
179
assert .Equal (t , "21" , lang1 .TrString ("b" ))
@@ -118,7 +214,7 @@ func (e *errorPointerReceiver) Error() string {
118
214
119
215
func TestLocaleWithTemplate (t * testing.T ) {
120
216
ls := NewLocaleStore ()
121
- require .NoError (t , ls .AddLocaleByIni ("lang1" , "Lang1" , []byte (`key=<a>%s</a>` ), nil ))
217
+ require .NoError (t , ls .AddLocaleByIni ("lang1" , "Lang1" , MockPluralRule , []byte (`key=<a>%s</a>` ), nil ))
122
218
lang1 , _ := ls .Locale ("lang1" )
123
219
124
220
tmpl := template .New ("test" ).Funcs (template.FuncMap {"tr" : lang1 .TrHTML })
@@ -181,7 +277,7 @@ func TestLocaleStoreQuirks(t *testing.T) {
181
277
182
278
for _ , testData := range testDataList {
183
279
ls := NewLocaleStore ()
184
- err := ls .AddLocaleByIni ("lang1" , "Lang1" , []byte ("a=" + testData .in ), nil )
280
+ err := ls .AddLocaleByIni ("lang1" , "Lang1" , nil , []byte ("a=" + testData .in ), nil )
185
281
lang1 , _ := ls .Locale ("lang1" )
186
282
require .NoError (t , err , testData .hint )
187
283
assert .Equal (t , testData .out , lang1 .TrString ("a" ), testData .hint )
0 commit comments