-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
62 lines (50 loc) · 1.1 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
package main
import (
"embed"
"fmt"
"github.com/kataras/i18n"
)
//go:embed static/locales/*
var staticFS embed.FS
type user struct {
Name string
Age int
}
func main() {
loaderOpts := i18n.LoaderConfig{
Left: "{{",
Right: "}}",
Strict: false,
DefaultMessageFunc: nil,
Funcs: nil,
}
loader, err := i18n.FS(staticFS, "./static/locales/*/*.yml", loaderOpts)
if err != nil {
panic(err)
}
I18n, err := i18n.New(loader, "en-US", "el-GR", "zh-CN")
if err != nil {
panic(err)
}
//
// The rest as expected...
//
// Fmt style.
enText := I18n.Tr("en", "hi", "John Doe") // or "en-US"
elText := I18n.Tr("el", "hi", "John Doe")
zhText := I18n.Tr("zh", "hi", "John Doe")
fmt.Println(enText)
fmt.Println(elText)
fmt.Println(zhText)
// Templates style.
templateData := user{
Name: "John Doe",
Age: 66,
}
enText = I18n.Tr("en-US", "intro", templateData) // or "en"
elText = I18n.Tr("el-GR", "intro", templateData)
zhText = I18n.Tr("zh-CN", "intro", templateData)
fmt.Println(enText)
fmt.Println(elText)
fmt.Println(zhText)
}