Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix fiberi18n error on concurrent requests #595

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions fiberi18n/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,23 @@ var ConfigDefault = &Config{
FormatBundleFile: "yaml",
UnmarshalFunc: yaml.Unmarshal,
Loader: LoaderFunc(os.ReadFile),
LangHandler: func(c *fiber.Ctx, defaultLang string) string {
if c == nil {
return defaultLang
}
lang := c.Get("Accept-Language")
if lang != "" {
return lang
}
lang = c.Query("lang")
if lang == "" {
return defaultLang
}
LangHandler: defaultLangHandler,
}

func defaultLangHandler(c *fiber.Ctx, defaultLang string) string {
var lang string
lang = c.Query("lang")
if lang != "" {
return lang
}

lang = c.Get("Accept-Language")
if lang != "" {
return lang
},
}

return defaultLang

}

func configDefault(config ...*Config) *Config {
Expand Down
67 changes: 41 additions & 26 deletions fiberi18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,59 @@ var appCfg *Config
// New creates a new middleware handler
func New(config ...*Config) fiber.Handler {
appCfg = configDefault(config...)
appCfg.bundle = initBundle()
loadMessages()
appCfg.localizerMap = initLocalizerMap()

return func(c *fiber.Ctx) error {
if appCfg.Next != nil && appCfg.Next(c) {
return c.Next()
}
appCfg.ctx = c
return c.Next()
}
}

func initBundle() *i18n.Bundle {
bundle := i18n.NewBundle(appCfg.DefaultLanguage)
bundle.RegisterUnmarshalFunc(appCfg.FormatBundleFile, appCfg.UnmarshalFunc)
appCfg.bundle = bundle

return bundle
}

func loadMessage(filepath string) {
buf, err := appCfg.Loader.LoadMessage(filepath)
if err != nil {
panic(err)
}
if _, err := appCfg.bundle.ParseMessageFileBytes(buf, filepath); err != nil {
panic(err)
}
}

func loadMessages() {
for _, lang := range appCfg.AcceptLanguages {
bundleFile := fmt.Sprintf("%s.%s", lang.String(), appCfg.FormatBundleFile)
filepath := path.Join(appCfg.RootPath, bundleFile)

buf, err := appCfg.Loader.LoadMessage(filepath)
if err != nil {
panic(err)
}
if _, err := appCfg.bundle.ParseMessageFileBytes(buf, filepath); err != nil {
panic(err)
}
loadMessage(filepath)
}
}

return func(c *fiber.Ctx) error {
if appCfg.Next != nil && appCfg.Next(c) {
return c.Next()
}

appCfg.ctx = c
appCfg.localizerMap = map[string]*i18n.Localizer{}
for _, lang := range appCfg.AcceptLanguages {
s := lang.String()
appCfg.localizerMap[s] = i18n.NewLocalizer(appCfg.bundle, s)
}

// dataPool.Put(appCfg)
func initLocalizerMap() map[string]*i18n.Localizer {
localizerMap := map[string]*i18n.Localizer{}

lang := appCfg.DefaultLanguage.String()
if _, ok := appCfg.localizerMap[lang]; !ok {
appCfg.localizerMap[lang] = i18n.NewLocalizer(appCfg.bundle, lang)
}
for _, lang := range appCfg.AcceptLanguages {
s := lang.String()
localizerMap[s] = i18n.NewLocalizer(appCfg.bundle, s)
}

return c.Next()
lang := appCfg.DefaultLanguage.String()
if _, ok := localizerMap[lang]; !ok {
localizerMap[lang] = i18n.NewLocalizer(appCfg.bundle, lang)
}

return localizerMap
}

/*
Expand Down
47 changes: 47 additions & 0 deletions fiberi18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,50 @@ func TestI18nZH(t *testing.T) {
})
}
}

func TestParallelI18n(t *testing.T) {
type args struct {
lang language.Tag
name string
}
tests := []struct {
name string
args args
want string
}{
{
name: "hello world",
args: args{
name: "",
lang: language.Chinese,
},
want: "你好",
},
{
name: "hello alex",
args: args{
name: "alex",
lang: language.Chinese,
},
want: "你好 alex",
},
{
name: "hello peter",
args: args{
name: "peter",
lang: language.English,
},
want: "hello peter",
},
}
t.Parallel()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := makeRequest(tt.args.lang, tt.args.name)
utils.AssertEqual(t, err, nil)
body, err := io.ReadAll(got.Body)
utils.AssertEqual(t, err, nil)
utils.AssertEqual(t, tt.want, string(body))
})
}
}