-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
77 lines (65 loc) · 1.84 KB
/
simple.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
package main
import (
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"fmt"
"encoding/json"
)
func main() {
// Step 1: Create bundle
bundle := &i18n.Bundle{DefaultLanguage: language.English}
// Step 2: Create localizer for that bundle using one or more language tags
loc := i18n.NewLocalizer(bundle, language.English.String())
// Step 3: Define messages
messages := &i18n.Message{
ID: "Emails",
Description: "The number of unread emails a user has",
One: "{{.Name}} has {{.Count}} email.",
Other: "{{.Name}} has {{.Count}} emails.",
}
// Step 3: Localize Messages
messagesCount := 2
translation := loc.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: messages,
TemplateData: map[string]interface{}{
"Name": "Theo",
"Count": messagesCount,
},
PluralCount: messagesCount,
})
fmt.Println(translation)
// Define different delimiters
messages = &i18n.Message{
ID: "Notifications",
Description: "The number of unread notifications a user has",
One: "<<.Name>> has <<.Count>> notification.",
Other: "<<.Name>> has <<.Count>> notifications.",
LeftDelim: "<<",
RightDelim: ">>",
}
notificationsCount := 1
translation = loc.MustLocalize(&i18n.LocalizeConfig{
DefaultMessage: messages,
TemplateData: map[string]interface{}{
"Name": "Nick",
"Count": notificationsCount,
},
PluralCount: notificationsCount,
})
fmt.Println(translation)
// Unmarshaling from files
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
bundle.MustLoadMessageFile("en.json")
bundle.MustLoadMessageFile("el.json")
loc = i18n.NewLocalizer(bundle, "el")
messagesCount = 10
translation = loc.MustLocalize(&i18n.LocalizeConfig{
MessageID: "messages",
TemplateData: map[string]interface{}{
"Name": "Alex",
"Count": messagesCount,
},
PluralCount: messagesCount,
})
fmt.Println(translation)
}