forked from gin-contrib/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.go
68 lines (58 loc) · 1.46 KB
/
i18n.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
package i18n
import (
"github.com/gin-gonic/gin"
)
var atI18n GinI18n
// newI18n ...
func newI18n(opts ...Option) {
// init ins
ins := &ginI18nImpl{}
// set ins property from opts
for _, opt := range opts {
opt(ins)
}
// if bundle isn't constructed then assign it from default
if ins.bundle == nil {
ins.setBundle(defaultBundleConfig)
}
// if getLngHandler isn't constructed then assign it from default
if ins.getLngHandler == nil {
ins.getLngHandler = defaultGetLngHandler
}
atI18n = ins
}
// Localize ...
func Localize(opts ...Option) gin.HandlerFunc {
newI18n(opts...)
return func(context *gin.Context) {
atI18n.setCurrentContext(context)
}
}
/*GetMessage get the i18n message
param is one of these type: messageID, *i18n.LocalizeConfig
Example:
GetMessage("hello") // messageID is hello
GetMessage(&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
})
*/
func GetMessage(param interface{}) (string, error) {
return atI18n.getMessage(param)
}
/*MustGetMessage get the i18n message without error handling
param is one of these type: messageID, *i18n.LocalizeConfig
Example:
MustGetMessage("hello") // messageID is hello
MustGetMessage(&i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
})
*/
func MustGetMessage(param interface{}) string {
return atI18n.mustGetMessage(param)
}