-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathut.go
90 lines (72 loc) · 2.11 KB
/
ut.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
// Copyright 2018 Sergey Novichkov. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
package ut
import (
"github.com/gozix/di"
"github.com/gozix/glue/v3"
"github.com/go-playground/locales"
"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
)
type (
// Bundle implements the glue.Bundle interface.
Bundle struct {
fallback locales.Translator
locales []locales.Translator
}
// Configurator configures universal translator after its creation.
Configurator func(*ut.UniversalTranslator) error
)
// Bundle implements the glue.Bundle interface.
var _ glue.Bundle = (*Bundle)(nil)
// BundleName is default definition name.
const BundleName = "universal-translator"
// NewBundle create bundle instance.
func NewBundle(options ...Option) *Bundle {
var (
locale = en.New()
bundle = Bundle{
fallback: locale,
locales: []locales.Translator{locale},
}
)
for _, option := range options {
option.apply(&bundle)
}
return &bundle
}
func (b *Bundle) Name() string {
return BundleName
}
func (b *Bundle) Build(builder di.Builder) error {
return builder.Provide(
b.provideUT,
di.Constraint(0, di.Optional(true), withTranslator(false)),
di.Constraint(1, di.Optional(true), withTranslator(true)),
di.Constraint(2, di.Optional(true), withConfigurator()),
)
}
func (b *Bundle) provideUT(append []locales.Translator, override []locales.Translator, configurators []Configurator) (_ *ut.UniversalTranslator, err error) {
var translator = ut.New(b.fallback, b.locales...)
for _, localeTranslator := range append {
if err = translator.AddTranslator(localeTranslator, false); err != nil {
return nil, err
}
}
for _, localeTranslator := range override {
if err = translator.AddTranslator(localeTranslator, true); err != nil {
return nil, err
}
}
for _, configure := range configurators {
if err = configure(translator); err != nil {
return nil, err
}
}
return translator, nil
}
// apply implements Option.
func (f optionFunc) apply(bundle *Bundle) {
f(bundle)
}