generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.go
51 lines (42 loc) · 1.19 KB
/
middleware.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 i18nmiddleware
import (
"clevergo.tech/clevergo"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
type contextKey int
// LocalizerKey is the context key of localizer.
const LocalizerKey contextKey = 0
// Option is a function for setting middleware.
type Option func(*middleware)
// WithFormField is an option for setting form field.
func WithFormField(field string) Option {
return func(m *middleware) {
m.formField = field
}
}
type middleware struct {
bundle *i18n.Bundle
formField string
}
func (m *middleware) handle(next clevergo.Handle) clevergo.Handle {
return func(c *clevergo.Context) error {
localizer := i18n.NewLocalizer(m.bundle, c.FormValue(m.formField), c.GetHeader("Accept-Language"))
c.WithValue(LocalizerKey, localizer)
return next(c)
}
}
// New returns a I18N middleware with the given bundle and optional options.
func New(bundle *i18n.Bundle, opts ...Option) clevergo.MiddlewareFunc {
m := &middleware{
bundle: bundle,
formField: "lang",
}
for _, opt := range opts {
opt(m)
}
return m.handle
}
// Localizer returns the localizer instance from context.
func Localizer(c *clevergo.Context) *i18n.Localizer {
return c.Value(LocalizerKey).(*i18n.Localizer)
}