-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
73 lines (61 loc) · 1.67 KB
/
main.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
package main
import (
"strings"
"text/template"
"github.com/kataras/iris/v12"
// go get -u github.com/gertd/go-pluralize
"github.com/gertd/go-pluralize"
)
/*
Iris I18n supports text/template inside the translation values.
Follow this example to learn how to use that feature.
*/
func main() {
app := newApp()
app.Listen(":8080")
}
func newApp() *iris.Application {
app := iris.New()
pluralize := pluralize.NewClient()
// Set custom functions per locale!
app.I18n.Loader.Funcs = func(current iris.Locale) template.FuncMap {
return template.FuncMap{
"plural": func(word string, count int) string {
// Your own implementation or use a 3rd-party package
// like we do here.
//
// Note that this is only for english,
// but you can use the "current" locale
// and make a map with dictionaries to
// pluralize words based on the given language.
return pluralize.Pluralize(word, count, true)
},
"uppercase": func(word string) string {
return strings.ToUpper(word)
},
"concat": func(words ...string) string {
return strings.Join(words, " ")
},
}
}
app.I18n.Load("./locales/*/*", "en-US", "el-GR")
app.Get("/", func(ctx iris.Context) {
text := ctx.Tr("HiDogs", iris.Map{
"count": 2,
}) // en-US: prints "Hi 2 dogs".
ctx.WriteString(text)
})
app.Get("/singular", func(ctx iris.Context) {
text := ctx.Tr("HiDogs", iris.Map{
"count": 1,
}) // en-US: prints "Hi 1 dog".
ctx.WriteString(text)
})
app.Get("/members", func(ctx iris.Context) {
text := ctx.Tr("forms.registered_members", iris.Map{
"count": 42,
}) // en-US: prints "There are 42 members registered".
ctx.WriteString(text)
})
return app
}