-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.go
68 lines (58 loc) · 1.89 KB
/
locale.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
package i18n
import (
"bytes"
"fmt"
)
// Locale represents a translated locale.
type Locale struct {
parent *I18n
locale string
}
// Locale returns the current locale name.
func (l *Locale) Locale() string {
return l.locale
}
// String returns a translated string.
func (l *Locale) String(name string, data ...any) string {
selectedTrans := l.lookup(name)
return l.render(selectedTrans.texts[0], data...)
}
// StringX returns a translated string with a specified context.
func (l *Locale) StringX(name, context string, data ...any) string {
return l.String(fmt.Sprintf("%s <%s>", name, context), data...)
}
// Number returns a translated string based on the `count`.
func (l *Locale) Number(name string, count int, data ...any) string {
selectedTrans := l.lookup(name)
selectedIndex := selectedTrans.pluralizor(count, len(selectedTrans.texts))
return l.render(selectedTrans.texts[selectedIndex], data...)
}
// NumberX returns a translated string based on the `count` with a specified context.
func (l *Locale) NumberX(name string, context string, count int, data ...any) string {
return l.Number(fmt.Sprintf("%s <%s>", name, context), count, data...)
}
// lookup
func (l *Locale) lookup(name string) *compiledTranslation {
if selectedTrans, ok := l.parent.compiledTranslations[l.locale][name]; ok {
return selectedTrans
}
runtimeTrans, ok := l.parent.runtimeCompiledTranslations[name]
if !ok {
runtimeTrans = l.parent.compileTranslation(l.parent.defaultLocale, name, trimContext(name))
}
l.parent.runtimeCompiledTranslations[name] = runtimeTrans
return runtimeTrans
}
// render
func (l *Locale) render(text *compiledText, data ...any) string {
if text.tmpl != nil {
var tpl bytes.Buffer
if len(data) > 0 {
text.tmpl.Execute(&tpl, data[0])
} else {
text.tmpl.Execute(&tpl, nil)
}
return tpl.String()
}
return text.text
}