-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
63 lines (53 loc) · 1.51 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
package main
import (
"embed"
"strings"
"text/template"
"github.com/kataras/iris/v12"
)
//go:embed embedded/locales/*
var embeddedFS embed.FS
func main() {
app := newApp()
// http://localhost:8080
// http://localhost:8080?lang=el
// http://localhost:8080?lang=el
// http://localhost:8080?lang=el-GR
// http://localhost:8080?lang=en
// http://localhost:8080?lang=en-US
//
// http://localhost:8080/title
// http://localhost:8080/title?lang=el-GR
// ...
app.Listen(":8080")
}
func newApp() *iris.Application {
app := iris.New()
// Set custom functions per locale!
app.I18n.Loader.Funcs = func(current iris.Locale) template.FuncMap {
return template.FuncMap{
"uppercase": func(word string) string {
return strings.ToUpper(word)
},
}
}
// Instead of:
// err := app.I18n.Load("./locales/*/*.ini", "en-US", "el-GR")
// apply the below in order to build with embedded locales inside your executable binary.
err := app.I18n.LoadFS(embeddedFS, "./embedded/locales/*/*.ini", "en-US", "el-GR")
if err != nil {
panic(err)
} // OR to load all languages by filename:
// app.I18n.LoadFS(embeddedFS, "./embedded/locales/*/*.ini")
// Then set the default language using:
// app.I18n.SetDefault("en-US")
app.Get("/", func(ctx iris.Context) {
text := ctx.Tr("forms.register") // en-US: prints "Become a MEMBER".
ctx.WriteString(text)
})
app.Get("/title", func(ctx iris.Context) {
text := ctx.Tr("user.connections.Title") // en-US: prints "Accounts Connections".
ctx.WriteString(text)
})
return app
}