forked from gofiber/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.go
138 lines (118 loc) · 3.26 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package fiberi18n
import (
"fmt"
"github.com/gofiber/fiber/v2/log"
"path"
"sync"
"github.com/gofiber/fiber/v2"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
const localsKey = "fiberi18n"
// New creates a new middleware handler
func New(config ...*Config) fiber.Handler {
cfg := configDefault(config...)
// init bundle
bundle := i18n.NewBundle(cfg.DefaultLanguage)
bundle.RegisterUnmarshalFunc(cfg.FormatBundleFile, cfg.UnmarshalFunc)
cfg.bundle = bundle
cfg.loadMessages()
cfg.initLocalizerMap()
return func(c *fiber.Ctx) error {
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
c.Locals(localsKey, cfg)
return c.Next()
}
}
func (c *Config) loadMessage(filepath string) {
buf, err := c.Loader.LoadMessage(filepath)
if err != nil {
panic(err)
}
if _, err := c.bundle.ParseMessageFileBytes(buf, filepath); err != nil {
panic(err)
}
}
func (c *Config) loadMessages() *Config {
for _, lang := range c.AcceptLanguages {
bundleFilePath := fmt.Sprintf("%s.%s", lang.String(), c.FormatBundleFile)
filepath := path.Join(c.RootPath, bundleFilePath)
c.loadMessage(filepath)
}
return c
}
func (c *Config) initLocalizerMap() {
localizerMap := &sync.Map{}
for _, lang := range c.AcceptLanguages {
s := lang.String()
localizerMap.Store(s, i18n.NewLocalizer(c.bundle, s))
}
lang := c.DefaultLanguage.String()
if _, ok := localizerMap.Load(lang); !ok {
localizerMap.Store(lang, i18n.NewLocalizer(c.bundle, lang))
}
c.mu.Lock()
c.localizerMap = localizerMap
c.mu.Unlock()
}
/*
MustLocalize get the i18n message without error handling
param is one of these type: messageID, *i18n.LocalizeConfig
Example:
MustLocalize(ctx, "hello") // messageID is hello
MustLocalize(ctx, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
})
*/
func MustLocalize(ctx *fiber.Ctx, params interface{}) string {
message, err := Localize(ctx, params)
if err != nil {
panic(err)
}
return message
}
/*
Localize get the i18n message
param is one of these type: messageID, *i18n.LocalizeConfig
Example:
Localize(ctx, "hello") // messageID is hello
Localize(ctx, &i18n.LocalizeConfig{
MessageID: "welcomeWithName",
TemplateData: map[string]string{
"name": context.Param("name"),
},
})
*/
func Localize(ctx *fiber.Ctx, params interface{}) (string, error) {
local := ctx.Locals(localsKey)
if local == nil {
return "", fmt.Errorf("i18n.Localize error: %v", "Config is nil")
}
appCfg, ok := local.(*Config)
if !ok {
return "", fmt.Errorf("i18n.Localize error: %v", "Config is not *Config type")
}
lang := appCfg.LangHandler(ctx, appCfg.DefaultLanguage.String())
localizer, _ := appCfg.localizerMap.Load(lang)
if localizer == nil {
defaultLang := appCfg.DefaultLanguage.String()
localizer, _ = appCfg.localizerMap.Load(defaultLang)
}
var localizeConfig *i18n.LocalizeConfig
switch paramValue := params.(type) {
case string:
localizeConfig = &i18n.LocalizeConfig{MessageID: paramValue}
case *i18n.LocalizeConfig:
localizeConfig = paramValue
}
message, err := localizer.(*i18n.Localizer).Localize(localizeConfig)
if err != nil {
log.Errorf("i18n.Localize error: %v", err)
return "", fmt.Errorf("i18n.Localize error: %v", err)
}
return message, nil
}