-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
131 lines (110 loc) · 2.87 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"golang.org/x/text/language"
"github.com/vorlif/spreak"
"github.com/vorlif/spreak/catalog"
)
func main() {
// We want spreak to load our .json files here, so we create a FilesystemLoader here with our own decoder.
fsLoader, errFS := spreak.NewFilesystemLoader(
spreak.WithDecoder(".json", jsonDecoder{}),
spreak.WithPath("./"),
)
if errFS != nil {
panic(errFS)
}
bundle, err := spreak.NewBundle(
spreak.WithDomainLoader(spreak.NoDomain, fsLoader),
spreak.WithRequiredLanguage(language.Spanish, language.English),
spreak.WithLanguage(language.German, language.French),
)
if err != nil {
panic(err)
}
fmt.Println(bundle.SupportedLanguages())
t := spreak.NewLocalizer(bundle, language.Spanish)
fmt.Println(t.Get("hello.world"))
fmt.Println(t.NGetf("dog", "dog", 2, 2))
fmt.Println(t.NGetf("cat", "cats", 2, 2))
t = spreak.NewLocalizer(bundle, language.English)
fmt.Println(t.Get("hello.world"))
fmt.Println(t.NGetf("dog", "dogs", 2, 2))
// Output:
// [es en]
// Hola Mundo
// Tengo 2 perros
// cats
// Hello world
// I have 2 dogs
}
// We create our own decoder which designs a JSON catalog from the content of the JSON files.
type jsonDecoder struct{}
var _ catalog.Decoder = (*jsonDecoder)(nil)
func (jsonDecoder) Decode(lang language.Tag, domain string, data []byte) (catalog.Catalog, error) {
catl := NewJsonCatalog(lang, domain)
if err := json.Unmarshal(data, &catl.translations); err != nil {
return nil, err
}
return catl, nil
}
// We create a catalog for our JSON files.
type jsonCatalog struct {
language language.Tag
domain string
translations map[string]string
}
func NewJsonCatalog(lang language.Tag, domain string) *jsonCatalog {
return &jsonCatalog{
language: lang,
domain: domain,
translations: make(map[string]string),
}
}
var _ catalog.Catalog = (*jsonCatalog)(nil)
func (c *jsonCatalog) GetTranslation(ctx, msgID string) (string, error) {
return c.GetPluralTranslation(ctx, msgID, 1)
}
func (c *jsonCatalog) GetPluralTranslation(ctx, msgID string, n interface{}) (string, error) {
if ctx != "" {
err := &catalog.ErrMissingContext{
Language: c.language,
Domain: c.domain,
Context: ctx,
}
return "", err
}
// Common plural form:
// n == 1 -> singular
// n != 1 -> plural
idx := 0
if n != 1 {
idx = 1
msgID += "_plural"
}
if _, hasMsg := c.translations[msgID]; !hasMsg {
err := &catalog.ErrMissingMessageID{
Language: c.language,
Domain: c.domain,
Context: ctx,
MsgID: msgID,
}
return "", err
}
translation := c.translations[msgID]
if translation == "" {
err := &catalog.ErrMissingTranslation{
Language: c.language,
Domain: c.domain,
Context: ctx,
MsgID: msgID,
Idx: idx,
}
return "", err
}
return translation, nil
}
func (c *jsonCatalog) Language() language.Tag {
return c.language
}