-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.go
207 lines (177 loc) · 5.86 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package echoi18n
import (
"fmt"
"os"
"path"
"sync"
"github.com/labstack/echo/v4"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"
)
// localsKey is the key used to store the i18n Config in the Echo Context.
const localsKey = "echoi18n"
// Config holds the configuration for the i18n middleware.
type Config struct {
DefaultLanguage language.Tag // Default language to use if no language is determined.
AcceptLanguages []language.Tag // Supported languages.
FormatBundleFile string // File format for message bundles.
Loader Loader // Loader interface to load message files.
RootPath string // Root directory path for message files.
LangHandler func(echo.Context, string) string // Language handler function.
bundle *i18n.Bundle // i18n message bundle.
localizerMap *sync.Map // Map of localizers for each language.
mu sync.Mutex // Mutex for thread safety.
UnmarshalFunc i18n.UnmarshalFunc // Function to unmarshal message files.
}
// Loader is the interface for loading message files.
type Loader interface {
LoadMessage(path string) ([]byte, error)
}
// LoaderFunc is a function type that implements the Loader interface.
type LoaderFunc func(path string) ([]byte, error)
// LoadMessage loads a message file using the LoaderFunc.
func (f LoaderFunc) LoadMessage(path string) ([]byte, error) {
return f(path)
}
// loadMessage loads a single message file for a given language.
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)
}
}
// loadMessages loads all message files for the supported languages.
func (c *Config) loadMessages() {
for _, lang := range c.AcceptLanguages {
bundleFilePath := fmt.Sprintf("%s.%s", lang.String(), c.FormatBundleFile)
filepath := path.Join(c.RootPath, bundleFilePath)
c.loadMessage(filepath)
}
}
// initLocalizerMap initializes localizers for each supported language.
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()
}
// Localize localizes a message using the provided context and parameters.
func Localize(c echo.Context, params interface{}) (string, error) {
local := c.Get(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(c, 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
default:
return "", fmt.Errorf("i18n.Localize error: %v", "Invalid params type")
}
message, err := localizer.(*i18n.Localizer).Localize(localizeConfig)
if err != nil {
return "", fmt.Errorf("i18n.Localize error: %v", err)
}
return message, nil
}
// MustLocalize is a helper function to localize a message, panicking on error.
func MustLocalize(c echo.Context, params interface{}) string {
message, err := Localize(c, params)
if err != nil {
panic(err)
}
return message
}
// NewMiddleware creates a new i18n middleware handler with the provided configuration.
func NewMiddleware(config ...*Config) echo.MiddlewareFunc {
cfg := configDefault(config...)
bundle := i18n.NewBundle(cfg.DefaultLanguage)
bundle.RegisterUnmarshalFunc(cfg.FormatBundleFile, cfg.UnmarshalFunc)
cfg.bundle = bundle
cfg.loadMessages()
cfg.initLocalizerMap()
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set(localsKey, cfg)
return next(c)
}
}
}
var ConfigDefault = &Config{
DefaultLanguage: language.English,
AcceptLanguages: []language.Tag{language.Chinese, language.English},
FormatBundleFile: "yaml",
Loader: LoaderFunc(os.ReadFile),
RootPath: "./example/localize",
LangHandler: defaultLangHandler,
UnmarshalFunc: yaml.Unmarshal,
}
// configDefault provides default values for the configuration
func configDefault(config ...*Config) *Config {
if len(config) == 0 {
return ConfigDefault
}
cfg := config[0]
if cfg.DefaultLanguage == language.Und {
cfg.DefaultLanguage = language.English
}
if cfg.AcceptLanguages == nil {
cfg.AcceptLanguages = []language.Tag{language.Chinese, language.English}
}
if cfg.FormatBundleFile == "" {
cfg.FormatBundleFile = "yaml"
}
if cfg.Loader == nil {
cfg.Loader = LoaderFunc(os.ReadFile)
}
if cfg.RootPath == "" {
cfg.RootPath = "./example/localize"
}
if cfg.LangHandler == nil {
cfg.LangHandler = defaultLangHandler
}
if cfg.UnmarshalFunc == nil {
cfg.UnmarshalFunc = yaml.Unmarshal
}
return cfg
}
// defaultLangHandler returns the default language based on the request context.
func defaultLangHandler(c echo.Context, defaultLang string) string {
if c == nil || c.Request() == nil {
return defaultLang
}
var lang string
lang = c.QueryParam("lang")
if lang != "" {
return lang
}
lang = c.Request().Header.Get("Accept-Language")
if lang != "" {
return lang
}
return defaultLang
}