-
Notifications
You must be signed in to change notification settings - Fork 5
/
example_test.go
236 lines (188 loc) · 5.16 KB
/
example_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package i18n_test
import (
"fmt"
"github.com/yext/i18n"
)
func ExampleNewTranslatorFactory() {
// creates a new Factory using "en" as the global fallback locale
f, _ := i18n.NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
_ = f
}
func ExampleTranslatorFactory_GetTranslator() {
f, _ := i18n.NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
// gets a translator for the Canadian French locale
tFrCa, _ := f.GetTranslator("fr-ca")
_ = tFrCa
}
func ExampleTranslator_Translate() {
f, _ := i18n.NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
tEn, _ := f.GetTranslator("en")
// performs 2 translations, one with a substitution
t1, _ := tEn.Translate("WELCOME", map[string]string{})
t2, _ := tEn.Translate("WELCOME_USER", map[string]string{"user": "Mother Goose"})
fmt.Printf("Basic Translation : %s\n", t1)
fmt.Printf("Substitution : %s\n", t2)
// Output:
// Basic Translation : Welcome!
// Substitution : Welcome, Mother Goose!
}
func ExampleTranslator_Pluralize() {
f, _ := i18n.NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
tFr, _ := f.GetTranslator("fr")
// performs 4 plural translations
p1, _ := tFr.Pluralize("UNIT_DAY", 0, "0")
p2, _ := tFr.Pluralize("UNIT_DAY", 0.5, "0,5")
p3, _ := tFr.Pluralize("UNIT_DAY", 1, "one")
p4, _ := tFr.Pluralize("UNIT_DAY", 2000, "2K")
fmt.Printf("Plurilization : %s\n", p1)
fmt.Printf("Plurilization : %s\n", p2)
fmt.Printf("Plurilization : %s\n", p3)
fmt.Printf("Plurilization : %s\n", p4)
// Output:
// Plurilization : 0 jour
// Plurilization : 0,5 jour
// Plurilization : one jour
// Plurilization : 2K jours
}
func ExampleTranslator_FormatCurrency() {
f, _ := i18n.NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
tEn, _ := f.GetTranslator("en")
// performs 2 currency formats - one positive, one negative
c1, _ := tEn.FormatCurrency(12345000000000.6789, "USD")
c2, _ := tEn.FormatCurrency(-12345000000000.6789, "USD")
fmt.Printf("Currency : %s\n", c1)
fmt.Printf("Currency : %s\n", c2)
// Output:
// Currency : $12,345,000,000,000.68
// Currency : ($12,345,000,000,000.68)
}
func ExampleTranslator_FormatNumber() {
f, _ := i18n.NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
tEn, _ := f.GetTranslator("en")
// performs 2 number formats - one positive, one negative
n1 := tEn.FormatNumber(12345000000000.6789)
n2 := tEn.FormatNumber(-12345000000000.6789)
fmt.Printf("Number : %s\n", n1)
fmt.Printf("Number : %s\n", n2)
// Output:
// Number : 12,345,000,000,000.679
// Number : -12,345,000,000,000.679
}
func ExampleTranslator_FormatPercent() {
f, _ := i18n.NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
tEn, _ := f.GetTranslator("en")
// performs 3 percent formats
p1 := tEn.FormatPercent(0.01234)
p2 := tEn.FormatPercent(0.5678)
p3 := tEn.FormatPercent(12.34)
fmt.Printf("Percent : %s\n", p1)
fmt.Printf("Percent : %s\n", p2)
fmt.Printf("Percent : %s\n", p3)
// Output:
// Percent : 1%
// Percent : 57%
// Percent : 1,234%
}
type Food struct {
Name string
}
func ExampleTranslator_Sort() {
f, _ := i18n.NewTranslatorFactory(
[]string{"data/rules"},
[]string{"data/messages"},
"en",
)
tEn, _ := f.GetTranslator("en")
toSort := []interface{}{
Food{Name: "apple"},
Food{Name: "beet"},
Food{Name: "carrot"},
Food{Name: "ȧpricot"},
Food{Name: "ḃanana"},
Food{Name: "ċlementine"},
}
fmt.Printf("Before Sort : %v\n", toSort)
// sorts the food list
tEn.Sort(toSort, func(i interface{}) string {
if food, ok := i.(Food); ok {
return food.Name
}
return ""
})
fmt.Printf("After Sort : %v\n", toSort)
// Output:
// Before Sort : [{apple} {beet} {carrot} {ȧpricot} {ḃanana} {ċlementine}]
// After Sort : [{apple} {ȧpricot} {ḃanana} {beet} {carrot} {ċlementine}]
}
func ExampleSortUniversal() {
toSort := []interface{}{
Food{Name: "apple"},
Food{Name: "beet"},
Food{Name: "carrot"},
Food{Name: "ȧpricot"},
Food{Name: "ḃanana"},
Food{Name: "ċlementine"},
}
fmt.Printf("Before Sort : %v\n", toSort)
// sorts the list
i18n.SortUniversal(toSort, func(i interface{}) string {
if food, ok := i.(Food); ok {
return food.Name
}
return ""
})
fmt.Printf("After Sort : %v\n", toSort)
// Output:
// Before Sort : [{apple} {beet} {carrot} {ȧpricot} {ḃanana} {ċlementine}]
// After Sort : [{apple} {ȧpricot} {beet} {ḃanana} {carrot} {ċlementine}]
}
func ExampleSortLocal() {
toSort := []interface{}{
Food{Name: "apple"},
Food{Name: "beet"},
Food{Name: "carrot"},
Food{Name: "ȧpricot"},
Food{Name: "ḃanana"},
Food{Name: "ċlementine"},
}
fmt.Printf("Before Sort : %v\n", toSort)
// sorts the list
i18n.SortLocal("en", toSort, func(i interface{}) string {
if food, ok := i.(Food); ok {
return food.Name
}
return ""
})
fmt.Printf("After Sort : %v\n", toSort)
// Output:
// Before Sort : [{apple} {beet} {carrot} {ȧpricot} {ḃanana} {ċlementine}]
// After Sort : [{apple} {ȧpricot} {ḃanana} {beet} {carrot} {ċlementine}]
}