Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
96 lines (85 loc) · 3.6 KB
/
Copy pathmain.go
File metadata and controls
96 lines (85 loc) · 3.6 KB
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
// Internationalization: one message catalog, one language per request.
//
// i18n.New() creates the catalog and Load fills it from a glob pattern. The
// language of each file is deduced from its name (locales/en-US.yml) or from a
// directory named after it; the first language listed is the default and the
// fallback. INI, JSON, TOML and YAML files are supported.
//
// i18n.NewHandler installs the per-request detection. It checks its sources in
// a fixed order - a server-set language, ExtractFunc, URLParameter, Cookie,
// Subdomain, then the Accept-Language header, which is always consulted - and
// falls back to the default locale. Matching is lenient: "el", "el-gr" and
// "el-GR" all resolve to the registered el-GR.
//
// Handlers translate with i18n.GetMessage(ctx, key, args...) or through the
// locale returned by i18n.GetLocale(ctx).
//
// Learn: load locale files, detect the language of each request, and translate
// plain, plural and variable messages.
// Before: ../cache
//
// Run from this directory, so ./locales resolves.
//
// Run:
//
// go run main.go
//
// Try:
//
// curl http://localhost:8080/ # [en-US] hello, iris
// curl -H "Accept-Language: el" http://localhost:8080/ # [el-GR] γεια, iris
// curl "http://localhost:8080/days/1?lang=el-GR" # Έχεις μία ελεύθερη μέρα.
// curl "http://localhost:8080/days/5?lang=el-GR" # Έχεις 5 ελεύθερες μέρες.
// curl http://localhost:8080/late/1 # You are 1 minute late.
// curl http://localhost:8080/late/10 # You are 10 minutes late.
//
// Next: ../../07-dependency-injection/constructors
package main
import (
"log"
"github.com/kataras/iris/v14"
"github.com/kataras/iris/v14/middleware/i18n"
)
func main() {
newApp().Listen(":8080")
}
func newApp() *iris.Application {
app := iris.New()
locales := i18n.New()
// One glob, both files. LoadFS(embed.FS, ...) compiles them into the
// binary instead, LoadKV loads them from a map.
if err := locales.Load("./locales/*", "en-US", "el-GR"); err != nil {
log.Fatal(err)
}
// Every detection source is opt-in except Accept-Language. Cookie: "lang"
// and Subdomain: true are the other two; i18n.NewRouter, registered with
// UseRouter, additionally strips language path prefixes such as
// /el-GR/days/5 before routing.
app.Use(i18n.NewHandler(locales, i18n.HandlerOptions{
URLParameter: "lang",
}))
app.Get("/", func(ctx iris.Context) {
// The matched locale: Language(), Tag(), Index() and GetMessage.
locale := i18n.GetLocale(ctx)
// Plain messages are rendered with printf verbs, indexed ones (%[1]s)
// included, so a translation can move the value elsewhere.
ctx.Writef("[%s] %s", locale.Language(), locale.GetMessage("hi", "iris"))
})
// A plural message. The count is the first argument: it selects the form
// and it also feeds that form's printf verbs.
app.Get("/days/{count:int}", func(ctx iris.Context) {
count := ctx.Params().GetInt("count")
// i18n.GetMessage is the shortcut for GetLocale(ctx).GetMessage.
ctx.WriteString(i18n.GetMessage(ctx, "FreeDay", count))
})
// ${Minutes} is a Vars entry with plural forms of its own, selected by the
// same count: "1 minute late", "10 minutes late".
app.Get("/late/{count:int}", func(ctx iris.Context) {
count := ctx.Params().GetInt("count")
ctx.WriteString(i18n.GetMessage(ctx, "YouLate", count))
})
// A message containing {{ and }} is parsed as a Go text/template instead,
// with the first argument as its data: i18n.GetMessage(ctx, "welcome",
// iris.Map{"name": "Makis"}) renders "Welcome back, {{.name}}!".
return app
}