-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.go
59 lines (48 loc) · 1.46 KB
/
group.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
package i18n
import (
"fmt"
"golang.org/x/text/language"
)
// Group is a translation subgroup
type Group struct {
i18n *I18n
group string
}
func (group *Group) key(k string) string {
return fmt.Sprintf("%s.%s", group.group, k)
}
// Group gets a translation group
func (group *Group) Group(key string) *Group {
return &Group{
i18n: group.i18n,
group: group.key(key),
}
}
// GenerateHelper generates a method that allways gets tags in a certain language
// This is usefull for passing to the template engine
func (group *Group) GenerateHelper(tag language.Tag) T {
return T(func(key string) string {
return group.T(tag, group.key(key))
})
}
// T is a helper method to get translation by lang string or language tag
func (group *Group) T(lang interface{}, key string) string {
return group.i18n.T(lang, group.key(key))
}
// GetWithLangString parses the lang string before lookip up the translation
func (group *Group) GetWithLangString(lang string, key string) (*Translation, error) {
return group.i18n.GetWithLangString(lang, group.key(key))
}
// Get translation
func (group *Group) Get(lang language.Tag, key string) *Translation {
return group.i18n.Get(lang, group.key(key))
}
// Add translation
func (group *Group) Add(translation *Translation) error {
translation.Key = group.key(translation.Key)
return group.i18n.Add(translation)
}
// Delete translation
func (group *Group) Delete(translation *Translation) error {
return group.i18n.Delete(translation)
}