-
Notifications
You must be signed in to change notification settings - Fork 1
/
locale.go
62 lines (51 loc) · 1.29 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
package validation
import (
enTemplates "github.com/tiendc/go-validator/internal/templates/en"
)
const (
langEn = "en"
)
// TemplateProvider interface for providing template for generating error messages
type TemplateProvider interface {
Get(Error) string
}
type templateProvider struct {
fmtTemplates map[string]string
}
func (t *templateProvider) Get(e Error) string {
key := e.ValueType()
if key != "" {
key += "_"
}
key += e.Type()
return t.fmtTemplates[key]
}
var (
defaultLang = langEn
fmtTemplates = map[string]TemplateProvider{
langEn: &templateProvider{fmtTemplates: enTemplates.FmtTemplates},
}
)
// DefaultLang default language used in the template
func DefaultLang() string {
return defaultLang
}
// SetDefaultLang set default language used in the template
func SetDefaultLang(lang string) {
defaultLang = lang
}
// GetTemplateProvider gets current template provider
func GetTemplateProvider(lang string) TemplateProvider {
return fmtTemplates[lang]
}
// SetTemplateProvider sets current template provider
func SetTemplateProvider(lang string, provider TemplateProvider) {
fmtTemplates[lang] = provider
}
func getFmtTemplate(e Error) string {
provider := GetTemplateProvider(defaultLang)
if provider == nil {
provider = GetTemplateProvider(langEn)
}
return provider.Get(e)
}