This repository has been archived by the owner on Feb 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
105 lines (87 loc) · 2.3 KB
/
helper.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
package i18n
import (
"errors"
"fmt"
"github.com/volatile/core"
"golang.org/x/text/language"
)
const contextDataKey = "i18nLocale"
// TnPlaceholder is the placeholder replaced by n in a translation, when using the TransN function.
const TnPlaceholder = "{{.n}}"
var (
locales Locales
defaultLocale language.Tag
matcher language.Matcher
)
// Errors
var (
ErrUnknownLocale = errors.New("i18n: unknown locale")
)
// TemplatesFuncs provides i18n functions that can be set for templates.
var TemplatesFuncs = map[string]interface{}{
"clientLocale": ClientLocale,
"fmtn": Fmtn,
"t": T,
"ht": HT,
"tn": Tn,
"htn": HTn,
}
// Translations is a map of translations.
type Translations map[string]string
// Locales is a map of translations associated to language tags.
type Locales map[language.Tag]Translations
// Has checks if locale t exists in the locales map.
func (ll *Locales) Has(t language.Tag) bool {
_, ok := (*ll)[t]
return ok
}
// Init registers locales ll and default locale def for the entire app.
func Init(ll Locales, def language.Tag) {
if locales != nil {
panic(errors.New("i18n: Init called multiple times"))
}
locales = ll
defaultLocale = def
if !locales.Has(def) {
panic(fmt.Errorf("i18n: default locale %q doesn't exist", def))
}
tt := []language.Tag{def}
for t := range ll {
if t != def {
tt = append(tt, t)
}
}
matcher = language.NewMatcher(tt)
}
// Use adds the handler to the default handlers stack.
// It matches locale for client, thanks to matchers.
// Multiple matchers can be used.
// The client locale is set as soon as a matcher is confident.
func Use(matchers ...Matcher) {
core.Use(func(c *core.Context) {
for _, m := range matchers {
if t, conf := m(c); conf != language.No {
if err := SetClientLocale(c, t); err == nil {
break
}
}
}
c.Next()
})
}
// CleanAcceptLanguage parses, cleans and returns the contents of a Accept-Language header.
// If an error is encountered, the returned string is the same as given.
func CleanAcceptLanguage(s string) (string, error) {
tt, q, err := language.ParseAcceptLanguage(s)
if err != nil {
return s, err
}
s = ""
for i := 0; i < len(tt); i++ {
if i > 0 {
s += ","
}
s += fmt.Sprintf("%s;q=%g", tt[i].String(), q[i])
}
return s, nil
}