-
Notifications
You must be signed in to change notification settings - Fork 0
/
localizer.go
51 lines (42 loc) · 1.01 KB
/
localizer.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
package yeahapi
import (
// _ "github.com/yeahuz/yeah-api/internal/translations"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
type Localizer struct {
fallback string
printers []printer
}
type printer struct {
code string
printer *message.Printer
}
type LocalizerService interface {
Get(args ...string) *printer
}
func NewLocalizerService(fallback string) *Localizer {
return &Localizer{
printers: []printer{
{code: "en", printer: message.NewPrinter(language.MustParse("en"))},
{code: "ru", printer: message.NewPrinter(language.MustParse("ru"))},
{code: "uz", printer: message.NewPrinter(language.MustParse("uz"))},
},
fallback: fallback,
}
}
func (l *Localizer) Get(args ...string) (p *printer) {
locale := l.fallback
if len(args) > 0 {
locale = args[0]
}
for _, printer := range l.printers {
if printer.code == locale {
p = &printer
}
}
return p
}
func (p *printer) T(key message.Reference, args ...interface{}) string {
return p.printer.Sprintf(key, args...)
}